Skip to content

Commit

Permalink
Merge pull request #134 from TripInfoWeb/dev_auth
Browse files Browse the repository at this point in the history
로그인 부분 에러 처리 수정
  • Loading branch information
ssssksss authored Jul 25, 2024
2 parents 38c8b48 + 04bb886 commit 2b19815
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/app/api/auth/refresh-access-token/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export async function POST(request: NextRequest) {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
cache: "no-store",
credentials: "include",
},
);
Expand All @@ -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 });
}
}
18 changes: 7 additions & 11 deletions src/app/api/auth/user/route.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
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 });
}
// 리프레시 토큰으로 재발급 받아 재요청 보내기 위한 응답
return new NextResponse("Refresh token not found", { status: 401 });
}

// 사용자 정보 조회 API
const response = await fetch(`${process.env.BACKEND_URL}/api/users/info`, {
method: "GET",
Expand All @@ -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,
});
}
5 changes: 3 additions & 2 deletions src/utils/fetchWithAuth.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down

0 comments on commit 2b19815

Please sign in to comment.