Skip to content

Commit

Permalink
HotFix refresh Token
Browse files Browse the repository at this point in the history
Origin/hotfix/#98 refresh token
  • Loading branch information
ftery0 authored Dec 18, 2024
2 parents 1e27dbf + 72e9bab commit 0de28e6
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 9 deletions.
10 changes: 10 additions & 0 deletions src/hooks/leave/useApplyLeave.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,16 @@ const useApplyLeave = () => {
showToast("외박신청은 최대 4개까지 가능해요!", "INFO");
return;
}

if(/^[^a-zA-Z0-9-]+$/.test(reason)){
showToast("특수문자만으로 사유를 작성할 수 없습니다!", "INFO");
return;
}

if (!reason || reason.replace(/\s+/g, "").length <= 5){
showToast("사유의 길이를 5자 이상으로 적어주세요!", "INFO")
return;
}

if (reason?.length > 50) {
showToast("사유의 길이를 50자 이내로 적어주세요!", "INFO");
Expand Down
6 changes: 5 additions & 1 deletion src/hooks/pass/useApplyPass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,11 @@ const useApplyPass = () => {
showToast("복귀시간이 출발시간보다 빨라요!", "INFO");
return;
}


if (!reason || reason.replace(/\s+/g, "").length <= 5) {
showToast("사유의 길이를 5자 이상로 적어주세요!", "INFO");
return;
}
if (reason?.length > 50) {
showToast("사유의 길이를 50자 이내로 적어주세요!", "INFO");
return;
Expand Down
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;
2 changes: 1 addition & 1 deletion src/repository/auth/auth.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class AuthRepository {
refreshToken: string;
}): Promise<NewAccessTokenResponse> {
const { data } = await axios.post<NewAccessTokenResponse>(
`${config}/auth/reissue`,
`${config.DODAM_SERVER}/auth/reissue`,
refreshToken
);
return data;
Expand Down

0 comments on commit 0de28e6

Please sign in to comment.