-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add refresh token guard and strategy #18
- Loading branch information
Showing
2 changed files
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |