-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b9cd203
commit ea0df01
Showing
9 changed files
with
151 additions
and
158 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 |
---|---|---|
@@ -1,96 +1,71 @@ | ||
import axios from 'axios'; | ||
import { HTTP_STATUS_CODE } from '../constants/api'; | ||
import { getCookie, removeCookie, setCookie } from '@hooks/sign-in/useSignIn'; | ||
import { REFRESH_API } from './refresh'; | ||
import { message } from 'antd'; | ||
import { TextBox } from '@components/text-box'; | ||
import { ROUTES } from '@/constants/routes'; | ||
import { REFRESH_API } from './refresh'; | ||
import { isAccessTokenExpired } from '@/utils/refresh'; | ||
|
||
export const instance = axios.create({ | ||
// baseURL: '', | ||
baseURL: process.env.REACT_APP_SERVER_URL, | ||
timeout: 5000, | ||
headers: { | ||
'Content-Type': 'application/json', | ||
timeout: 5000, | ||
}, | ||
}); | ||
|
||
const handleUnauthorized = () => { | ||
removeCookie('accessToken'); | ||
removeCookie('refreshToken'); | ||
localStorage.clear(); | ||
message.error('로그인 만료 입니다.'); | ||
window.location.replace('/signin'); | ||
}; | ||
|
||
instance.interceptors.request.use( | ||
(config) => { | ||
async (config) => { | ||
const accessToken = getCookie('accessToken'); | ||
if (accessToken) { | ||
config.headers['Authorization'] = `Bearer ${accessToken}`; | ||
const isTokenExpired = isAccessTokenExpired(accessToken); | ||
if (isTokenExpired) { | ||
try { | ||
const res = await REFRESH_API.postRefresh({ | ||
accessToken: accessToken, | ||
refreshToken: getCookie('refreshToken') as string, | ||
}); | ||
config.headers['Authorization'] = `Bearer ${res.data.accessToken}`; | ||
setCookie('accessToken', res.data.accessToken); | ||
setCookie('refreshToken', res.data.refreshToken); | ||
} catch (refreshError) { | ||
console.log('재발급 실패'); | ||
} | ||
} else { | ||
config.headers['Authorization'] = `Bearer ${accessToken}`; | ||
} | ||
} | ||
return config; | ||
}, | ||
(error) => { | ||
// 여기 뺐습니다. | ||
return Promise.reject(error); | ||
}, | ||
); | ||
|
||
instance.interceptors.response.use( | ||
(response) => { | ||
return response; | ||
}, | ||
async (error) => { | ||
const accessToken = getCookie('accessToken'); | ||
const refreshToken = getCookie('refreshToken'); | ||
if (error.response.status === HTTP_STATUS_CODE.NOTFOUND) { | ||
console.log('404페이지로 이동'); | ||
} else if ( | ||
(response) => response, | ||
(error) => { | ||
if ( | ||
error.response?.status === HTTP_STATUS_CODE.UNAUTHORIZED && | ||
window.location.pathname !== ROUTES.SIGNIN && | ||
window.location.pathname !== ROUTES.SIGNUP && | ||
window.location.pathname !== ROUTES.SIGNIN_AGREEMENT && | ||
window.location.pathname !== ROUTES.SIGNUP_SUCCESS && | ||
error.response.status === HTTP_STATUS_CODE.UNAUTHORIZED && | ||
accessToken | ||
) { | ||
try { | ||
const response = await REFRESH_API.postRefresh({ | ||
accessToken: accessToken, | ||
refreshToken: refreshToken as string, | ||
}); | ||
removeCookie('accessToken'); | ||
removeCookie('refreshToken'); | ||
removeCookie('accommodationId'); | ||
const newAccessToken = response.data.data.accessToken; | ||
const newRefreshToken = response.data.data.refreshToken; | ||
setCookie('accessToken', newAccessToken); | ||
setCookie('refreshToken', newRefreshToken); | ||
} catch (error) { | ||
removeCookie('accessToken'); | ||
removeCookie('refreshToken'); | ||
removeCookie('accommodationId'); | ||
message.error({ | ||
content: ( | ||
<TextBox typography="body3" fontWeight={'400'}> | ||
로그인 만료입니다. | ||
</TextBox> | ||
), | ||
duration: 2, | ||
}); | ||
setTimeout(() => { | ||
window.location.href = ROUTES.SIGNIN; | ||
}, 1000); | ||
} | ||
return axios(error.config); | ||
} else if ( | ||
!accessToken && | ||
window.location.pathname !== ROUTES.SIGNIN && | ||
window.location.pathname !== ROUTES.SIGNUP && | ||
window.location.pathname !== ROUTES.SIGNIN_AGREEMENT && | ||
window.location.pathname !== ROUTES.SIGNUP_SUCCESS | ||
) { | ||
removeCookie('accessToken'); | ||
removeCookie('refreshToken'); | ||
removeCookie('accommodationId'); | ||
localStorage.clear(); | ||
setTimeout(() => { | ||
window.location.href = ROUTES.SIGNIN; | ||
}, 1000); | ||
handleUnauthorized(); | ||
} else if (error.response?.status === HTTP_STATUS_CODE.NOTFOUND) { | ||
console.log('여기 404에러 핸들링 필요해요~'); | ||
} | ||
return Promise.reject(error); | ||
}, | ||
); | ||
|
||
export default instance; |
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,8 +1,7 @@ | ||
import { instance } from '..'; | ||
import { PostRefreshData, RefreshData } from './type'; | ||
import { Response } from '@/types/api'; | ||
|
||
export const REFRESH_API = { | ||
postRefresh: (data: RefreshData) => | ||
instance.post<Response<PostRefreshData>>('/api/auth/refresh', data), | ||
instance.post<PostRefreshData>('/api/auth/refresh', data), | ||
}; |
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,12 +1,9 @@ | ||
{ | ||
"message": "성공적으로 로그인 했습니다.", | ||
"data": { | ||
"accessToken": "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJ0ZXN0MTIzNDVAbmF2ZXIuY29tIiwiaWF0IjoxNzAxMzMwOTE0LCJleHAiOjE3MDE0MTczMTR9._G-tEQZpJdCgT_Uo4ipDByBwjP-0HclSq35G3MLvNvIiPILbT0Y9MZR19gTWDbzLuU0Gp1phFMbSGNYuAD1cSg", | ||
"refreshToken": "eyJhbGciOiJIUzUxMiJ9.eyJpYXQiOjE3MDEzMzA5MTQsImV4cCI6MTcwMTkzNTcxNH0.8BZt06CkTEWx7FyT4OH4c7ZNNplz9un2TCmyxf-nM5aCJe-MXJVdJAYnHW00j3ZytR_aYWp-R78tBSSF3pfAkw", | ||
"memberResponse": { | ||
"id": 1, | ||
"email": "[email protected]", | ||
"name": "홍길동" | ||
} | ||
"accessToken": "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJ0ZXN0MTIzNDVAbmF2ZXIuY29tIiwiaWF0IjoxNzAxMzMwOTE0LCJleHAiOjE3MDE0MTczMTR9._G-tEQZpJdCgT_Uo4ipDByBwjP-0HclSq35G3MLvNvIiPILbT0Y9MZR19gTWDbzLuU0Gp1phFMbSGNYuAD1cSg", | ||
"refreshToken": "eyJhbGciOiJIUzUxMiJ9.eyJpYXQiOjE3MDEzMzA5MTQsImV4cCI6MTcwMTkzNTcxNH0.8BZt06CkTEWx7FyT4OH4c7ZNNplz9un2TCmyxf-nM5aCJe-MXJVdJAYnHW00j3ZytR_aYWp-R78tBSSF3pfAkw", | ||
"memberResponse": { | ||
"id": 1, | ||
"email": "[email protected]", | ||
"name": "홍길동" | ||
} | ||
} |
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
Oops, something went wrong.