diff --git a/backend/src/services/authService.ts b/backend/src/services/authService.ts index 3f91bcd3..33219408 100644 --- a/backend/src/services/authService.ts +++ b/backend/src/services/authService.ts @@ -310,8 +310,10 @@ export async function login( throw e; } - const settings = await settingsRepository.getSettings(); - const failedCount = await userRepository.getFailedLoginCount(user.id); + const [settings, failedCount] = await Promise.all([ + settingsRepository.getSettings(), + userRepository.getFailedLoginCount(user.id), + ]); const afterFailures = settings.login_captcha_after_failures; if (afterFailures > 0 && failedCount >= afterFailures) { try { @@ -382,8 +384,10 @@ export async function login( } } - await loginLockoutService.clearLoginFailures(user.id); - const payload = await buildJwtPayload(user); + const [payload] = await Promise.all([ + buildJwtPayload(user), + loginLockoutService.clearLoginFailures(user.id), + ]); const accessExpiry = settings.access_token_expiry || config.jwt.accessExpiry; const accessToken = jwt.sign(payload, config.jwt.secret, { expiresIn: accessExpiry } as jwt.SignOptions); const refreshToken = uuidv4(); @@ -393,33 +397,40 @@ export async function login( ipAddress: auditContext?.ipAddress, userAgent: auditContext?.userAgent, }); - await enforceConcurrentSessionLimit(user.id, auditContext); + // Session trim + telemetry must not block the login response. + void enforceConcurrentSessionLimit(user.id, auditContext).catch(() => undefined); if (auditContext) { - await auditLogRepository.create({ - userId: user.id, - action: 'login', - ipAddress: auditContext.ipAddress, - userAgent: auditContext.userAgent, - }); - queueUserAdminStatsRefresh(user.id); - const fingerprint = await registerLoginFingerprint( - user.id, - auditContext.ipAddress, - auditContext.userAgent - ); - const notifyCtx = { - ipAddress: auditContext.ipAddress, - userAgent: auditContext.userAgent, - countryCode: fingerprint.countryCode, - }; - if (fingerprint.isNewDevice) { - void securityNotificationService.notifyNewLogin(user.id, notifyCtx); - } - if (fingerprint.isNewCountry && fingerprint.countryCode) { - void securityNotificationService.notifyNewCountryLogin(user.id, notifyCtx); - } - const risk = assessLoginRisk(fingerprint); - void recordLoginRisk(user.id, risk, notifyCtx); + void (async () => { + try { + await auditLogRepository.create({ + userId: user.id, + action: 'login', + ipAddress: auditContext.ipAddress, + userAgent: auditContext.userAgent, + }); + queueUserAdminStatsRefresh(user.id); + const fingerprint = await registerLoginFingerprint( + user.id, + auditContext.ipAddress, + auditContext.userAgent + ); + const notifyCtx = { + ipAddress: auditContext.ipAddress, + userAgent: auditContext.userAgent, + countryCode: fingerprint.countryCode, + }; + if (fingerprint.isNewDevice) { + void securityNotificationService.notifyNewLogin(user.id, notifyCtx); + } + if (fingerprint.isNewCountry && fingerprint.countryCode) { + void securityNotificationService.notifyNewCountryLogin(user.id, notifyCtx); + } + const risk = assessLoginRisk(fingerprint); + void recordLoginRisk(user.id, risk, notifyCtx); + } catch { + /* best-effort post-login side effects */ + } + })(); } void mergeVisitorSupportForUser(user.id, auditContext?.visitorSupportToken); const decoded = jwt.decode(accessToken) as { exp: number }; diff --git a/prediqo_mobile/lib/features/auth/presentation/login_screen.dart b/prediqo_mobile/lib/features/auth/presentation/login_screen.dart index dd281442..8a1b61ec 100644 --- a/prediqo_mobile/lib/features/auth/presentation/login_screen.dart +++ b/prediqo_mobile/lib/features/auth/presentation/login_screen.dart @@ -235,7 +235,9 @@ class _LoginScreenState extends State { state.errorMessage != null) { final msg = state.errorMessage! .replaceFirst('Exception: ', ''); - if (state.isCaptchaChallenge || _captchaRequired) { + // Only refresh the math challenge on captcha errors. + // Other failures (wrong password/2FA) must keep the current prompt. + if (state.isCaptchaChallenge) { _applyMathChallenge(state.captcha); } if (state.isEmailNotVerified) {