@experium/nest-bruteforce-guard
Version:
Authorization protection from bruteforce
23 lines (18 loc) • 832 B
text/typescript
import { CallHandler, ExecutionContext, Injectable, NestInterceptor } from '@nestjs/common';
import { Observable } from 'rxjs';
import { catchError, tap } from 'rxjs/operators';
import { BruteforceGuardService } from './bruteforce-guard.service';
()
export class BruteforceGuardInterceptor implements NestInterceptor {
constructor(private readonly bruteforceGuardService: BruteforceGuardService) {}
async intercept(context: ExecutionContext, next: CallHandler): Promise<Observable<any>> {
await this.bruteforceGuardService.canLogin(context);
return next.handle().pipe(
catchError(async (err) => {
await this.bruteforceGuardService.saveErrorAttempt(context, err);
throw err;
}),
tap(async () => await this.bruteforceGuardService.saveSuccessAttempt(context)),
);
}
}