-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.js
65 lines (57 loc) · 1.51 KB
/
user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
'use strict';
/* eslint-disable func-names */
const bcrypt = require('bcrypt');
const mongoose = require('mongoose');
const uniqueValidator = require('mongoose-unique-validator');
// eslint-disable-next-line no-unused-vars
const logger = require('../../lib/logger')('M-USER');
const User = new mongoose.Schema({
email: {
type: String,
unique: true,
required: true,
},
token: {
type: String,
require: true,
},
passwordDigest: String,
}, {
timestamps: true,
});
User.plugin(uniqueValidator);
User.methods.comparePassword = function (password) {
const compared = bcrypt.compareSync(password, this.passwordDigest);
if (compared) {
return this.token;
}
const err = new Error('Not Authorized');
err.status = 401;
throw err;
};
/**
* Set the virtual _password field for saving the user and generating a password digest
* virtual handlers *must* be a function, not an arrow callback
* @param {string} password password to set to virtual field
*/
User.virtual('password').set(function (password) {
this._password = password;
});
User.pre('save', function (next) {
/* istanbul ignore else */
if (this._password) {
const salt = bcrypt.genSaltSync(null);
/* istanbul ignore next */
if (!salt) {
throw new Error('no salt');
}
const digest = bcrypt.hashSync(this._password, salt);
/* istanbul ignore next */
if (!digest) {
throw new Error('no digest');
}
this.passwordDigest = digest;
}
next();
});
module.exports = mongoose.model('User', User);