diff options
Diffstat (limited to 'lib/models')
-rw-r--r-- | lib/models/user.js | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/lib/models/user.js b/lib/models/user.js index 7272f688..aaf344de 100644 --- a/lib/models/user.js +++ b/lib/models/user.js @@ -3,6 +3,7 @@ // external modules var md5 = require("blueimp-md5"); var Sequelize = require("sequelize"); +var scrypt = require('scrypt'); // core var logger = require("../logger.js"); @@ -29,8 +30,30 @@ module.exports = function (sequelize, DataTypes) { }, refreshToken: { type: DataTypes.STRING + }, + email: { + type: Sequelize.TEXT, + validate: { + isEmail: true + } + }, + password: { + type: Sequelize.TEXT, + set: function(value) { + var hash = scrypt.kdfSync(value, scrypt.paramsSync(0.1)).toString("hex"); + this.setDataValue('password', hash); + } } }, { + instanceMethods: { + verifyPassword: function(attempt) { + if (scrypt.verifyKdfSync(new Buffer(this.password, "hex"), attempt)) { + return this; + } else { + return false; + } + } + }, classMethods: { associate: function (models) { User.hasMany(models.Note, { @@ -42,6 +65,9 @@ module.exports = function (sequelize, DataTypes) { constraints: false }); }, + getProfile: function (user) { + return user.profile ? User.parseProfile(user.profile) : (user.email ? User.parseProfileByEmail(user.email) : null); + }, parseProfile: function (profile) { try { var profile = JSON.parse(profile); @@ -81,6 +107,13 @@ module.exports = function (sequelize, DataTypes) { break; } return photo; + }, + parseProfileByEmail: function (email) { + var photoUrl = 'https://www.gravatar.com/avatar/' + md5(email); + return { + name: email.substring(0, email.lastIndexOf("@")), + photo: photoUrl += '?s=96' + }; } } }); |