Add missing variable def to User model

This commit is contained in:
Alex Holliday committed 2024-10-16 11:45:38 +08:00
1 parent 6753446387
commit 05dae6b060
1 file changed
+65 -65
+65 -65
View File
@@ -2,86 +2,86 @@ import mongoose from "mongoose";
import bcrypt from "bcrypt"; import bcrypt from "bcrypt";
const UserSchema = mongoose.Schema( const UserSchema = mongoose.Schema(
{ {
firstName: { firstName: {
type: String, type: String,
required: true, required: true,
}, },
lastName: { lastName: {
type: String, type: String,
required: true, required: true,
}, },
email: { email: {
type: String, type: String,
required: true, required: true,
unique: true, unique: true,
}, },
password: { password: {
type: String, type: String,
required: true, required: true,
}, },
avatarImage: { avatarImage: {
type: String, type: String,
}, },
profileImage: { profileImage: {
data: Buffer, data: Buffer,
contentType: String, contentType: String,
}, },
isActive: { isActive: {
type: Boolean, type: Boolean,
default: true, default: true,
}, },
isVerified: { isVerified: {
type: Boolean, type: Boolean,
default: false, default: false,
}, },
role: { role: {
type: [String], type: [String],
default: "user", default: "user",
enum: ["user", "admin", "superadmin", "demo"], enum: ["user", "admin", "superadmin", "demo"],
}, },
teamId: { teamId: {
type: mongoose.Schema.Types.ObjectId, type: mongoose.Schema.Types.ObjectId,
ref: "Team", ref: "Team",
immutable: true, immutable: true,
}, },
checkTTL: { checkTTL: {
type: Number, type: Number,
}, },
}, },
{ {
timestamps: true, timestamps: true,
} }
); );
UserSchema.pre("save", async function (next) { UserSchema.pre("save", async function (next) {
if (!this.isModified("password")) { if (!this.isModified("password")) {
next(); next();
} }
const salt = await bcrypt.genSalt(10); //genSalt is asynchronous, need to wait 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 this.password = await bcrypt.hash(this.password, salt); // hash is also async, need to eitehr await or use hashSync
next(); next();
}); });
UserSchema.pre("findOneAndUpdate", async function (next) { UserSchema.pre("findOneAndUpdate", async function (next) {
const update = this.getUpdate(); const update = this.getUpdate();
if ("password" in update) { if ("password" in update) {
const salt = await bcrypt.genSalt(10); //genSalt is asynchronous, need to wait 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 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) { UserSchema.methods.comparePassword = async function (submittedPassword) {
res = await bcrypt.compare(submittedPassword, this.password); const res = await bcrypt.compare(submittedPassword, this.password);
return res; return res;
}; };
const User = mongoose.model("User", UserSchema); const User = mongoose.model("User", UserSchema);
User.init().then(() => { User.init().then(() => {
console.log("User model initialized"); console.log("User model initialized");
}); });
export default mongoose.model("User", UserSchema); export default mongoose.model("User", UserSchema);