api/models/userModel.js
2022-06-07 09:57:33 +05:30

52 lines
1.5 KiB
JavaScript

import mongoose from "mongoose"
import validator from "validator"
import bcrypt from "bcryptjs"
import jwt from "jsonwebtoken"
import crypto from "crypto"
const userSchema = new mongoose.Schema({
name: {
type: String,
required: [true, "Please Enter Your Name"],
maxLength: [30, "Name cannot exceed 30 characters"],
minLength: [3, "Name should have more than 4 characters"],
},
email: {
type: String,
required: [true, "Please Enter Your Email"],
unique: true,
validate: [validator.isEmail, "Please Enter a valid Email"],
},
password: {
type: String,
required: [true, "Please Enter Your Password"],
minLength: [4, "Password should be greater than 8 characters"],
select: false,//find not got passpord
},
}, { timestamps: true }
);
userSchema.pre("save", async function (next) {
if (!this.isModified("password")) {
next();
}
this.password = await bcrypt.hash(this.password, 10);
});
// JWT TOKEN
userSchema.methods.getJWTToken = function () {
return jwt.sign({ id: this._id }, process.env.JWT_SECRET, {//make token
expiresIn: "1d",
});
};
//process.env.JWT_SECRET
// Compare Password
userSchema.methods.comparePassword = async function (password) {
return await bcrypt.compare(password, this.password);
};
const UserModel = mongoose.model("User", userSchema);
export default UserModel;