mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2025-11-26 16:44:54 +00:00
35 lines
994 B
TypeScript
35 lines
994 B
TypeScript
import { ExecutionContext, Injectable, UnauthorizedException } from '@nestjs/common';
|
|
import { Reflector } from '@nestjs/core';
|
|
import { AuthGuard } from '@nestjs/passport';
|
|
import { IS_PUBLIC_KEY } from '../decorators';
|
|
import { CacheService } from '../modules/cache/services';
|
|
|
|
@Injectable()
|
|
export class AccessTokenGuard extends AuthGuard('access-token') {
|
|
constructor(protected reflector: Reflector, private readonly cacheService: CacheService) {
|
|
super();
|
|
}
|
|
async canActivate(context: ExecutionContext) {
|
|
const isPublic = this.reflector.getAllAndOverride<boolean>(IS_PUBLIC_KEY, [
|
|
context.getHandler(),
|
|
context.getClass(),
|
|
]);
|
|
|
|
if (isPublic) {
|
|
return true;
|
|
}
|
|
|
|
await super.canActivate(context);
|
|
|
|
const token = context.switchToHttp().getRequest().headers['authorization']?.split(' ')[1];
|
|
|
|
const isRevoked = await this.cacheService.get(token);
|
|
|
|
if (isRevoked) {
|
|
throw new UnauthorizedException();
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|