1- import mongoose , { Schema } from "mongoose" ;
1+ import mongoose , { Schema } from "mongoose" ;
22import jwt from "jsonwebtoken"
33import 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
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
1313 } ,
14- email :{
15- type :String ,
16- required :true ,
17- unique :true ,
18- lowercase :true ,
19- trim :true ,
14+ email : {
15+ type : String ,
16+ required : true ,
17+ unique : true ,
18+ lowercase : true ,
19+ trim : true ,
2020 } ,
21- fullname :{
22- type :String ,
23- required :true ,
24- trim :true ,
25- index :true
21+ fullname : {
22+ type : String ,
23+ required : true ,
24+ trim : true ,
25+ index : true
2626 } ,
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
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
4141} )
4242
43- userschema . pre ( "save" , async function ( next ) {
44- if ( ! this . isModified ( "password" ) ) return next ( ) ;
43+ userschema . pre ( "save" , async function ( next ) {
44+ if ( ! this . isModified ( "password" ) ) return next ( ) ;
4545
46- this . password = await bcrypt . hash ( this . password , 10 )
46+ this . password = await bcrypt . hash ( this . password , 10 )
4747 next ( )
4848} )
4949
50- userschema . methods . isPasswordCorrect = async function ( password ) {
51- return await bcrypt . compare ( password , this . password )
50+ userschema . methods . isPasswordCorrect = async function ( password ) {
51+ return await bcrypt . compare ( password , this . password )
5252}
5353
54- userschema . methods . genrateAccessToken = function ( ) {
54+ userschema . methods . generateAccessToken = function ( ) {
5555 try {
56- if ( ! process . env . JWT_SECRET ) {
56+ if ( ! process . env . JWT_SECRET ) {
5757 throw new Error ( "Environment variables for token generation are missing" ) ;
5858 }
5959
@@ -95,4 +95,4 @@ userschema.methods.refreshAccessToken = function () {
9595} ;
9696
9797
98- export const User = mongoose . model ( "User" , userschema )
98+ export const User = mongoose . model ( "User" , userschema )
0 commit comments