/
eb
/
nodejs-screencast
Обзор
Документация
Войти
/
eb
/
nodejs-screencast
Код
Запросы
0
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
chat/6/models/user.js
43 строки
924 B
Ilya Kantor
add new casts
12 сен 2013, 13:57
12 сен 2013, 13:57
54774f8
Код
Авторство
О чём код?
var crypto = require('crypto'); var mongoose = require('lib/mongoose'), Schema = mongoose.Schema; var schema = new Schema({ username: { type: String, unique: true, required: true }, hashedPassword: { type: String, required: true }, salt: { type: String, required: true }, created: { type: Date, default: Date.now } }); schema.methods.encryptPassword = function(password) { return crypto.createHmac('sha1', this.salt).update(password).digest('hex'); }; schema.virtual('password') .set(function(password) { this._plainPassword = password; this.salt = Math.random() + ''; this.hashedPassword = this.encryptPassword(password); }) .get(function() { return this._plainPassword; }); schema.methods.checkPassword = function(password) { return this.encryptPassword(password) === this.hashedPassword; }; exports.User = mongoose.model('User', schema);