Skip to content

Commit 94ef2a8

Browse files
authored
fix(auth): fix access tokens on delete account (#207)
* fix(auth): fix access tokens on delete account * test(user): fix user tests
1 parent d9518aa commit 94ef2a8

File tree

3 files changed

+482
-13
lines changed

3 files changed

+482
-13
lines changed

src/auth/guards/jwt.guard.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,36 @@
11
import { ExecutionContext, Injectable, UnauthorizedException } from '@nestjs/common';
22
import { AuthGuard } from '@nestjs/passport';
3+
import { RedisService } from 'src/redis/redis.service';
34

45
@Injectable()
56
export class JwtAuthGuard extends AuthGuard('jwt') {
6-
override canActivate(context: ExecutionContext) {
7-
return super.canActivate(context);
7+
constructor(private readonly redis_service: RedisService) {
8+
super();
9+
}
10+
11+
override async canActivate(context: ExecutionContext) {
12+
const can_activate = await super.canActivate(context);
13+
14+
if (!can_activate) {
15+
return false;
16+
}
17+
18+
const request = context.switchToHttp().getRequest();
19+
const user = request.user;
20+
21+
let is_deleted = false;
22+
if (user) {
23+
try {
24+
is_deleted = await this.redis_service.exists(`deleted_user:${user.id}`);
25+
} catch (error) {
26+
console.warn('Failed to check deleted user in Redis:', error.message);
27+
}
28+
if (is_deleted) {
29+
throw new UnauthorizedException('User account has been deleted');
30+
}
31+
}
32+
33+
return true;
834
}
935

1036
override handleRequest(err: any, user: any, info: any) {

0 commit comments

Comments
 (0)