Skip to content

Commit

Permalink
feat: wip - validate refresh token
Browse files Browse the repository at this point in the history
add refresh token guard and strategy

#18
  • Loading branch information
seo-wo committed Dec 5, 2023
1 parent 732ea74 commit af60022
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/auth/guard/jwt-refresh.guard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Injectable } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';

@Injectable()
export class JwtRefreshGuard extends AuthGuard('jwt-refresh') {}
38 changes: 38 additions & 0 deletions src/auth/strategy/jwt-refresh.strategy.ts
Original file line number Diff line number Diff line change
@@ -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<string>('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;
}
}

0 comments on commit af60022

Please sign in to comment.