-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUser.js
103 lines (85 loc) · 2.65 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// importing all the requirements
const mongoose = require ('mongoose');
const uniqueValidator = require ('mongoose-unique-validator');
const path = require ('path');
const bcrypt = require ('bcrypt');
const { config } = require (path.resolve (__dirname, '..', '..', 'appConfig', 'config'));
const { validateEmail, validatePassword, validateUsername } = require (path.resolve (__dirname, '..', '..', 'validators', 'validator'));
// Defining the User Schema
const UserSchema = new mongoose.Schema ({
username: {
type: String,
unique: true,
required: true,
trim: true,
validate: {
validator: (username) => {
return validateUsername (username);
},
message: '{VALUE} is not a valid username'
}
},
email: {
type: String,
unique: true,
required: true,
trim: true,
validate: {
validator: (email) => {
return validateEmail (email);
},
message: '{VALUE} is not a valid email'
}
},
password: {
type: String,
trim: true,
required: true,
validate: {
validator: (password) => {
return validatePassword (password);
},
message: '{VALUE} is not a valid password'
}
},
refresh_token: {
type: String,
default: ''
},
reset_token: {
type: String,
default: ''
},
articles: [
{
type: mongoose.Schema.Types.ObjectId,
ref: 'article'
}
]
}, { timestamps: true });
UserSchema.plugin (uniqueValidator);
// Defining the function that needs to get executed before insertion of any document
// Always remember to make use of ES-5 function syntax in defining statics and method functions in schema
// because ES-6 functions cannot work with `this` keyword and arguments array, but these can be our requirements
UserSchema.pre ('save', function (next) {
let user = this; // the current document that needs to be inserted
bcrypt.hash (user.password, config.saltRounds, (error, hash) => {
if (error) {
return next (error);
}
else {
user.password = hash;
next ();
}
})
})
// Defining the comparePassword static function
// this will check for correct password
UserSchema.statics.comparePassword = function (plainTextpassword, hashedPassword) {
return bcrypt.compare (plainTextpassword, hashedPassword) // it returns a promise
}
const User = mongoose.model ('user', UserSchema);
// exporting the User Model
module.exports = {
User
}