【发布时间】:2015-04-08 05:13:23
【问题描述】:
/**
* User.js
*
* @description :: TODO: You might write a short summary of how this model works and what it represents here.
* @docs :: http://sailsjs.org/#!documentation/models
*/
var bcryptjs = require('bcryptjs');
function hashPassword(values, next) {
bcryptjs.hash(values.password, 10, function(err, hash) {
if (err) {
return next(err);
}
values.password = hash;
next();
});
}
module.exports = {
connection: 'mysql',
attributes: {
id:{
primaryKey: true,
autoIncrement: true,
unique: true
},
displayname:{
type: 'STRING',
required: true
},
password:{
type: 'STRING',
required: true
},
// Override toJSON instance method to remove password value
toJSON: function() {
var obj = this.toObject();
delete obj.password;
return obj;
},
},
// Lifecycle Callbacks
beforeCreate: function(values, next) {
hashPassword(values, next);
},
beforeUpdate: function(values, next) {
if (values.password) {
hashPassword(values, next);
}
else {
//IMPORTANT: The following is only needed when a BLANK password param gets submitted through a form. Otherwise, a next() call is enough.
User.findOne(values.id).done(function(err, user) {
if (err) {
next(err);
}
else {
values.password = user.password;
next();
}
});
}
},
validPassword: function(password, user, cb) {
bcryptjs.compare(password, user.password, function(err, match) {
if (err) cb(err);
if (match) {
cb(null, true);
} else {
cb(err);
}
});
}
};
hashPassword(values, next);在 beforeUpdate 方法中更改密码,同时更改用户模型的任何值,尽管我没有在“参数”中发送密码值。但是当我为用户更改密码时它工作正常。
示例:当我更改当前用户的密码时,它应该更改密码、散列并存储在数据库中。但是当我更新用户模型中的其他数据时,我不希望密码被更改(更改为随机密码)。
编辑:现在工作,更正代码: 现在,只有在 Update 方法中发送 password:password 时,它才会更新密码(散列和存储),否则它只会更新提供的用户字段。
控制器(UserController.js):
updateDisplayName: function(req, res) {
var userid = req.token;
var newDisplayName = req.param('newdisplayname');
User.update({id: userid},{displayname: newDisplayName}).exec(function afterwards(err,updated){
if (err) {
res.json(err);
} else {
res.json("Success");
}
});
},
模型(User.js):
beforeUpdate: function(values, next) {
if(values.password) {
hashPassword(values, next);
} else {
next();
}
},
【问题讨论】:
-
只检查 hashPassword 方法是否在 beforeUpdate 函数中被调用
-
这是问题,但仍在尝试找出解决方法。
-
只需在 beforeUpdate 函数中为 value.password 输入一条控制台消息,然后检查 value.password 的值是什么
-
我尝试了console.log,似乎旧的哈希密码再次被哈希并存储在数据库中。使旧密码不再起作用。
标签: javascript node.js sails.js passport.js bcrypt