-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser.js
More file actions
54 lines (50 loc) · 1.15 KB
/
Copy pathuser.js
File metadata and controls
54 lines (50 loc) · 1.15 KB
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
const mongoose = require('mongoose');
const bcrypt = require('bcryptjs'); // Ensure this is bcryptjs
const userSchema = new mongoose.Schema(
{
name: {
type: String,
required: true,
},
email: {
type: String,
required: true,
unique: true,
lowercase: true,
match: [/^\S+@\S+\.\S+$/, 'Please use a valid email address'],
},
password: {
type: String,
required: true,
validate: {
validator: function(v) {
return /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*\W).{8,}$/.test(v);
},
message: props => `${props.value} is not a strong enough password`
},
},
deleted: {
type: Boolean,
default: false,
},
deletedAt: {
type: Date,
default: null,
},
},
{
timestamps: true,
}
);
userSchema.pre('save', async function (next) {
if (!this.isModified('password')) return next();
try {
const salt = await bcrypt.genSalt(10);
this.password = await bcrypt.hash(this.password, salt);
next();
} catch (error) {
next(error);
}
});
const User = mongoose.model('User', userSchema);
module.exports = User;