diff --git a/src/app/api/auth/refresh-access-token/route.ts b/src/app/api/auth/refresh-access-token/route.ts index c313ee60..3f008c98 100644 --- a/src/app/api/auth/refresh-access-token/route.ts +++ b/src/app/api/auth/refresh-access-token/route.ts @@ -16,6 +16,7 @@ export async function POST(request: NextRequest) { "Content-Type": "application/json", "Access-Control-Allow-Origin": "*", }, + cache: "no-store", credentials: "include", }, ); @@ -32,7 +33,6 @@ export async function POST(request: NextRequest) { } return result; } catch (error) { - console.error(error); - return new NextResponse("살려줘", { status: 500 }); + return new NextResponse("서버 에러", { status: 500 }); } } diff --git a/src/app/api/auth/user/route.ts b/src/app/api/auth/user/route.ts index aff4c0b4..b198d3d6 100644 --- a/src/app/api/auth/user/route.ts +++ b/src/app/api/auth/user/route.ts @@ -1,10 +1,9 @@ import { NextRequest, NextResponse } from "next/server"; export async function GET(request: NextRequest) { - try { const access_cookie = request.cookies.get("access_token"); if (!access_cookie) { - const refresh_cookie = request.cookies.get("refresh_token"); + const refresh_cookie = request.cookies.get("refresh_token"); if (!refresh_cookie) { // 리프레시 토큰이 없으므로 요청 중단 return new NextResponse("Refresh token not found", { status: 403 }); @@ -12,7 +11,7 @@ export async function GET(request: NextRequest) { // 리프레시 토큰으로 재발급 받아 재요청 보내기 위한 응답 return new NextResponse("Refresh token not found", { status: 401 }); } - + // 사용자 정보 조회 API const response = await fetch(`${process.env.BACKEND_URL}/api/users/info`, { method: "GET", @@ -23,17 +22,14 @@ export async function GET(request: NextRequest) { }, cache: "no-store", }); - + if (response.status === 200) { const data = await response.json(); return new NextResponse(JSON.stringify(data), { status: 200, - headers: { "Content-Type": "application/json" }, }); - } else { - return NextResponse.error(); - } - } catch (error) { - return NextResponse.error(); - } + } + return new NextResponse("서버 에러", { + status: 500, + }); } diff --git a/src/utils/fetchWithAuth.ts b/src/utils/fetchWithAuth.ts index 855de044..e88756ca 100644 --- a/src/utils/fetchWithAuth.ts +++ b/src/utils/fetchWithAuth.ts @@ -1,10 +1,11 @@ export async function fetchWithAuth(url: string, options = {}, retries = 1) { try { const response = await fetch(url, options); - // 사용하는 api에서 return new NextResponse("Refresh token not found", {status: 401}); 처럼 response값에 401값을 담아주어야 한다. if (response.status === 401 && retries > 0) { // 토큰 갱신 - const data = await fetch(`/api/auth/refresh-access-token`); + const data = await fetch(`/api/auth/refresh-access-token`, { + method: "POST", + }); if (data.status == 401) { return Promise.reject({ status: 401,