Skip to content

Commit 1d5508e

Browse files
Sukomal07krishnaacharyaa
authored andcommitted
fix: fix user model
1 parent 09e32fd commit 1d5508e

File tree

1 file changed

+79
-8
lines changed

1 file changed

+79
-8
lines changed

Diff for: backend/models/user.js

+79-8
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,98 @@
11
import { Schema, model } from 'mongoose';
2+
import JWT from 'jsonwebtoken'
3+
import bcrypt from 'bcryptjs'
4+
import crypto from 'crypto'
5+
import { ACCESS_TOKEN_EXPIRES_IN, JWT_SECRET, REFRESH_TOKEN_EXPIRES_IN } from '../config/utils.js';
26

37
const userSchema = new Schema({
4-
name: {
8+
userName: {
59
type: String,
6-
required: [true, 'User name is required.'],
10+
required: [true, 'Username is required'],
11+
lowercase: true,
12+
unique: true,
13+
trim: true,
14+
index: true
15+
},
16+
fullName: {
17+
type: String,
18+
required: [true, 'Name is required'],
19+
minLength: [3, 'Name must be at least 3 character'],
20+
maxLength: [15, 'Name should be less than 15 character'],
21+
trim: true
722
},
823
email: {
924
type: String,
10-
required: [true, 'Email is required.'],
25+
unique: true,
26+
required: [true, 'Email is required'],
27+
trim: true,
28+
match: [/^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/, 'Please Enter a valid email address']
1129
},
1230
password: {
1331
type: String,
14-
required: false,
32+
required: [true, 'Password is required'],
33+
minLength: [8, 'Password must be at least 8 character '],
34+
match: [/^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,}$/, 'Password must be contains at least one uppercase and one lowercase and one digit and one special character'],
35+
select: false
1536
},
1637
avatar: {
1738
type: String,
1839
required: false,
1940
},
2041
role: {
2142
type: String,
22-
default: 'user',
43+
default: 'USER',
44+
enum: ['USER', 'ADMIN']
2345
},
24-
posts: [{ type: Schema.Types.ObjectId, ref: 'Post' }],
25-
});
46+
posts: [
47+
{
48+
type: Schema.Types.ObjectId,
49+
ref: 'Post'
50+
}
51+
],
52+
refreshToken: String,
53+
forgotPasswordToken: String,
54+
forgotPasswordExpiry: Date
55+
}, { timestamps: true });
56+
57+
userSchema.pre('save', async function (next) {
58+
if (!this.isModified('password')) {
59+
return next();
60+
}
61+
this.password = await bcrypt.hash(this.password, 10);
62+
})
63+
64+
userSchema.methods = {
65+
isPasswordCorrect: async function (password) {
66+
return await bcrypt.compare(password, this.password);
67+
},
68+
generateAccessToken: async function () {
69+
return JWT.sign({
70+
_id: this._id,
71+
username: this.userName,
72+
email: this.email,
73+
role: this.role
74+
}, JWT_SECRET, {
75+
expiresIn: ACCESS_TOKEN_EXPIRES_IN
76+
})
77+
},
78+
generateRefreshToken: async function () {
79+
return JWT.sign({
80+
_id: this._id,
81+
username: this.userName,
82+
email: this.email,
83+
role: this.role
84+
}, JWT_SECRET, {
85+
expiresIn: REFRESH_TOKEN_EXPIRES_IN
86+
})
87+
},
88+
generateResetToken: async function () {
89+
const resetToken = crypto.randomBytes(20).toString('hex')
90+
this.forgotPasswordToken = crypto.createHash('sha256').update(resetToken).digest('hex')
91+
this.forgotPasswordExpiry = Date.now() + 15 * 60 * 1000
92+
return resetToken;
93+
}
94+
}
95+
96+
const User = model('User', userSchema);
2697

27-
export default model('User', userSchema);
98+
export default User

0 commit comments

Comments
 (0)