Spaces:
Sleeping
Sleeping
File size: 965 Bytes
7b3aac2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | 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);
|