From a445f95088d924b1dd8c2918547024290c9d1b0c Mon Sep 17 00:00:00 2001 From: Joonsoo Kim Date: Thu, 7 Dec 2023 19:08:32 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20CORS=20=EC=97=90=EB=9F=AC=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - NGINX 요청 시 CORS 에러를 수정 --- BE/src/auth/auth.controller.ts | 21 +++++++++++++++------ FE/src/App.js | 14 ++++++++++++++ 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/BE/src/auth/auth.controller.ts b/BE/src/auth/auth.controller.ts index 5273780..7e96fa0 100644 --- a/BE/src/auth/auth.controller.ts +++ b/BE/src/auth/auth.controller.ts @@ -4,6 +4,7 @@ import { Get, HttpCode, Post, + Redirect, Req, UseGuards, } from "@nestjs/common"; @@ -26,22 +27,30 @@ export class AuthController { @Get("/kakao/callback") @UseGuards(AuthGuard("kakao")) - @HttpCode(201) + @Redirect(process.env.FRONTEND_URL, 302) async kakaoSignIn( @GetUser() user: User, @Req() request: Request, - ): Promise { - return await this.authService.kakaoSignIn(user, request); + ): Promise { + const loginResponseDto: LoginResponseDto = + await this.authService.kakaoSignIn(user, request); + return { + url: `${process.env.FRONTEND_URL}?access-token=${loginResponseDto.accessToken}&nickname=${loginResponseDto.nickname}&premium=${loginResponseDto.premium}`, + }; } @Get("/naver/callback") @UseGuards(AuthGuard("naver")) - @HttpCode(201) + @Redirect(process.env.FRONTEND_URL, 302) async naverSignIn( @GetUser() user: User, @Req() request: Request, - ): Promise { - return await this.authService.naverSignIn(user, request); + ): Promise { + const loginResponseDto: LoginResponseDto = + await this.authService.naverSignIn(user, request); + return { + url: `${process.env.FRONTEND_URL}?access-token=${loginResponseDto.accessToken}&nickname=${loginResponseDto.nickname}&premium=${loginResponseDto.premium}`, + }; } @Post("/signup") diff --git a/FE/src/App.js b/FE/src/App.js index 4142c65..c135988 100644 --- a/FE/src/App.js +++ b/FE/src/App.js @@ -55,6 +55,20 @@ function App() { }, []); useEffect(() => { + if (window.location.search) { + const params = new URLSearchParams(window.location.search); + + const accessToken = params.get("access-token"); + const nickname = params.get("nickname"); + + if (accessToken && nickname) { + setUserState({ ...userState, isLogin: true, accessToken, nickname }); + sessionStorage.setItem("accessToken", accessToken); + sessionStorage.setItem("nickname", nickname); + window.history.replaceState({}, "", "/"); + } + } + window.onpopstate = (event) => { if (event.state) { setDiaryState(event.state);