Spaces:
Sleeping
Sleeping
File size: 1,997 Bytes
c01955c | 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 | import mongoose from "mongoose";
import bcrypt from "bcrypt";
import jwt from "jsonwebtoken";
const userSchema = new mongoose.Schema(
{
fullName: {
type: String,
required: [true,"fullName name cannot be empty"],
trim:true
},
password: {
type: String,
required: [true,"password is required"],
},
avatar: {
type: String ,// Cloudinary image URL
},
email:{
type:String,
required:[true,"email is required"],
unique:true,
trim:true
},
username: {
type: String,
required: [true, "username is required"],
unique: true,
trim: true,
lowercase: true
},
refreshToken: {
type: String
},
resumes: [{type:mongoose.Schema.ObjectId,ref:"templates",trim:true}], // URLs of previously generated resumes
aboutUser: {type:String,trim:true}, // statements about user
temp_data:Object
},
{
timestamps: true // createdAt & updatedAt auto
}
);
userSchema.pre("save", async function () {
if(!this.isModified("password")) return;
this.password = await bcrypt.hash(this.password, 10)
})
userSchema.methods.isPasswordCorrect = async function(password:any){
return await bcrypt.compare(password, this.password)
}
userSchema.methods.generateAccessToken = function(){
return jwt.sign(
{
_id: this._id,
email: this.email,
username: this.username,
fullName: this.fullName
},
process.env.ACCESS_TOKEN_SECRET as string,
{
expiresIn: process.env.ACCESS_TOKEN_EXPIRY
}
)
}
userSchema.methods.generateRefreshToken = function(){
return jwt.sign(
{
_id: this._id,
},
process.env.REFRESH_TOKEN_SECRET as string,
{
expiresIn: process.env.REFRESH_TOKEN_EXPIRY
}
)
}
const User = mongoose.model("User", userSchema);
export default User;
|