| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- import {
- CanActivate,
- ExecutionContext,
- Injectable,
- UnauthorizedException,
- } from '@nestjs/common';
- import { Reflector } from '@nestjs/core';
- import { AuthType } from '../../enums/auth-type.enum';
- import { AccessTokenGuard } from '../access-token/access-token.guard';
- import { AUTH_TYPE_KEY } from '../../decorators/auth-guard.decorator';
- @Injectable()
- export class AuthenticationGuard implements CanActivate {
- private static readonly defaultAuthType = AuthType.Bearer;
- private readonly authTypeGuardMap: Record<
- AuthType,
- CanActivate | CanActivate[]
- > = {
- [AuthType.Bearer]: this.accessTokenGuard,
- [AuthType.None]: { canActivate: () => true },
- };
- constructor(
- private readonly reflector: Reflector,
- private readonly accessTokenGuard: AccessTokenGuard,
- ) {}
- async canActivate(context: ExecutionContext): Promise<boolean> {
- const authTypes = this.reflector.getAllAndOverride<AuthType[]>(
- AUTH_TYPE_KEY,
- [context.getHandler(), context.getClass()],
- ) ?? [AuthenticationGuard.defaultAuthType];
- const guards = authTypes.map((type) => this.authTypeGuardMap[type]).flat();
- let error = new UnauthorizedException();
- for (const instance of guards) {
- const canActivate = await Promise.resolve(
- instance.canActivate(context),
- ).catch((err) => {
- error = err;
- });
- if (canActivate) {
- return true;
- }
- }
- throw error;
- }
- }
|