-
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.
Feat: interceptor 구현 및 퍼블리싱 수정 (#177)
- Loading branch information
1 parent
642915d
commit 2908443
Showing
8 changed files
with
129 additions
and
13 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,85 @@ | ||
import axios from 'axios'; | ||
import { HTTP_BASE_URL, HTTP_STATUS_CODE } from '../constants/api'; | ||
import { getCookie, removeCookie, setCookie } from '@hooks/sign-in/useSignIn'; | ||
import { message } from 'antd'; | ||
import { TextBox } from '@components/text-box'; | ||
import { ROUTES } from '@/constants/routes'; | ||
|
||
export const instance = axios.create({ | ||
baseURL: HTTP_BASE_URL, | ||
headers: { | ||
'Content-Type': 'application/json', | ||
timeout: 5000, | ||
}, | ||
}); | ||
|
||
instance.interceptors.request.use( | ||
(config) => { | ||
const accessToken = getCookie('accessToken'); | ||
if (accessToken) { | ||
config.headers['Authorization'] = `Bearer ${accessToken}`; | ||
} | ||
return config; | ||
}, | ||
(error) => { | ||
return Promise.reject(error); | ||
}, | ||
); | ||
|
||
instance.interceptors.response.use( | ||
(response) => { | ||
if (response.status === HTTP_STATUS_CODE.NOTFOUND) { | ||
// 콘솔 지우고 바로 404 페이지로 넘어가게 할 예정 | ||
console.log('404 페이지로 넘어가야 함!'); | ||
} | ||
return response; | ||
}, | ||
async (error) => { | ||
const accessToken = getCookie('accessToken'); | ||
if (window.location.pathname !== ROUTES.SIGNIN && !accessToken) { | ||
message.error({ | ||
content: ( | ||
<TextBox typography="body3" fontWeight={'400'}> | ||
accessToken이 없습니다. | ||
</TextBox> | ||
), | ||
duration: 2, | ||
}); | ||
removeCookie('accessToken'); | ||
removeCookie('refreshToken'); | ||
removeCookie('accommodationId'); | ||
setTimeout(() => { | ||
window.location.href = ROUTES.SIGNIN; | ||
}, 1000); | ||
return Promise.reject(error); | ||
} else if ( | ||
window.location.pathname !== ROUTES.SIGNIN && | ||
error.response.status === HTTP_STATUS_CODE.UNAUTHORIZED | ||
) { | ||
try { | ||
// 여기에 재발급 api 선언, 아래는 예시 | ||
const newAccessToken = 'ivegaeul'; | ||
setCookie('accessToken', newAccessToken); | ||
return axios(error.config); | ||
} catch (refreshError) { | ||
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 Promise.reject(refreshError); | ||
} | ||
} | ||
}, | ||
); | ||
|
||
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
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 |
---|---|---|
|
@@ -32,7 +32,7 @@ const email = '[email protected]'; | |
const verificationCode = '020924'; | ||
const accommodationId = 1; | ||
export const handlers = [ | ||
http.post('/api/auth/owner/signin', postSignInResolver), | ||
http.post('/api/auth/owners/signin', postSignInResolver), | ||
http.post('/api/auth/owners/signup', postSignUpResolver), | ||
http.post('/api/auth/owners/request-email', postAuthenticationResolver), | ||
http.get( | ||
|
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
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