UNPKG

@thugdacake/operante

Version:

Cérebro Operante — o núcleo IA que invade e domina assistentes, criando uma mente única com Thug Developer

51 lines (46 loc) 1.1 kB
const mongoose = require('mongoose'); const bcrypt = require('bcryptjs'); const userSchema = new mongoose.Schema({ username: { type: String, required: true, unique: true, trim: true }, email: { type: String, required: true, unique: true, trim: true, lowercase: true }, password: { type: String, required: true }, role: { type: String, enum: ['admin', 'author', 'user'], default: 'user' }, createdAt: { type: Date, default: Date.now } }); // Hash da senha antes de salvar 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); } }); // Método para comparar senhas userSchema.methods.comparePassword = async function(candidatePassword) { return bcrypt.compare(candidatePassword, this.password); }; module.exports = mongoose.model('User', userSchema);