Spaces:
Sleeping
Sleeping
| const mongoose = require('mongoose'); | |
| const bcrypt = require('bcryptjs'); | |
| const userSchema = new mongoose.Schema({ | |
| name: { type: String, required: true }, | |
| phone: { type: String, default: '' }, | |
| email: { type: String, default: '' }, | |
| password: { type: String, required: true }, | |
| firebaseUid: { type: String, default: '' }, | |
| photoURL: { type: String, default: '' }, | |
| addresses: [{ type: String }] | |
| }, { timestamps: true }); | |
| // Only hash if password is modified and not a Google placeholder | |
| userSchema.pre('save', async function (next) { | |
| if (!this.isModified('password')) return next(); | |
| if (this.password.startsWith('google_')) return next(); | |
| this.password = await bcrypt.hash(this.password, 10); | |
| next(); | |
| }); | |
| userSchema.methods.comparePassword = async function (candidatePassword) { | |
| return bcrypt.compare(candidatePassword, this.password); | |
| }; | |
| module.exports = mongoose.model('User', userSchema); | |