-
Notifications
You must be signed in to change notification settings - Fork 1
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
[BE] 2.04 oAuth를 활용한 로그인 API 구현 #7 #43
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
5d5318b
➕ add : express 모듈의 Request 타입 선언 재정의(#7)
jinddings 0a276f9
⚙️ chore : typeOrmModule이 모든 entity 포함하도록 변경(#7)
jinddings 4357a54
✨ feat : refresh Token 구현(#7)
jinddings a9c95d7
✨ feat : Token Refresh 요청 API 구현(#7)
jinddings 91607bd
➕ add : kakao strategy 추가(#4)
jinddings 0f58a72
Merge branch 'back/main' into feature/api/login-#7
jinddings 1fe2082
🔧 fix : merge confict 수정
jinddings b872066
♻️ refactor: lint 에 위배되는 코드 수정
jinddings 50b9170
Merge branch 'back/main' of https://github.com/boostcampwm-2024/web16…
jinddings 7f20998
🔧 fix : refreshToken, accessToken response 방식 수정
jinddings e1dda3f
🔧 fix : jwt secret .env에서 읽도록 수정, refresh API 응답 방식 수정
jinddings 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 |
---|---|---|
|
@@ -3,18 +3,25 @@ import { | |
Post, | ||
Get, | ||
Body, | ||
Req, | ||
ValidationPipe, | ||
UseGuards, | ||
Req, | ||
Res, | ||
UnauthorizedException, | ||
} from '@nestjs/common'; | ||
import { AuthGuard } from '@nestjs/passport'; | ||
import { ApiOperation } from '@nestjs/swagger'; | ||
import { Request, Response } from 'express'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import { AuthService } from './auth.service'; | ||
import { AuthCredentialsDto } from './dto/auth-credentials.dto'; | ||
|
||
@Controller('auth') | ||
export class AuthController { | ||
constructor(private authService: AuthService) {} | ||
constructor( | ||
private authService: AuthService, | ||
private configService: ConfigService, | ||
) {} | ||
Comment on lines
+21
to
+24
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟢 요런 아이들 private readonly로 통일하는게 좋지 않을까용?? |
||
|
||
@ApiOperation({ summary: '회원 가입 API' }) | ||
@Post('/signup') | ||
|
@@ -24,16 +31,56 @@ export class AuthController { | |
|
||
@ApiOperation({ summary: '로그인 API' }) | ||
@Post('/login') | ||
loginWithCredentials( | ||
async loginWithCredentials( | ||
@Body(ValidationPipe) authCredentialsDto: AuthCredentialsDto, | ||
@Res() res: Response, | ||
) { | ||
return this.authService.loginUser(authCredentialsDto); | ||
const { accessToken, refreshToken } = | ||
await this.authService.loginUser(authCredentialsDto); | ||
|
||
res.cookie('refreshToken', refreshToken, { httpOnly: true }); | ||
res.cookie('isRefreshToken', true, { httpOnly: true }); | ||
return res.status(200).json({ accessToken }); | ||
} | ||
|
||
@ApiOperation({ summary: 'Token 인증 테스트 API' }) | ||
@Get('/test') | ||
@UseGuards(AuthGuard()) | ||
@UseGuards(AuthGuard('jwt')) | ||
test(@Req() req: Request) { | ||
return req; | ||
} | ||
|
||
@ApiOperation({ summary: 'Kakao 로그인 API' }) | ||
@Get('/kakao') | ||
@UseGuards(AuthGuard('kakao')) | ||
async kakaoLogin( | ||
@Body() authCredentialsDto: AuthCredentialsDto, | ||
@Res() res: Response, | ||
) { | ||
const { accessToken, refreshToken } = | ||
await this.authService.kakaoLoginUser(authCredentialsDto); | ||
|
||
res.cookie('refreshToken', refreshToken, { httpOnly: true }); | ||
res.cookie('isRefreshToken', true, { httpOnly: true }); | ||
return res.status(200).json({ accessToken }); | ||
} | ||
|
||
@ApiOperation({ summary: 'Refresh Token 요청 API' }) | ||
@Get('/refresh') | ||
async refresh(@Req() req: Request, @Res() res: Response) { | ||
if ( | ||
typeof req.cookies.refreshToken !== 'string' || | ||
typeof req.cookies.accessToken !== 'string' | ||
) { | ||
throw new UnauthorizedException('Invalid refresh token'); | ||
} | ||
|
||
const { refreshToken } = req.cookies; | ||
|
||
const newAccessToken = await this.authService.refreshToken(refreshToken); | ||
|
||
res.cookie('refreshToken', refreshToken, { httpOnly: true }); | ||
res.cookie('isRefreshToken', true, { httpOnly: true }); | ||
return res.status(200).json({ accessToken: newAccessToken }); | ||
} | ||
} |
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
Oops, something went wrong.
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.
🟢 코드를 정리하니 보기 좋네요~