diff --git a/src/auth/guard/jwt-refresh.guard.ts b/src/auth/guard/jwt-refresh.guard.ts new file mode 100644 index 0000000..ed74420 --- /dev/null +++ b/src/auth/guard/jwt-refresh.guard.ts @@ -0,0 +1,5 @@ +import { Injectable } from '@nestjs/common'; +import { AuthGuard } from '@nestjs/passport'; + +@Injectable() +export class JwtRefreshGuard extends AuthGuard('jwt-refresh') {} diff --git a/src/auth/strategy/jwt-refresh.strategy.ts b/src/auth/strategy/jwt-refresh.strategy.ts new file mode 100644 index 0000000..cb6f63a --- /dev/null +++ b/src/auth/strategy/jwt-refresh.strategy.ts @@ -0,0 +1,38 @@ +import { Injectable, UnauthorizedException } from '@nestjs/common'; +import { PassportStrategy } from '@nestjs/passport'; +import { Strategy } from 'passport-jwt'; +import { ConfigService } from '@nestjs/config'; +import { Request } from 'express'; +import { UserService } from 'src/user/user.service'; + +@Injectable() +export class JwtRefreshStrategy extends PassportStrategy( + Strategy, + 'jwt-refresh', +) { + constructor( + private readonly configService: ConfigService, + private readonly userService: UserService, + ) { + super({ + jwtFromRequest: (req) => { + return req?.cookies?.Refresh; + }, + secretOrKey: configService.get('jwt.refreshSecret'), + passReqToCallback: true, + }); + } + + async validate(req: Request, payload: any) { + const refreshToken = req?.cookies?.Refresh; + const user = await this.userService.isRefreshTokenVaild( + refreshToken, + payload.id, + ); + try { + } catch (error) { + throw new UnauthorizedException('Unauthorized', '401'); + } + return user; + } +}