1- import mongoose , { Schema } from " mongoose" ;
2- import jwt from " jsonwebtoken"
3- import bcrypt from " bcryptjs"
1+ import mongoose , { Schema } from ' mongoose' ;
2+ import jwt from ' jsonwebtoken' ;
3+ import bcrypt from ' bcryptjs' ;
44
5- const userschema = new Schema ( {
6- username : {
7- type : String ,
8- required : true ,
9- unique : true ,
10- lowercase : true ,
11- trim : true ,
12- index : true
13- } ,
14- email : {
15- type : String ,
16- required : true ,
17- unique : true ,
18- lowercase : true ,
19- trim : true ,
20- } ,
21- fullname : {
22- type : String ,
23- required : true ,
24- trim : true ,
25- index : true
26- } ,
27- password : {
28- type : String ,
29- required : [ true , "Password is required" ]
30- } ,
31- refreshToken : {
32- type : String
33- } ,
34- role : {
35- type : mongoose . Schema . Types . ObjectId ,
36- ref : "Role" ,
37- default : null
38- }
39- } , {
40- timestamps : true
41- } )
42-
43- userschema . pre ( "save" , async function ( next ) {
44- if ( ! this . isModified ( "password" ) ) return next ( ) ;
5+ const userschema = new Schema ( {
6+ username :{
7+ type :String ,
8+ required :true ,
9+ unique :true ,
10+ lowercase :true ,
11+ trim :true ,
12+ index :true
13+ } ,
14+ email :{
15+ type :String ,
16+ required :true ,
17+ unique :true ,
18+ lowercase :true ,
19+ trim :true ,
20+ } ,
21+ fullname :{
22+ type :String ,
23+ required :true ,
24+ trim :true ,
25+ index :true
26+ } ,
27+ password :{
28+ type :String ,
29+ required :[ true , 'Password is required' ]
30+ } ,
31+ refreshToken :{
32+ type :String
33+ } ,
34+ role : {
35+ type : mongoose . Schema . Types . ObjectId ,
36+ ref : 'Role' ,
37+ default : null
38+ }
39+ } , {
40+ timestamps :true
41+ } ) ;
4542
46- this . password = await bcrypt . hash ( this . password , 10 )
47- next ( )
48- } )
43+ userschema . pre ( 'save' , async function ( next ) {
44+ if ( ! this . isModified ( 'password' ) ) return next ( ) ;
4945
50- userschema . methods . isPasswordCorrect = async function ( password ) {
51- return await bcrypt . compare ( password , this . password )
52- }
46+ this . password = await bcrypt . hash ( this . password , 10 ) ;
47+ next ( ) ;
48+ } ) ;
5349
54- userschema . methods . generateAccessToken = function ( ) {
55- try {
56- if ( ! process . env . JWT_SECRET ) {
57- throw new Error ( "Environment variables for token generation are missing" ) ;
58- }
50+ userschema . methods . isPasswordCorrect = async function ( password ) {
51+ return await bcrypt . compare ( password , this . password ) ;
52+ } ;
5953
60- return jwt . sign (
61- {
62- _id : this . _id ,
63- email : this . email ,
64- username : this . username ,
65- fullname : this . fullname ,
66- } ,
67- process . env . JWT_SECRET ,
68- {
69- expiresIn : '1d' ,
70- }
71- ) ;
72- } catch ( error ) {
73- throw new Error ( "Failed to generate access token" ) ;
54+ userschema . methods . genrateAccessToken = function ( ) {
55+ try {
56+ if ( ! process . env . JWT_SECRET ) {
57+ throw new Error ( 'Environment variables for token generation are missing' ) ;
7458 }
59+
60+ return jwt . sign (
61+ {
62+ id : this . _id ,
63+ email : this . email ,
64+ username : this . username ,
65+ fullname : this . fullname ,
66+ } ,
67+ process . env . JWT_SECRET ,
68+ {
69+ expiresIn : '1d' ,
70+ }
71+ ) ;
72+ }
73+ catch ( error ) {
74+ throw new Error ( 'Failed to generate access token' ) ;
75+ }
7576} ;
7677
7778userschema . methods . refreshAccessToken = function ( ) {
78- try {
79- if ( ! process . env . REFRESH_TOKEN_SECRET || ! process . env . REFRESH_TOKEN_EXPIRY ) {
80- throw new Error ( "Environment variables for refresh token generation are missing" ) ;
81- }
82-
83- return jwt . sign (
84- {
85- _id : this . _id ,
86- } ,
87- process . env . REFRESH_TOKEN_SECRET ,
88- {
89- expiresIn : '10d' ,
90- }
91- ) ;
92- } catch ( error ) {
93- throw new Error ( "Failed to generate refresh token" ) ;
79+ try {
80+ if ( ! process . env . REFRESH_TOKEN_SECRET || ! process . env . REFRESH_TOKEN_EXPIRY ) {
81+ throw new Error ( 'Environment variables for refresh token generation are missing' ) ;
9482 }
83+
84+ return jwt . sign (
85+ {
86+ _id : this . _id ,
87+ } ,
88+ process . env . REFRESH_TOKEN_SECRET ,
89+ {
90+ expiresIn : '10d' ,
91+ }
92+ ) ;
93+ }
94+ catch ( error ) {
95+ throw new Error ( 'Failed to generate refresh token' ) ;
96+ }
9597} ;
9698
9799
98- export const User = mongoose . model ( " User" , userschema )
100+ export const User = mongoose . model ( ' User' , userschema ) ;
0 commit comments