-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[#61] refreshToken 보내면 새로운 token 들 반환 #62
Merged
Merged
Changes from 7 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
a05bf42
Feat : apple 로그인 과정에서 token expire 에러 처리
namewhat99 66bac6f
Feat : refreshToken 발급 로직 작성
namewhat99 90f6545
Feat : controller 작성
namewhat99 c07bc49
Feat : module 작성
namewhat99 fd26565
Feat : 중복 제거
namewhat99 7292a0c
Fix: 분기문 삭제
namewhat99 aaf80b0
Fix: url 매칭 설정
namewhat99 7b50b55
Fix: 폴더 구조 변경
namewhat99 1bab13c
Fix: app 모듈 변경
namewhat99 2b7938e
Fix: accessToken expiresIn 추가
namewhat99 d69e9fb
Fix: accessToken option 삭제
namewhat99 45a4059
Merge branch 'develop' into feature/jwtRefresh-#61
koomin1227 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,12 @@ | ||
import { Body, Controller, Post } from '@nestjs/common'; | ||
import { LoginService } from './login.service'; | ||
|
||
@Controller('/api') | ||
export class LoginController { | ||
constructor(private loginService: LoginService) {} | ||
|
||
@Post('login/refresh') | ||
public refreshTokens(@Body('refreshToken') refreshToken: string) { | ||
return this.loginService.sendTokens(refreshToken); | ||
} | ||
} |
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,13 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { LoginController } from './login.controller'; | ||
import { LoginService } from './login.service'; | ||
import { JwtModule } from '@nestjs/jwt'; | ||
import { JwtConfig } from '../Config/jwt.config'; | ||
import { JwtStrategy } from '../auth/strategy/jwt.strategy'; | ||
|
||
@Module({ | ||
imports: [JwtModule.registerAsync({ useClass: JwtConfig })], | ||
controllers: [LoginController], | ||
providers: [LoginService, JwtStrategy], | ||
}) | ||
export class LoginModule {} | ||
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,56 @@ | ||
import { Injectable, UnauthorizedException } from '@nestjs/common'; | ||
import { JwtService } from '@nestjs/jwt'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import { TokenExpiredError } from 'jsonwebtoken'; | ||
|
||
@Injectable() | ||
export class LoginService { | ||
constructor( | ||
private jwtService: JwtService, | ||
private configService: ConfigService, | ||
) {} | ||
|
||
public sendTokens(refreshToken: string) { | ||
const payload = this.validateRefreshToken(refreshToken); | ||
|
||
return { | ||
accessToken: this.generateAccessToken(payload.userId), | ||
refreshToken: this.generateRefreshToken(payload.userId), | ||
}; | ||
} | ||
|
||
public generateAccessToken(userId: string): string { | ||
return this.jwtService.sign( | ||
{ | ||
userId: userId, | ||
}, | ||
{ | ||
secret: this.configService.get('JWT_ACCESS_SECRET'), | ||
expiresIn: this.configService.get('JWT_ACCESS_EXPIRES_IN'), | ||
}, | ||
); | ||
} | ||
|
||
public generateRefreshToken(userId: string): string { | ||
return this.jwtService.sign( | ||
{ | ||
userId: userId, | ||
}, | ||
{ | ||
secret: this.configService.get('JWT_REFRESH_SECRET'), | ||
expiresIn: this.configService.get('JWT_REFRESH_EXPIRES_IN'), | ||
}, | ||
); | ||
} | ||
public validateRefreshToken(refreshToken: string) { | ||
try { | ||
return this.jwtService.verify(refreshToken, { | ||
secret: this.configService.get('JWT_REFRESH_SECRET'), | ||
}); | ||
} catch (e) { | ||
if (e instanceof TokenExpiredError) { | ||
throw new UnauthorizedException(); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
auth module에 합치는게 더 낫지 않을까 생각됩니다. 중복되는 코드도 많고 그래서요