В MEAN-приложении я определяю свою пользовательскую модель и шифрую пароль, используя поля хэш и соль, как эти:
var mongoose = require('mongoose');
var crypto = require('crypto');
var jwt = require('jsonwebtoken');
var UsersSchema = new mongoose.Schema({
personalId: {
type: String,
unique: "Personal Id already exists",
required: true
},
name: {
type: String,
required: true
},
surname:{
type: String,
required: true
},
username: {
type: String,
unique: "Username already exists",
required: "Please fill in a username",
lowercase: true
},
hash: String,
salt: String,
email:{
type: String,
unique: true,
lowercase: true,
trim: true
},
contract:{
type: String
},
role:{
type: String,
required: true
},
dateUpdated: {
type: Date
},
dateCreated: {
type: Date,
default: Date.now
}
});
UsersSchema.methods.setPassword = function (password) {
this.salt = crypto.randomBytes(16).toString('hex');
this.hash = crypto.pbkdf2Sync(password, this.salt, 1000, 64, 'sha512').toString('hex');
};
UsersSchema.methods.validPassword = function (password) {
var hash = crypto.pbkdf2Sync(password, this.salt, 1000, 64, 'sha512').toString('hex');
return this.hash === hash;
};
UsersSchema.methods.generateJwt = function () {
var expiry = new Date();
expiry.setDate(expiry.getDate() + 7);
return jwt.sign({
_id: this._id,
username: this.username,
exp: parseInt(expiry.getTime() / 1000),
}, "MY_SECRET"); // DO NOT KEEP YOUR SECRET IN THE CODE!
};
mongoose.model('Users', UsersSchema);
Когда я создаю нового пользователя, возвращаю объекту user complete (все значения), когда я получаю список пользователей, тоже возвращаю все значения для каждого пользователя.
Мой вопрос: Правильно ли возвращаются значения соли и хеша, когда я запрашиваю объекты пользователей?