From fafaea6f12c8473c0be0d168add55dea4bcf99c2 Mon Sep 17 00:00:00 2001 From: "SK\\ssssk" Date: Mon, 29 Jul 2024 09:31:53 +0900 Subject: [PATCH] =?UTF-8?q?=EC=BF=A0=ED=82=A4=EA=B0=80=20=EC=A0=80?= =?UTF-8?q?=EC=9E=A5=EC=9D=B4=20=EB=90=98=EC=A7=80=20=EC=95=8A=EB=8A=94=20?= =?UTF-8?q?=EB=AC=B8=EC=A0=9C=20SSR=EB=A1=9C=20=EB=B3=80=EA=B2=BD=ED=95=B4?= =?UTF-8?q?=EC=84=9C=20=EB=90=98=EB=8A=94=EC=A7=80=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app/api/auth/kakao/getToken/route.ts | 43 ++++++++++++++++++++++ src/containers/auth/AuthKaKaoContainer.tsx | 23 ++++++------ 2 files changed, 55 insertions(+), 11 deletions(-) create mode 100644 src/app/api/auth/kakao/getToken/route.ts diff --git a/src/app/api/auth/kakao/getToken/route.ts b/src/app/api/auth/kakao/getToken/route.ts new file mode 100644 index 00000000..4471db1b --- /dev/null +++ b/src/app/api/auth/kakao/getToken/route.ts @@ -0,0 +1,43 @@ +import { NextRequest, NextResponse } from "next/server"; + +export async function GET(request: NextRequest) { + try { + // Query string 파싱 + const url = new URL(request.url); + const code = url.searchParams.get("code"); + + // 백엔드에 액세스 토큰 재요청 + const backendResponse = await fetch( + `${process.env.NEXT_PUBLIC_BACKEND_URL}/api/auth/oauth2/login?type=kakao&redirectUrl=${process.env.NEXT_PUBLIC_KAKAO_REDIRECT_URL}&code=${code}`, + { + method: "GET", + headers: { + "Content-Type": "application/json", + }, + credentials: "include", + }, + ); + + const result = new NextResponse( + backendResponse.status == 200 ? "성공" : "실패", + { + status: backendResponse.status, + headers: { "Content-Type": "application/json" }, + }, + ); + + if (backendResponse.status == 200) { + const cookies = backendResponse.headers.get("set-cookie"); + if (cookies) { + // 받은 쿠키를 파싱하여 설정 + cookies.split(",").forEach((cookie) => { + result.headers.append("Set-Cookie", cookie.trim()); + }); + } + } + + return result; + } catch (error) { + return new NextResponse("서버 에러", { status: 500 }); + } +} diff --git a/src/containers/auth/AuthKaKaoContainer.tsx b/src/containers/auth/AuthKaKaoContainer.tsx index fe1ccbb0..2d96dff2 100644 --- a/src/containers/auth/AuthKaKaoContainer.tsx +++ b/src/containers/auth/AuthKaKaoContainer.tsx @@ -17,28 +17,29 @@ const AuthKaKaoContainer = () => { }>(window.location.href); const kakaoLogin = async () => { try { - // 액세스와 리프레시 토큰 발급 - await fetch( - `${process.env.NEXT_PUBLIC_BACKEND_URL}/api/auth/oauth2/login?type=kakao&redirectUrl=${process.env.NEXT_PUBLIC_KAKAO_REDIRECT_URL}&code=${_queryStringObject?.code}`, + const response = await fetch( + `/api/auth/kakao/getToken?code=${_queryStringObject?.code}`, { method: "GET", headers: { "Content-Type": "application/json", - "Access-Control-Allow-Origin": "*", }, - cache: "no-store", credentials: "include", }, ); + + if (response.status !== 200) { + throw new Error("Failed to login"); + } + // 액세스 토큰을 이용해서 사용자 정보 조회 - const data = await fetch("/api/auth/user"); - if (data.status == 200) { - data.json().then((res: userResponseDto) => { - authStore.setUser(res); - }); + const userDataResponse = await fetch("/api/auth/user"); + if (userDataResponse.status == 200) { + const userData = await userDataResponse.json(); + authStore.setUser(userData as userResponseDto); router.push("/"); } else { - throw ""; + throw new Error("Failed to fetch user data"); } } catch (error) { console.error("로그인 실패", error);