forked from TEAM-BEAT/BEAT-Client
-
Notifications
You must be signed in to change notification settings - Fork 0
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 TEAM-BEAT#465 from TEAM-BEAT/refactor/TEAM-BEAT#464/…
…refresh-token Refactor/TEAM-BEAT#464/refresh token
- Loading branch information
Showing
5 changed files
with
93 additions
and
11 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
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,77 @@ | ||
import { get, instance } from "@apis/index"; | ||
import { AxiosResponse } from "axios"; | ||
import React, { useEffect } from "react"; | ||
import { useNavigate } from "react-router-dom"; | ||
import useModal from "src/hooks/useModal"; | ||
|
||
interface AuthRequiredProps { | ||
children: React.ReactNode; | ||
} | ||
|
||
const AuthRequired = ({ children }: AuthRequiredProps) => { | ||
const navigate = useNavigate(); | ||
|
||
const { openAlert } = useModal(); | ||
|
||
useEffect(() => { | ||
const user = localStorage.getItem("user"); | ||
|
||
if (!user || !JSON.parse(user)?.refreshToken) { | ||
localStorage.clear(); | ||
openAlert({ title: "다시 로그인 해주세요." }); | ||
navigate("/auth"); | ||
return; | ||
} | ||
|
||
const interceptor = instance.interceptors.response.use( | ||
(response) => response, | ||
async (error) => { | ||
const originalConfig = error.config; | ||
const status = error.response?.status; | ||
const msg = error.response?.data?.message; | ||
|
||
if (status === 401) { | ||
try { | ||
const refreshToken = JSON.parse(user)?.refreshToken; | ||
|
||
const response: AxiosResponse<{ data: { accessToken: string } }> = await get( | ||
"/users/refresh-token", | ||
{ | ||
headers: { Authorization_Refresh: refreshToken }, | ||
} | ||
); | ||
|
||
const newAccessToken = response.data?.data?.accessToken; | ||
|
||
if (newAccessToken) { | ||
localStorage.setItem("accessToken", `Bearer ${newAccessToken}`); | ||
originalConfig.headers["Authorization"] = `Bearer ${newAccessToken}`; | ||
return instance(originalConfig); // 기존 요청 재시도 | ||
} | ||
throw new Error("Failed to refresh access token"); | ||
} catch (refreshError) { | ||
console.error("Token refresh failed:", refreshError); | ||
localStorage.clear(); | ||
openAlert({ title: "장시간 미활동으로 인해 \n자동으로 로그아웃 되었습니다." }); | ||
navigate("/auth"); | ||
window.location.reload(); | ||
} | ||
} else if (status === 500) { | ||
openAlert({ | ||
title: "서버에 문제가 발생했습니다. 잠시 후 다시 시도해주세요.", | ||
}); | ||
} | ||
|
||
return Promise.reject(error); | ||
} | ||
); | ||
|
||
return () => { | ||
instance.interceptors.response.eject(interceptor); | ||
}; | ||
}, [navigate, openAlert]); | ||
|
||
return <>{children}</>; | ||
}; | ||
|
||
export default AuthRequired; |
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
export interface UserProps { | ||
nickname: string; | ||
accessToken: string; | ||
refreshToken: string; | ||
role: string; | ||
} |