From 05dae6b060824c152d296918e8511015626c01ce Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Wed, 16 Oct 2024 11:45:38 +0800 Subject: [PATCH] Add missing variable def to User model --- Server/db/models/User.js | 130 +++++++++++++++++++-------------------- 1 file changed, 65 insertions(+), 65 deletions(-) diff --git a/Server/db/models/User.js b/Server/db/models/User.js index dfdb6397d..37ecbcec0 100644 --- a/Server/db/models/User.js +++ b/Server/db/models/User.js @@ -2,86 +2,86 @@ import mongoose from "mongoose"; import bcrypt from "bcrypt"; const UserSchema = mongoose.Schema( - { - firstName: { - type: String, - required: true, - }, - lastName: { - type: String, - required: true, - }, - email: { - type: String, - required: true, - unique: true, - }, - password: { - type: String, - required: true, - }, - avatarImage: { - type: String, - }, - profileImage: { - data: Buffer, - contentType: String, - }, - isActive: { - type: Boolean, - default: true, - }, - isVerified: { - type: Boolean, - default: false, - }, - role: { - type: [String], - default: "user", - enum: ["user", "admin", "superadmin", "demo"], - }, - teamId: { - type: mongoose.Schema.Types.ObjectId, - ref: "Team", - immutable: true, - }, - checkTTL: { - type: Number, - }, - }, - { - timestamps: true, - } + { + firstName: { + type: String, + required: true, + }, + lastName: { + type: String, + required: true, + }, + email: { + type: String, + required: true, + unique: true, + }, + password: { + type: String, + required: true, + }, + avatarImage: { + type: String, + }, + profileImage: { + data: Buffer, + contentType: String, + }, + isActive: { + type: Boolean, + default: true, + }, + isVerified: { + type: Boolean, + default: false, + }, + role: { + type: [String], + default: "user", + enum: ["user", "admin", "superadmin", "demo"], + }, + teamId: { + type: mongoose.Schema.Types.ObjectId, + ref: "Team", + immutable: true, + }, + checkTTL: { + type: Number, + }, + }, + { + timestamps: true, + } ); UserSchema.pre("save", async function (next) { - if (!this.isModified("password")) { - next(); - } - const salt = await bcrypt.genSalt(10); //genSalt is asynchronous, need to wait - this.password = await bcrypt.hash(this.password, salt); // hash is also async, need to eitehr await or use hashSync - next(); + if (!this.isModified("password")) { + next(); + } + const salt = await bcrypt.genSalt(10); //genSalt is asynchronous, need to wait + this.password = await bcrypt.hash(this.password, salt); // hash is also async, need to eitehr await or use hashSync + next(); }); UserSchema.pre("findOneAndUpdate", async function (next) { - const update = this.getUpdate(); - if ("password" in update) { - const salt = await bcrypt.genSalt(10); //genSalt is asynchronous, need to wait - update.password = await bcrypt.hash(update.password, salt); // hash is also async, need to eitehr await or use hashSync - } + const update = this.getUpdate(); + if ("password" in update) { + const salt = await bcrypt.genSalt(10); //genSalt is asynchronous, need to wait + update.password = await bcrypt.hash(update.password, salt); // hash is also async, need to eitehr await or use hashSync + } - next(); + next(); }); UserSchema.methods.comparePassword = async function (submittedPassword) { - res = await bcrypt.compare(submittedPassword, this.password); - return res; + const res = await bcrypt.compare(submittedPassword, this.password); + return res; }; const User = mongoose.model("User", UserSchema); User.init().then(() => { - console.log("User model initialized"); + console.log("User model initialized"); }); export default mongoose.model("User", UserSchema);