zkt25/sk1/backend/models/User.js

41 lines
1.2 KiB
JavaScript

import mongoose from "mongoose";
import bcrypt from "bcrypt";
const profileSchema = new mongoose.Schema({
firstName: String,
lastName: String,
phone: String,
field: String,
rate: Number,
address: String,
city: String,
location: {
type: { type: String, enum: ["Point"], default: "Point" },
coordinates: { type: [Number], index: "2dsphere" },
},
published: { type: Boolean, default: false },
description: String, // ← new
picture: String, // ← new (URL or upload path)
});
const userSchema = new mongoose.Schema({
email: { type: String, required: true, unique: true },
password: { type: String, required: true },
role: { type: String, enum: ["normal", "pro"], default: "normal" },
profile: { type: profileSchema, default: {} },
});
// hash password before save
userSchema.pre("save", async function () {
if (!this.isModified("password")) return;
const salt = await bcrypt.genSalt(10);
this.password = await bcrypt.hash(this.password, salt);
});
// helper to compare passwords
userSchema.methods.comparePassword = function (plain) {
return bcrypt.compare(plain, this.password);
};
export const User = mongoose.model("User", userSchema);