This commit is contained in:
Ccjcarkbla
2026-06-13 20:42:08 -04:00
parent a2c300678b
commit f4f0a2c93a
13 changed files with 8267 additions and 22 deletions
+12 -6
View File
@@ -1,5 +1,6 @@
'use strict';
const { Op } = require('sequelize');
const bcrypt = require('bcryptjs');
const jwt = require('jsonwebtoken');
const crypto = require('crypto');
@@ -60,22 +61,27 @@ async function createDeviceToken(models, userId, orgId, deviceName, deviceType)
return { rawToken, record };
}
async function login(models, req, { username, password, rememberDevice, deviceName, deviceType }) {
checkLockout(username);
async function login(models, req, { identifier, password, rememberDevice, deviceName, deviceType }) {
checkLockout(identifier);
const user = await models.User.findOne({
where: { username },
where: {
[Op.or]: [
{ username: identifier },
{ email: identifier },
],
},
attributes: ['id', 'username', 'email', 'role', 'organizationId', 'isActive', 'password'],
});
const valid = user && user.isActive && await bcrypt.compare(password, user.password);
if (!valid) {
recordFailure(username);
recordFailure(identifier);
throw new AppError('LOGIN_FAILED', 'Invalid username or password.', 401);
}
clearFailures(username);
clearFailures(identifier);
await user.update({ lastLoginAt: new Date() });
@@ -89,7 +95,7 @@ async function login(models, req, { username, password, rememberDevice, deviceNa
const userPublic = { id: user.id, username: user.username, role: user.role, organizationId: user.organizationId };
await auditLog(req, 'auth.login.success', 'user', user.id, { username });
await auditLog(req, 'auth.login.success', 'user', user.id, { identifier });
return { jwt: token, user: userPublic, ...(deviceToken ? { deviceToken } : {}) };
}