【发布时间】:2021-04-12 11:44:07
【问题描述】:
我有一个这样的用户模型:
module.exports = {
attributes: {
email: {
type: 'string',
isEmail: true,
unique: true,
required: true
},
password: {
type: 'string',
required: true
}
},
beforeCreate: (value, next) => {
bcrypt.hash(value.password, 10, (err, hash) => {
if (err){
throw new Error(err);
}
value.password = hash;
next();
});
},
};
现在当我想在登录期间匹配密码时,我如何解密密码,如果可能的话我更愿意在用户模型文件中执行它。
控制器/login.js
module.exports = {
login: async (req, res) => {
try{
const user = await User.findOne({email: req.body.email});
if (!user){
throw new Error('Failed to find User');
}
// here I want to match the password by calling some compare
//function from userModel.js
res.status(201).json({user: user});
}catch(e){
res.status(401).json({message: e.message});
}
},
};
【问题讨论】:
标签: node.js mongodb sails.js bcrypt sails-mongo