UNPKG

@dax-crafta/auth

Version:

A powerful, flexible, and secure authentication plugin for the Crafta framework. Supports JWT, social login, 2FA, RBAC, audit logging, and enterprise-grade security features.

59 lines (54 loc) 1.3 kB
const mongoose = require('mongoose'); const bcrypt = require('bcryptjs'); const userSchema = new mongoose.Schema({ email: { type: String, required: true, unique: true, lowercase: true }, password: { type: String, required: true }, role: { type: String, enum: ['user', 'admin', 'moderator'], default: 'user' }, isVerified: { type: Boolean, default: false }, verificationToken: String, passwordResetToken: String, passwordResetExpires: Date, loginAttempts: { type: Number, default: 0 }, lockUntil: Date, twoFactorSecret: String, twoFactorEnabled: { type: Boolean, default: false }, refreshTokens: [{ token: String, expires: Date }], customFields: mongoose.Schema.Types.Mixed }, { timestamps: true }); userSchema.pre('save', async function(next) { if (this.isModified('password')) { this.password = await bcrypt.hash(this.password, 10); } next(); }); userSchema.methods.comparePassword = async function(password) { return bcrypt.compare(password, this.password); }; userSchema.methods.isLocked = function() { return this.lockUntil && this.lockUntil > Date.now(); }; module.exports = mongoose.model('User', userSchema);