Skip to content

Commit

Permalink
Merge pull request #38 from Brints/update-user
Browse files Browse the repository at this point in the history
feat: refactoring login attempts provider
  • Loading branch information
aniebietafia authored Sep 29, 2024
2 parents a2204c2 + b4630ff commit b4fb21d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 19 deletions.
34 changes: 26 additions & 8 deletions brints-estate-api/src/auth/providers/login-user.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ 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 {
constructor(
@InjectRepository(User)
private readonly userRepository: Repository<User>,

@InjectRepository(LoginAttempts)
private readonly loginAttemptsRepository: Repository<LoginAttempts>,

@Inject(forwardRef(() => HashingProvider))
private readonly hashingProvider: HashingProvider,

Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,10 @@ export class LoginAttemptsProvider {
return false;
}

public async resetLoginAttempts(user: User): Promise<void> {
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<void> {
loginAttempts.isBlocked = false;
loginAttempts.blockedUntil = null;
await this.loginAttemptsRepository.save(loginAttempts);
Expand Down

0 comments on commit b4fb21d

Please sign in to comment.