diff options
Diffstat (limited to 'server/providers/guards/auth.guard.ts')
-rw-r--r-- | server/providers/guards/auth.guard.ts | 17 |
1 files changed, 16 insertions, 1 deletions
diff --git a/server/providers/guards/auth.guard.ts b/server/providers/guards/auth.guard.ts index d7da81e..8c03ad8 100644 --- a/server/providers/guards/auth.guard.ts +++ b/server/providers/guards/auth.guard.ts @@ -1,13 +1,28 @@ import { Injectable, CanActivate, ExecutionContext } from '@nestjs/common'; import { JwtService } from '../services/jwt.service'; +import { SKIP_KEY } from 'server/decorators/skip.decorator'; +import { Reflector } from '@nestjs/core'; +import { Class } from 'server/dto/class.dto'; @Injectable() export class AuthGuard implements CanActivate { - constructor(private jwtService: JwtService) {} + constructor(private reflector: Reflector, private jwtService: JwtService) {} canActivate(context: ExecutionContext) { + const skippedGuards = this.reflector.getAllAndOverride<Class<CanActivate>[]>(SKIP_KEY, [ + context.getHandler(), + context.getClass(), + ]); + if (skippedGuards) { + const skippedGuard = skippedGuards.find((guard) => this instanceof guard); + if (skippedGuard) { + return true; + } + } const req = context.switchToHttp().getRequest(); const authHeader = req.headers.authorization; + if (!authHeader) return false; + const jwt = authHeader.split(' ')[1]; try { req.jwtBody = this.jwtService.parseToken(jwt); |