-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GMMQ-79 feat: axios interceptor (#4)
* feat: baseUrl env 상수 추가, gitignore * feat: axios 인스턴스 추가 * feat: 401, 404, 5xx axios 인터셉터 추가 * Gmmq 72 feat: 라우터 설정하기 (#2) * feat: 라우터 적용을 위한 페이지 생성 * feat: 레이아웃 생성 * feat: 라우터 설정 * chore: index 파일 수정 * feat: Page 폴더 구조 정리 * feat: 라우터 설정 * chore: 사용하지 않는 파일 제거 * fix: 경로 수정 * chore: index 파일 삭제 * fix: 라우팅 변경, 파일 이름 변경 * fix: 환경변수 prefix 수정 --------- Co-authored-by: backward99 <86753969+backward99@users.noreply.github.com>
- Loading branch information
1 parent
e2d8b8c
commit b297e80
Showing
4 changed files
with
54 additions
and
0 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 |
---|---|---|
|
@@ -22,3 +22,5 @@ dist-ssr | |
*.njsproj | ||
*.sln | ||
*.sw? | ||
|
||
.env |
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,40 @@ | ||
import axios from 'axios'; | ||
import { redirect } from 'react-router-dom'; | ||
import constants from '~/constants'; | ||
|
||
export const resumeMeAxios = axios.create({ | ||
baseURL: constants.baseUrlEnv, | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
}); | ||
|
||
resumeMeAxios.interceptors.response.use( | ||
function (response) { | ||
return response; | ||
}, | ||
function (error) { | ||
const statusCode = error.response.status; | ||
const { code } = error.response.data; | ||
switch (statusCode) { | ||
//FIXME: 에러 코드 획정되면 code 수정하기 | ||
case 401: | ||
if (code === '권한 없음 코드') { | ||
alert('로그인이 필요합니다.'); //TODO: 토스트 처리, 로그인 페이지 리다이렉트 | ||
} else if (code === '리프레시 토큰 만료 코드') { | ||
alert('토큰이 만료되어 자동으로 로그아웃되었습니다.'); //TODO: 토스트 처리, 로그인 페이지 리다이렉트 | ||
} | ||
break; | ||
case 404: | ||
if (code === '페이지 없음 코드') { | ||
redirect('/'); //FIXME: 404페이지로 리다이렉트 | ||
} | ||
break; | ||
default: | ||
if (statusCode >= 500) { | ||
console.error(error.response); | ||
} | ||
} | ||
return Promise.reject(error); | ||
}, | ||
); |
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,5 @@ | ||
const { VITE_BASE_URL } = import.meta.env; | ||
|
||
export const environments = { | ||
baseUrlEnv: VITE_BASE_URL, | ||
}; |
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,7 @@ | ||
import { environments } from './environments'; | ||
|
||
const constants = { | ||
...environments, | ||
}; | ||
|
||
export default constants; |