2024-05-14 11:12:22 -07:00
|
|
|
const mongoose = require("mongoose");
|
|
|
|
|
const bcrypt = require("bcrypt");
|
2024-05-09 17:06:00 -04:00
|
|
|
|
2024-05-27 11:57:25 -07:00
|
|
|
const UserSchema = mongoose.Schema(
|
|
|
|
|
{
|
2024-07-22 12:45:33 -04:00
|
|
|
firstName: {
|
2024-05-27 11:57:25 -07:00
|
|
|
type: String,
|
|
|
|
|
required: true,
|
|
|
|
|
},
|
2024-07-22 12:45:33 -04:00
|
|
|
lastName: {
|
2024-05-27 11:57:25 -07:00
|
|
|
type: String,
|
|
|
|
|
required: true,
|
|
|
|
|
},
|
|
|
|
|
email: {
|
|
|
|
|
type: String,
|
|
|
|
|
required: true,
|
|
|
|
|
unique: true,
|
|
|
|
|
},
|
|
|
|
|
password: {
|
|
|
|
|
type: String,
|
|
|
|
|
required: true,
|
|
|
|
|
},
|
2024-07-08 10:26:56 -07:00
|
|
|
avatarImage: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
2024-07-01 11:48:56 -07:00
|
|
|
profileImage: {
|
|
|
|
|
data: Buffer,
|
|
|
|
|
contentType: String,
|
2024-05-27 11:57:25 -07:00
|
|
|
},
|
|
|
|
|
isActive: {
|
|
|
|
|
type: Boolean,
|
|
|
|
|
default: true,
|
|
|
|
|
},
|
|
|
|
|
isVerified: {
|
|
|
|
|
type: Boolean,
|
|
|
|
|
default: false,
|
|
|
|
|
},
|
2024-07-02 10:24:06 -07:00
|
|
|
role: {
|
2024-07-18 12:42:02 -07:00
|
|
|
type: [String],
|
2024-07-02 10:24:06 -07:00
|
|
|
default: "user",
|
2024-08-26 09:46:23 -07:00
|
|
|
enum: ["user", "admin", "superadmin"],
|
2024-07-02 10:24:06 -07:00
|
|
|
},
|
2024-08-26 10:23:05 -07:00
|
|
|
teamId: {
|
|
|
|
|
type: mongoose.Schema.Types.ObjectId,
|
|
|
|
|
ref: "Team",
|
|
|
|
|
immutable: true,
|
2024-07-02 10:24:06 -07:00
|
|
|
},
|
2024-05-27 11:57:25 -07:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
timestamps: true,
|
|
|
|
|
}
|
|
|
|
|
);
|
2024-05-09 17:06:00 -04:00
|
|
|
|
2024-05-14 11:12:22 -07:00
|
|
|
UserSchema.pre("save", async function (next) {
|
|
|
|
|
if (!this.isModified("password")) {
|
|
|
|
|
next();
|
|
|
|
|
}
|
|
|
|
|
const salt = await bcrypt.genSalt(10); //genSalt is asynchronous, need to wait
|
2024-05-15 12:16:04 -07:00
|
|
|
this.password = await bcrypt.hash(this.password, salt); // hash is also async, need to eitehr await or use hashSync
|
|
|
|
|
next();
|
2024-05-14 11:12:22 -07:00
|
|
|
});
|
2024-05-09 17:06:00 -04:00
|
|
|
|
2024-07-10 11:51:27 -07:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
next();
|
|
|
|
|
});
|
|
|
|
|
|
2024-06-14 12:07:34 -07:00
|
|
|
UserSchema.methods.comparePassword = async function (submittedPassword) {
|
|
|
|
|
res = await bcrypt.compare(submittedPassword, this.password);
|
|
|
|
|
return res;
|
2024-05-15 12:16:04 -07:00
|
|
|
};
|
|
|
|
|
|
2024-08-26 15:27:05 -07:00
|
|
|
const User = mongoose.model("User", UserSchema);
|
|
|
|
|
|
|
|
|
|
User.init().then(() => {
|
|
|
|
|
console.log("User model initialized");
|
|
|
|
|
});
|
|
|
|
|
|
2024-05-14 11:12:22 -07:00
|
|
|
module.exports = mongoose.model("User", UserSchema);
|