-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #123 from TripInfoWeb/dev_auth
Dev auth
- Loading branch information
Showing
5 changed files
with
91 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { NextRequest, NextResponse } from "next/server"; | ||
|
||
export async function GET(request: NextRequest) { | ||
try { | ||
const cookie = request.cookies.get("refresh_token"); | ||
if (!cookie) { | ||
return new NextResponse("Access token not found", { status: 401 }); | ||
} | ||
// 액세스 토큰 재요청 | ||
const backendResponse = await fetch( | ||
`${process.env.BACKEND_URL}/api/auth/oauth2/token/refresh`, | ||
{ | ||
method: "POST", | ||
headers: { | ||
Cookie: `${cookie?.name}=${cookie?.value}`, | ||
"Content-Type": "application/json", | ||
"Access-Control-Allow-Origin": "*", | ||
}, | ||
credentials: "include", | ||
}, | ||
) | ||
const result = new NextResponse(backendResponse.status == 200 ? "성공" : "실패", { | ||
status: backendResponse.status, | ||
headers: { "Content-Type": "application/json" }, | ||
}); | ||
if (backendResponse.status == 200) { | ||
const accessToken = backendResponse.headers.get("set-cookie"); | ||
result.headers.set("set-cookie", accessToken as string); | ||
} | ||
return result; | ||
} catch (error) { | ||
console.error(error); | ||
return new NextResponse("살려줘" , { status: 500 }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
|
||
export async function fetchWithAuth(url: string, options = {}, retries = 1) { | ||
|
||
try { | ||
const response = await fetch(url, options); | ||
if (response.status === 401 && retries > 0) { | ||
// 토큰 갱신 | ||
const data = await fetch( | ||
`/api/auth/refresh-access-token`, | ||
); | ||
if (data.status == 401) { | ||
return Promise.reject({ | ||
status: 401, | ||
message: "실패", | ||
}); | ||
} | ||
// TODO: 추가적인 에러처리 필요 | ||
return await fetchWithAuth(url, options, 0); // 요청 재시도 | ||
} | ||
return response; | ||
} catch (error) { | ||
throw error; | ||
} | ||
} |