Sails.js-加密密码
对于一个项目,我需要有用户,我想在数据库中储存一个加密密码 因此,我需要您的帮助,因为我需要在添加用户时加密密码,但在启动Sails.js-加密密码,sails.js,Sails.js,对于一个项目,我需要有用户,我想在数据库中储存一个加密密码 因此,我需要您的帮助,因为我需要在添加用户时加密密码,但在启动sails lift时,终端出错: In model `user`: The `toJSON` instance method is no longer supported. Instead, please use the `customToJSON` model setting. 配置: 我使用的是sails1.0beta和bcrypt1.0.2 Model User.j
sails lift
时,终端出错:
In model `user`:
The `toJSON` instance method is no longer supported.
Instead, please use the `customToJSON` model setting.
配置:
我使用的是sails1.0beta
和bcrypt1.0.2
Model User.js
/**
* User.js
*
* @description :: A model definition. Represents a database
table/collection/etc.
* @docs :: https://sailsjs.com/docs/concepts/models-and-
orm/models
*/
var bcrypt = require('bcrypt');
module.exports = {
attributes: {
firstname: {
type: 'string'
},
lastname: {
type: 'string'
},
password: {
type: 'string'
},
email: {
type: 'string',
unique: true
},
code: {
type: 'string',
unique: true
},
referring: {
type: 'string'
},
comment: {
type: 'text'
},
// Add reference to Profil
profil: {
model: 'profil'
},
toJSON: function() {
var obj = this.toObject();
delete obj.password;
return obj;
}
},
beforeCreate: function(user, cb) {
bcrypt.genSalt(10, function(err, salt) {
bcrypt.hash(user.password, salt, function(err, hash) {
if (err) {
console.log(err);
cb(err);
} else {
user.password = hash;
cb();
}
});
});
}
};
我想我正在使用一种旧的方式来加密密码,但我不知道或者找不到另一种方式来加密密码
提前感谢您看到的错误与加密无关。查看您的模型并注意toJSON函数。如错误消息所示,这是一个实例方法,不再受支持。按照建议执行:使用
customToJSON
model设置。我相信你会在文档中找到它。我知道这个问题很老,但希望这能给你一些启示
从Sails 1.0开始,实例方法不再受支持。文档建议您改为使用customToJSON
,但没有说明您应该在属性之外使用它
customToJSON允许您在发送数据之前使用自定义函数对数据进行字符串化。
在您的情况下,您需要省略密码。
使用customToJSON,您可以使用this
关键字访问返回的对象。建议不要改变这个对象,intsead创建一个副本
因此,对于您的示例,您将使用:
module.exports = {
attributes: {...},
customToJSON: function() {
return _.omit(this, ['password'])
},
beforeCreate: function(user, cb) {...}
};
我认为您应该执行以下操作,并记住将这个customToJSON函数放在
属性之后:{…},
attributes:{...},
customToJSON: function() {
// Return a shallow copy of this record with the password and ssn removed.
return _.omit(this, ['password'])
}
bcrypt
不是加密,只是名称不正确。它是为密码设计的加密散列方法,包含大量CPU计算时间,是一种安全的方法。关键是让攻击者花费大量时间通过暴力手段查找密码。