Skip to content

Commit

Permalink
hotfix :: refreshToken error
Browse files Browse the repository at this point in the history
  • Loading branch information
ftery0 committed Dec 12, 2024
1 parent 8626c82 commit 106f8f5
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions src/lib/axios/errorResponseHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ import authRepository from "@src/repository/auth/auth.repository";
//리프레쉬 작업중인지 아닌지를 구분하는 변수
let isRefreshing = false;

let refreshSubscribers: ((accessToken: string) => void)[] = [];

const onTokenRefreshed = (accessToken: string) => {
refreshSubscribers.map((callback) => callback(accessToken));
};

const addRefreshSubscriber = (callback: (accessToken: string) => void) => {
refreshSubscribers.push(callback);
};


const errorResponseHandler = async (error: AxiosError) => {
if (error.response) {
Expand All @@ -23,7 +33,7 @@ const errorResponseHandler = async (error: AxiosError) => {
const usingRefreshToken = token.getToken(REFRESH_TOKEN_KEY);

if (
usingAccessToken !==undefined &&
usingAccessToken !== undefined &&
usingRefreshToken !== undefined &&
status === 401
) {
Expand All @@ -49,21 +59,22 @@ const errorResponseHandler = async (error: AxiosError) => {
isRefreshing = false;

//새로 받은 accessToken을 기반으로 이때까지 밀려있던 요청을 모두 처리

onTokenRefreshed(newAccessToken);
} catch (error) {
//리프레쉬 하다가 오류난거면 리프레쉬도 만료된 것이므로 다시 로그인
window.alert("세션이 만료되었습니다.");
token.clearToken();
window.location.href = "/sign";
}
}



return new Promise((resolve) => {
addRefreshSubscriber((accessToken: string) => {
originalRequest.headers![REQUEST_TOKEN_KEY] = `Bearer ${accessToken}`;
resolve(dodamAxios(originalRequest));
});
});
}
}

return Promise.reject(error);
};

export default errorResponseHandler;

0 comments on commit 106f8f5

Please sign in to comment.