This repository has been archived by the owner on Aug 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
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
Showing
13 changed files
with
370 additions
and
113 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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
Empty file.
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,6 @@ | ||
import { AuthInstance } from './Axios'; | ||
|
||
export const RefreshAccessToken = async (params: any) => { | ||
const { data } = await AuthInstance.post(`/jwt/`, params); | ||
return 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import axios, { AxiosResponse } from 'axios'; | ||
import { useMutation, UseMutationResult } from 'react-query'; | ||
import { RefreshAccessToken } from './Auth'; | ||
|
||
const AUTH_URL = process.env.REACT_APP_SERVER_ORIGIN; | ||
|
||
export const AuthInstance = axios.create({ | ||
baseURL: AUTH_URL, | ||
timeout: 10000 | ||
}); | ||
|
||
AuthInstance.interceptors.request.use( | ||
(config) => { | ||
const accessToken = localStorage.getItem('accessToken'); | ||
const refreshToken = localStorage.getItem('refreshToken'); | ||
if (accessToken) { | ||
if (config.headers) { | ||
config.headers.accessToken = accessToken; | ||
} | ||
} | ||
if (refreshToken) { | ||
if (config.headers) { | ||
config.headers.refreshToken = refreshToken; | ||
} | ||
} | ||
return config; | ||
}, | ||
(error: any) => Promise.reject(error) | ||
); | ||
|
||
AuthInstance.interceptors.response.use( | ||
(response: AxiosResponse) => response, | ||
async (error: any) => { | ||
if (axios.isAxiosError(error) && error.response) { | ||
const { status } = error.response.data; | ||
const refreshToken = localStorage.getItem('refreshToken'); | ||
|
||
if (status === 403) { | ||
if (refreshToken) { | ||
const { mutate: loginMutate }: UseMutationResult<any, Error, void> = | ||
useMutation(RefreshAccessToken, { | ||
onSuccess: (res: AxiosResponse) => { | ||
if (error.config) { | ||
if (error.config.headers) { | ||
error.config.headers.accessToken = res.data.accessToken; | ||
} | ||
return axios.request(error.config); | ||
} | ||
}, | ||
onError: () => { | ||
alert('로그인이 필요한 서비스입니다.'); | ||
} | ||
}); | ||
|
||
loginMutate(); | ||
} else { | ||
throw error; | ||
} | ||
} else { | ||
throw error; | ||
} | ||
} else { | ||
throw error; | ||
} | ||
} | ||
); | ||
|
||
export const DefaultInstance = axios.create({ | ||
baseURL: AUTH_URL, | ||
timeout: 10000 | ||
}); |
Oops, something went wrong.