-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathauth.js
66 lines (63 loc) · 1.6 KB
/
auth.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
66
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var bcrypt = require('bcrypt-nodejs');
var AuthSchema = new Schema({
// fullname: {
// type: String,
// required: false
// },
providerId: {
type: String,
default: 'password',
required: true
},
subject: {
type: String,
required: true
},
email: {
type: String,
},
password: {
type: String,
required: false,
// required: true,
// https://stackoverflow.com/questions/12096262/how-to-protect-the-password-field-in-mongoose-mongodb-so-it-wont-return-in-a-qu
// select: false
},
// id_project: {
// type: String,
// required: true
// }
}, {
timestamps: true
}
);
AuthSchema.pre('save', function (next) {
var user = this;
if (this.isModified('password') || this.isNew) {
bcrypt.genSalt(10, function (err, salt) {
if (err) {
return next(err);
}
bcrypt.hash(user.password, salt, null, function (err, hash) {
if (err) {
return next(err);
}
user.password = hash;
next();
});
});
} else {
return next();
}
});
AuthSchema.methods.comparePassword = function (passw, cb) {
bcrypt.compare(passw, this.password, function (err, isMatch) {
if (err) {
return cb(err);
}
cb(null, isMatch);
});
};
module.exports = mongoose.model('auth', AuthSchema);