/
coffe
/
api
Обзор
Документация
Войти
/
coffe
/
api
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
main
src/auth/guards/rt.guard.ts
56 строк
1 KB
vwency
init
08 май 2026, 01:38
08 май 2026, 01:38
47ccfb3
Код
Авторство
О чём код?
import { CanActivate, ExecutionContext, Injectable, UnauthorizedException, } from '@nestjs/common' import { JwtService } from '@nestjs/jwt' import { Request } from 'express' import { ConfigService } from '@nestjs/config' import { Reflector } from '@nestjs/core' import { IS_PUBLIC_KEY } from '../decorators/public.decorator' @Injectable() export class RtGuard implements CanActivate { constructor( private jwtService: JwtService, private configService: ConfigService, private reflector: Reflector, ) {} async canActivate(context: ExecutionContext): Promise<boolean> { const isPublic = this.reflector.getAllAndOverride<boolean>(IS_PUBLIC_KEY, [ context.getHandler(), context.getClass(), ]) if (isPublic) { return true } const request = context.switchToHttp().getRequest() const token = this.extractTokenFromHeader(request) if (!token) { throw new UnauthorizedException('Refresh token not found') } try { const payload = await this.jwtService.verifyAsync(token, { secret: this.configService.get<string>('JWT_REFRESH_SECRET'), }) request.user = payload request.refreshToken = token } catch (error) { throw new UnauthorizedException('Invalid refresh token') } return true } private extractTokenFromHeader(request: Request): string | undefined { const [type, token] = request.headers.authorization?.split(' ') ?? [] return type === 'Bearer' ? token : undefined } }