api/models/userModel.js
pawan-dot 52a4570e85 cms
2022-11-01 16:08:06 +05:30

95 lines
2.6 KiB
JavaScript

import dotenv from 'dotenv'
dotenv.config()
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: [4, "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"],
},
phone: {
type: Number,
required: [true, "Please Enter Your phone no."],
maxLength: [12, "phone cannot exceed 12 characters"],
minLength: [6, "phone should have more than 6 characters"],
},
password: {
type: String,
required: [true, "Please Enter Your Password"],
minLength: [6, "Password should be greater than 6 characters"],
select: false,//find not got passpord
},
avatar: {
public_id: {
type: String,
required: true,
},
url: {
type: String,
required: true,
},
},
role: {
type: String,
default: "user",
},
// createdAt: {
// type: Date,
// default: Date.now,
// },
resetPasswordToken: String,
resetPasswordExpire: Date,
}, { 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);
};
// Compare Password
userSchema.methods.comparePassword = async function (password) {
return await bcrypt.compare(password, this.password);
};
// Generating Reset Token
userSchema.methods.getResetPasswordToken = function () {
// Generating Token
const resetToken = crypto.randomBytes(20).toString("hex");
// Hashing and adding reset PasswordToken to userSchema
this.resetPasswordToken = crypto
.createHash("sha256")
.update(resetToken)
.digest("hex");
//expire password time
// console.log(this.resetPasswordToken)
this.resetPasswordExpire = Date.now() + 15 * 60 * 1000;//15 minut
return resetToken;
};
const UserModel = mongoose.model("User", userSchema);
export default UserModel;