From b4630ff176e5a9fa128228f7a77d5d64ea6b926a Mon Sep 17 00:00:00 2001 From: aniebietafia Date: Sun, 29 Sep 2024 21:38:20 +0100 Subject: [PATCH] feat: refactoring login attempts provider --- .../src/auth/providers/login-user.provider.ts | 34 ++++++++++++++----- .../providers/login-attempts.provider.ts | 15 +++----- 2 files changed, 30 insertions(+), 19 deletions(-) diff --git a/brints-estate-api/src/auth/providers/login-user.provider.ts b/brints-estate-api/src/auth/providers/login-user.provider.ts index 76ed2c1..4cee604 100644 --- a/brints-estate-api/src/auth/providers/login-user.provider.ts +++ b/brints-estate-api/src/auth/providers/login-user.provider.ts @@ -9,6 +9,7 @@ import { CustomException } from '../../exceptions/custom.exception'; import { GenerateTokensProvider } from './generate-tokens.provider'; import { AccountStatus } from '../../enums/account-status.enum'; import { LoginAttemptsProvider } from '../../login-attempts/providers/login-attempts.provider'; +import { LoginAttempts } from 'src/login-attempts/entities/login-attempt.entity'; @Injectable() export class LoginUserProvider { @@ -16,6 +17,9 @@ export class LoginUserProvider { @InjectRepository(User) private readonly userRepository: Repository, + @InjectRepository(LoginAttempts) + private readonly loginAttemptsRepository: Repository, + @Inject(forwardRef(() => HashingProvider)) private readonly hashingProvider: HashingProvider, @@ -34,20 +38,34 @@ export class LoginUserProvider { throw new CustomException(HttpStatus.NOT_FOUND, 'User not found'); } + const loginAttempts = await this.loginAttemptsRepository.findOne({ + where: { id: user.login_attempts.id }, + }); + + if (!loginAttempts) { + throw new CustomException( + HttpStatus.NOT_FOUND, + 'Login attempts does not exist.', + ); + } + if ( - user.login_attempts.isBlocked && - user.login_attempts.blockedUntil && - user.login_attempts.blockedUntil < new Date() + loginAttempts.isBlocked && + loginAttempts.blockedUntil && + loginAttempts.blockedUntil > new Date() ) { - await this.loginAttemptsProvider.resetLoginAttempts(user); + await this.loginAttemptsProvider.attemptedLoginWhileBlocked(user); } if ( - user.login_attempts.isBlocked && - user.login_attempts.blockedUntil && - user.login_attempts.blockedUntil < new Date() + loginAttempts.isBlocked && + loginAttempts.blockedUntil && + loginAttempts.blockedUntil < new Date() ) { - await this.loginAttemptsProvider.attemptedLoginWhileBlocked(user); + await this.loginAttemptsProvider.resetLoginAttempts( + user, + user.login_attempts, + ); } const passwordMatch: boolean = await this.hashingProvider.comparePassword( diff --git a/brints-estate-api/src/login-attempts/providers/login-attempts.provider.ts b/brints-estate-api/src/login-attempts/providers/login-attempts.provider.ts index f20edf6..f7d95bf 100644 --- a/brints-estate-api/src/login-attempts/providers/login-attempts.provider.ts +++ b/brints-estate-api/src/login-attempts/providers/login-attempts.provider.ts @@ -22,17 +22,10 @@ export class LoginAttemptsProvider { return false; } - public async resetLoginAttempts(user: User): Promise { - const loginAttempts = await this.loginAttemptsRepository.findOne({ - where: { id: user.login_attempts.id }, - }); - - if (!loginAttempts) - throw new CustomException( - HttpStatus.NOT_FOUND, - 'Login attempts does not exist.', - ); - + public async resetLoginAttempts( + user: User, + loginAttempts: LoginAttempts, + ): Promise { loginAttempts.isBlocked = false; loginAttempts.blockedUntil = null; await this.loginAttemptsRepository.save(loginAttempts);