-
Notifications
You must be signed in to change notification settings - Fork 176
Expand file tree
/
Copy pathjwt-auth.guard.ts
More file actions
29 lines (24 loc) · 838 Bytes
/
Copy pathjwt-auth.guard.ts
File metadata and controls
29 lines (24 loc) · 838 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import { Injectable, UnauthorizedException } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { AuthService } from '../auth.service';
@Injectable()
export class JwtAuthGuard extends AuthGuard('jwt') {
constructor(private readonly authService: AuthService) {
super();
}
async canActivate(context: any): Promise<boolean> {
const result = (await super.canActivate(context)) as boolean;
if (result) {
const request = context.switchToHttp().getRequest();
const user = request.user;
// Check if token is blacklisted
if (user && user.jti) {
const isBlacklisted = await this.authService.isTokenBlacklisted(user.jti);
if (isBlacklisted) {
throw new UnauthorizedException('Token has been revoked');
}
}
}
return result;
}
}