|
| 1 | +var mongoose = require("mongoose"); |
1 | 2 | var bcrypt = require("bcrypt"); |
2 | 3 |
|
3 | | -module.exports = function (mongoose) { |
4 | | - |
5 | | - var schema = mongoose.Schema({ |
6 | | - username: { |
7 | | - type: String, |
8 | | - unique: true |
9 | | - }, |
10 | | - email: { |
11 | | - type: String, |
12 | | - required: true, |
13 | | - unique: true |
14 | | - }, |
15 | | - password: { |
16 | | - type: String, |
17 | | - required: true |
18 | | - }, |
19 | | - first_name: { |
20 | | - type: String, |
21 | | - required: true |
22 | | - }, |
23 | | - score: { |
24 | | - type: Number, |
25 | | - default: 0 |
26 | | - }, |
27 | | - lang: { |
28 | | - type: String, |
29 | | - default: 'en' |
30 | | - }, |
31 | | - created_at: { |
32 | | - type: Date, |
33 | | - default: Date.now |
34 | | - }, |
35 | | - updated_at: { |
36 | | - type: Date, |
37 | | - default: Date.now |
38 | | - } |
39 | | - |
40 | | - }, { |
41 | | - versionKey: false, |
42 | | - timestamps: true |
| 4 | +var schema = mongoose.Schema({ |
| 5 | + username: { |
| 6 | + type: String, |
| 7 | + unique: true |
| 8 | + }, |
| 9 | + email: { |
| 10 | + type: String, |
| 11 | + required: true, |
| 12 | + unique: true |
| 13 | + }, |
| 14 | + password: { |
| 15 | + type: String, |
| 16 | + required: true |
| 17 | + }, |
| 18 | + first_name: { |
| 19 | + type: String, |
| 20 | + required: true |
| 21 | + }, |
| 22 | + score: { |
| 23 | + type: Number, |
| 24 | + default: 0 |
| 25 | + }, |
| 26 | + lang: { |
| 27 | + type: String, |
| 28 | + default: 'en' |
| 29 | + }, |
| 30 | + created_at: { |
| 31 | + type: Date, |
| 32 | + default: Date.now |
| 33 | + }, |
| 34 | + updated_at: { |
| 35 | + type: Date, |
| 36 | + default: Date.now |
43 | 37 | } |
44 | | - ); |
45 | 38 |
|
46 | | - schema.pre('save', function (next) { |
| 39 | + }, { |
| 40 | + versionKey: false, |
| 41 | + timestamps: true |
| 42 | + } |
| 43 | +); |
47 | 44 |
|
48 | | - var user = this; |
| 45 | +schema.pre('save', function (next) { |
49 | 46 |
|
50 | | - // generate a salt |
| 47 | + var user = this; |
51 | 48 |
|
52 | | - if(user.isModified("password") || user.isNew) { |
| 49 | + // generate a salt |
53 | 50 |
|
54 | | - bcrypt.genSalt(10, function (error, salt) { |
| 51 | + if (user.isModified("password") || user.isNew) { |
55 | 52 |
|
56 | | - if (error) return next(error); |
| 53 | + bcrypt.genSalt(10, function (error, salt) { |
57 | 54 |
|
58 | | - // hash the password along with our new salt |
| 55 | + if (error) return next(error); |
59 | 56 |
|
60 | | - bcrypt.hash(user.password, salt, function (error, hash) { |
| 57 | + // hash the password along with our new salt |
61 | 58 |
|
62 | | - if (error) return next(error); |
| 59 | + bcrypt.hash(user.password, salt, function (error, hash) { |
| 60 | + |
| 61 | + if (error) return next(error); |
63 | 62 |
|
64 | | - // override the cleartext password with the hashed one |
| 63 | + // override the cleartext password with the hashed one |
65 | 64 |
|
66 | | - user.password = hash; |
| 65 | + user.password = hash; |
67 | 66 |
|
68 | | - next(null, user); |
69 | | - }); |
| 67 | + next(null, user); |
70 | 68 | }); |
| 69 | + }); |
71 | 70 |
|
72 | | - }else{ |
73 | | - next(null, user); |
| 71 | + } else { |
| 72 | + next(null, user); |
| 73 | + } |
| 74 | +}); |
| 75 | + |
| 76 | +/** |
| 77 | + * Compare raw and encrypted password |
| 78 | + * @param password |
| 79 | + * @param callback |
| 80 | + */ |
| 81 | +schema.methods.comparePassword = function (password, callback) { |
| 82 | + bcrypt.compare(password, this.password, function (error, match) { |
| 83 | + if (error) callback(error); |
| 84 | + if (match) { |
| 85 | + callback(null, true); |
| 86 | + } else { |
| 87 | + callback(error, false); |
74 | 88 | } |
75 | 89 | }); |
| 90 | +} |
76 | 91 |
|
77 | | - /** |
78 | | - * Compare raw and encrypted password |
79 | | - * @param password |
80 | | - * @param callback |
81 | | - */ |
82 | | - schema.methods.comparePassword = function (password, callback) { |
83 | | - bcrypt.compare(password, this.password, function (error, match) { |
84 | | - if (error) callback(error); |
85 | | - if (match) { |
86 | | - callback(null, true); |
87 | | - } else { |
88 | | - callback(error, false); |
89 | | - } |
90 | | - }); |
91 | | - } |
| 92 | +module.exports = mongoose.model("user", schema, "user"); |
92 | 93 |
|
93 | | - return mongoose.model("user", schema, "user"); |
94 | | -}; |
0 commit comments