mahmoud
upload frontend & backend folders
66821dd
Raw History Blame
1.89 kB
import mongoose from "mongoose";
import crypto from "crypto";
const userSchema = new mongoose.Schema(
{
username: {
type: String,
required: true,
unique: true,
trim: true,
},
fullName: {
type: String,
trim: true,
default: "",
},
email: {
type: String,
trim: true,
lowercase: true,
unique: true,
sparse: true,
},
phone: {
type: String,
trim: true,
default: "",
},
password: {
type: String,
required: true,
},
role: {
type: String,
enum: ["super_admin", "owner", "admin", "cashier"],
default: "cashier",
},
status: {
type: String,
enum: ["active", "suspended"],
default: "active",
},
tenant: {
type: mongoose.Schema.Types.ObjectId,
ref: "Tenant",
default: null,
},
sessionToken: {
type: String,
default: null,
},
lastLoginAt: {
type: Date,
default: null,
},
},
{ timestamps: true }
);
userSchema.index({ tenant: 1, role: 1 });
userSchema.pre("save", async function () {
if (!this.isModified("password")) return;
const salt = crypto.randomBytes(16).toString("hex");
const hash = crypto.pbkdf2Sync(this.password, salt, 1000, 64, "sha512").toString("hex");
this.password = `${salt}:${hash}`;
});
userSchema.methods.matchPassword = function (enteredPassword) {
try {
const [salt, originalHash] = this.password.split(":");
const attemptHash = crypto.pbkdf2Sync(enteredPassword, salt, 1000, 64, "sha512").toString("hex");
return originalHash === attemptHash;
} catch (err) {
return false;
}
};
export default mongoose.model("User", userSchema);