r2.1
This commit is contained in:
@@ -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 } : {}) };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user