Skip to content

Commit

Permalink
Merge pull request #132 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 4079f7d + fd4da99 commit 163264c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
24 changes: 15 additions & 9 deletions src/app/api/auth/user/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ export async function GET(request: NextRequest) {
if (!access_cookie) {
const refresh_cookie = request.cookies.get("refresh_token");
if (!refresh_cookie) {
return new NextResponse("불필요한 요청", { status: 400 });
// 리프레시 토큰이 없으므로 요청 중단
return new NextResponse("Refresh token not found", { status: 403 });
}
return new NextResponse("Access token not found", { status: 401 });
// 리프레시 토큰으로 재발급 받아 재요청 보내기 위한 응답
return new NextResponse("Refresh token not found", { status: 401 });
}
// 사용자 정보 조회 API
const response = await fetch(`${process.env.BACKEND_URL}/api/users/info`, {
Expand All @@ -18,14 +20,18 @@ export async function GET(request: NextRequest) {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
cache: "no-store",
});

const data = await response.json();
return new NextResponse(JSON.stringify(data), {
status: 200,
headers: { "Content-Type": "application/json" },
});
if (response.status == 200) {
const data = await response.json();
return new NextResponse(JSON.stringify(data), {
status: 200,
headers: { "Content-Type": "application/json" },
});
} else {
return new Error("Internal Server Error");
}
} catch (error) {
return new NextResponse("Internal Server Error", { status: 500 });
return new Error("Internal Server Error");
}
}
5 changes: 2 additions & 3 deletions src/utils/fetchWithAuth.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
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`, {
method: "POST",
});
const data = await fetch(`/api/auth/refresh-access-token`);
if (data.status == 401) {
return Promise.reject({
status: 401,
Expand Down

0 comments on commit 163264c

Please sign in to comment.