mirror of
https://github.com/ershisan99/flashcards-api.git
synced 2025-12-16 20:59:26 +00:00
add auth
This commit is contained in:
29
src/modules/auth/guards/base-auth.guard.ts
Normal file
29
src/modules/auth/guards/base-auth.guard.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import {
|
||||
CanActivate,
|
||||
ExecutionContext,
|
||||
Injectable,
|
||||
UnauthorizedException,
|
||||
} from '@nestjs/common';
|
||||
import { Observable } from 'rxjs';
|
||||
|
||||
@Injectable()
|
||||
export class BaseAuthGuard implements CanActivate {
|
||||
canActivate(
|
||||
context: ExecutionContext,
|
||||
): boolean | Promise<boolean> | Observable<boolean> {
|
||||
const request = context.switchToHttp().getRequest();
|
||||
const exceptedAuthInput = 'Basic YWRtaW46cXdlcnR5';
|
||||
if (!request.headers || !request.headers.authorization) {
|
||||
throw new UnauthorizedException([{ message: 'No any auth headers' }]);
|
||||
} else {
|
||||
if (request.headers.authorization != exceptedAuthInput) {
|
||||
throw new UnauthorizedException([
|
||||
{
|
||||
message: 'login or password invalid',
|
||||
},
|
||||
]);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user