Skip to content

Commit

Permalink
Merge pull request #168 from Nexters/develop
Browse files Browse the repository at this point in the history
fix: 토큰 API 인증 로직 수정 (옵션, 방어로직 추가)
  • Loading branch information
alstn2468 authored Sep 17, 2024
2 parents c2a0ac8 + 750f3e6 commit 14df24f
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions packages/api/src/fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,25 @@ interface PostRefreshTokenResponse {
}

const postRefreshToken = async () => {
const response = await ky.post(
`${API_URL}/${IS_SUPER_ADMIN ? 'sa-api' : 'web'}/papi/v1/login/refresh`,
{
json: {
refreshToken: window.localStorage.getItem(LOCAL_STORAGE.REFRESH_TOKEN),
const refreshToken = window.localStorage.getItem(LOCAL_STORAGE.REFRESH_TOKEN);

if (refreshToken) {
const response = await ky.post(
`${API_URL}/${IS_SUPER_ADMIN ? 'sa-api' : 'web'}/papi/v1/login/refresh`,
{
json: {
refreshToken,
},
},
},
);
);

return await response.json<PostRefreshTokenResponse>();
}
};

return await response.json<PostRefreshTokenResponse>();
const defaultOption: Options = {
retry: 0,
timeout: 30_000,
};

export const instance = ky.create({
Expand All @@ -45,14 +54,16 @@ export const instance = ky.create({
// access token이 만료되었을 때, refresh token으로 새로운 access token을 발급받는다.
if (!response.ok && response.status === 401 && !request.url.includes('logout')) {
try {
const { accessToken, refreshToken } = await postRefreshToken();
const { accessToken, refreshToken } = (await postRefreshToken()) ?? {};

window.localStorage.setItem(LOCAL_STORAGE.ACCESS_TOKEN, accessToken);
window.localStorage.setItem(LOCAL_STORAGE.REFRESH_TOKEN, refreshToken);
if (accessToken && refreshToken) {
window.localStorage.setItem(LOCAL_STORAGE.ACCESS_TOKEN, accessToken);
window.localStorage.setItem(LOCAL_STORAGE.REFRESH_TOKEN, refreshToken);

request.headers.set('Authorization', `Bearer ${accessToken}`);
request.headers.set('Authorization', `Bearer ${accessToken}`);

return ky(request);
return ky(request, options);
}
} catch (error) {
throw new BooltiHTTPError(response, request, options);
}
Expand All @@ -74,8 +85,7 @@ export const instance = ky.create({
},
],
},
retry: 0,
timeout: 30_000,
...defaultOption,
});

export async function resultify<T>(response: ResponsePromise) {
Expand Down

0 comments on commit 14df24f

Please sign in to comment.