/
morlli
/
zavav
Обзор
Документация
Войти
/
morlli
/
zavav
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
server/models/User.js
54 строки
1 KB
morlli
34
28 май 2026, 19:48
28 май 2026, 19:48
3299f08
Код
Авторство
О чём код?
const mongoose = require('mongoose'); const bcrypt = require('bcrypt'); const userSchema = new mongoose.Schema({ name: { type: String, required: true }, email: { type: String, required: true, unique: true, lowercase: true }, password: { type: String, required: true }, role: { type: String, enum: ['user', 'shelter', 'admin'], default: 'user' }, phone: { type: String }, address: { type: String }, createdAt: { type: Date, default: Date.now } }); // Hash password before saving userSchema.pre('save', async function(next) { if (!this.isModified('password')) return next(); try { const salt = await bcrypt.genSalt(10); this.password = await bcrypt.hash(this.password, salt); next(); } catch (error) { next(error); } }); // Method to compare passwords userSchema.methods.comparePassword = async function(candidatePassword) { return await bcrypt.compare(candidatePassword, this.password); }; module.exports = mongoose.model('User', userSchema);