UNPKG

http-response-handler

Version:

A comprehensive utility for standardizing HTTP responses in Node.js applications

80 lines (73 loc) 1.85 kB
import mongoose from "mongoose"; import bcrypt from "bcryptjs"; import jwt from "jsonwebtoken"; const saltRounds = 10; const userSchema = new mongoose.Schema( { username: { type: String, required: true, unique: true, // trim: true, }, email: { type: String, required: true, unique: true, // trim: true, // lowercase: true, }, password: { type: String, required: true, }, mobile: { type: Number, }, role: { type: String, enum: ["user", "admin"], default: "user", }, resetToken: { type: String, // stores the reset token default: null, }, resetTokenExpiry: { type: Date, // stores the expiration date of the reset token default: null, }, isActive: { type: Boolean, default: false, }, }, { timestamps: true, } ); // Hash password before saving // userSchema.pre('save', async function (next) { // if (this.isModified('password')) { // try { // const salt = await bcrypt.genSalt(saltRounds); // this.password = await bcrypt.hash(this.password, salt); // } catch (error) { // return next(error); // } // } // next(); // }); // Compare password method // userSchema.methods.comparePassword = function (password) { // return bcrypt.compare(password, this.password); // }; // Generate JWT token method // userSchema.methods.generateAuthToken = function () { // const token = jwt.sign( // { _id: this._id, role: this.role }, // process.env.JWT_SECRET, // Optional: Set token expiration // ); // return token; // }; export default mongoose.model("User", userSchema);