feat: logout

This commit is contained in:
Abdalhamid Alhamad
2024-12-19 16:50:39 +03:00
parent 93f5d83825
commit 8112fb81a2
6 changed files with 41 additions and 8 deletions

View File

@ -1,15 +1,15 @@
import { ExecutionContext, Injectable } from '@nestjs/common';
import { Reflector } from '@nestjs/core';
import { AuthGuard } from '@nestjs/passport';
import { Observable } from 'rxjs';
import { IS_PUBLIC_KEY } from '../decorators';
import { CacheService } from '../modules/cache/services';
@Injectable()
export class AccessTokenGuard extends AuthGuard('access-token') {
constructor(protected reflector: Reflector) {
constructor(protected reflector: Reflector, private readonly cacheService: CacheService) {
super();
}
canActivate(context: ExecutionContext): boolean | Promise<boolean> | Observable<boolean> {
async canActivate(context: ExecutionContext) {
const isPublic = this.reflector.getAllAndOverride<boolean>(IS_PUBLIC_KEY, [
context.getHandler(),
context.getClass(),
@ -18,6 +18,21 @@ export class AccessTokenGuard extends AuthGuard('access-token') {
if (isPublic) {
return true;
}
return super.canActivate(context);
const isValid = await super.canActivate(context);
if (!isValid) {
return false;
}
const token = context.switchToHttp().getRequest().headers['authorization']?.split(' ')[1];
const isRevoked = await this.cacheService.get(token);
if (isRevoked) {
return false;
}
return !isRevoked;
}
}