diff options
Diffstat (limited to 'server/providers/guards/auth.guard.ts')
-rw-r--r-- | server/providers/guards/auth.guard.ts | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/server/providers/guards/auth.guard.ts b/server/providers/guards/auth.guard.ts new file mode 100644 index 0000000..d7da81e --- /dev/null +++ b/server/providers/guards/auth.guard.ts @@ -0,0 +1,20 @@ +import { Injectable, CanActivate, ExecutionContext } from '@nestjs/common'; +import { JwtService } from '../services/jwt.service'; + +@Injectable() +export class AuthGuard implements CanActivate { + constructor(private jwtService: JwtService) {} + + canActivate(context: ExecutionContext) { + const req = context.switchToHttp().getRequest(); + const authHeader = req.headers.authorization; + const jwt = authHeader.split(' ')[1]; + try { + req.jwtBody = this.jwtService.parseToken(jwt); + } catch (e) { + return false; + } + + return true; + } +} |