-
Notifications
You must be signed in to change notification settings - Fork 1
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 #280 from Team-Ampersand/fix/token-refresh
🔀토큰 오염 에러 해결
- Loading branch information
Showing
4 changed files
with
59 additions
and
19 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 |
---|---|---|
@@ -1,10 +1,47 @@ | ||
import axios from 'axios'; | ||
import BASE_HEADER from '../Config/Config.json'; | ||
import { getRefresh } from './getRefresh'; | ||
import { tokenReissue } from 'api/member'; | ||
import { MemberController } from './requestUrls'; | ||
import { setToken } from './setToken'; | ||
import { getToken } from './getToken'; | ||
|
||
export const apiClient = axios.create({ | ||
baseURL: process.env.NEXT_PUBLIC_BASE_URL, | ||
headers: BASE_HEADER, | ||
}); | ||
|
||
apiClient.interceptors.request.use(getRefresh); | ||
apiClient.interceptors.response.use( | ||
function (response) { | ||
return response; | ||
}, | ||
async function (err) { | ||
const originalRequest = err.config; | ||
|
||
if ( | ||
err.response && | ||
(err.response.status === 401 || err.response.status === 403) && | ||
!originalRequest._retry | ||
) { | ||
originalRequest._retry = true; | ||
|
||
try { | ||
const { RefreshToken } = await getToken(null); | ||
const tokenResponse = await tokenReissue( | ||
'Bearer ' + RefreshToken, | ||
null | ||
); | ||
|
||
if (tokenResponse && tokenResponse.newAuthorization) { | ||
originalRequest.headers['Authorization'] = | ||
'Bearer ' + tokenResponse.newAuthorization; | ||
return apiClient(originalRequest); | ||
} | ||
} catch (refreshError) { | ||
return Promise.reject(refreshError); | ||
} | ||
} | ||
|
||
return Promise.reject(err); | ||
} | ||
); |
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