generated from Code-4-Community/frontend-scaffold
-
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.
refactor to addAxiosInterceptors function
- Loading branch information
Showing
7 changed files
with
77 additions
and
154 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,76 +1,76 @@ | ||
// import { AxiosError, AxiosResponse } from 'axios'; | ||
// import store, { LOCALSTORAGE_STATE_KEY } from '../store'; | ||
// import { asyncRequestIsComplete } from '../utils/asyncRequest'; | ||
// import { UserAuthenticationReducerState } from './ducks/types'; | ||
// import { isTokenValid } from './ducks/selectors'; | ||
// import { logout, refresh } from './ducks/thunks'; | ||
// import { AppAxiosInstance } from '../api/apiClient'; | ||
// import authClient from './authClient'; | ||
// import protectedApiClient from '../api/protectedApiClient'; | ||
// import history from '../history'; | ||
import { AxiosError, AxiosResponse } from 'axios'; | ||
import store, { LOCALSTORAGE_STATE_KEY } from '../store'; | ||
import { asyncRequestIsComplete } from '../utils/asyncRequest'; | ||
import { UserAuthenticationReducerState } from './ducks/types'; | ||
import { isTokenValid } from './ducks/selectors'; | ||
import { logout, refresh } from './ducks/thunks'; | ||
import { AppAxiosInstance } from '../api/apiClient'; | ||
import authClient from './authClient'; | ||
import protectedApiClient from '../api/protectedApiClient'; | ||
import history from '../history'; | ||
|
||
// const userAuthenticationExtraArgs = { | ||
// authClient, | ||
// protectedApiClient, | ||
// }; | ||
const userAuthenticationExtraArgs = { | ||
authClient, | ||
protectedApiClient, | ||
}; | ||
|
||
// const INVALID_ACCESS_TOKEN = 'Given access token is expired or invalid'; | ||
const INVALID_ACCESS_TOKEN = 'Given access token is expired or invalid'; | ||
|
||
// // TODO Use throughout application | ||
// export interface AppError extends Omit<AxiosError, 'response'> { | ||
// readonly response: AxiosResponse<string>; | ||
// } | ||
// TODO Use throughout application | ||
export interface AppError extends Omit<AxiosError, 'response'> { | ||
readonly response: AxiosResponse<string>; | ||
} | ||
|
||
// export const responseErrorInterceptor = async ( | ||
// error: AxiosError, | ||
// ): Promise<AxiosResponse> => { | ||
// const originalRequest = { | ||
// ...error.config, | ||
// _retry: true, | ||
// }; | ||
export const responseErrorInterceptor = async ( | ||
error: AxiosError, | ||
): Promise<AxiosResponse> => { | ||
const originalRequest = { | ||
...error.config, | ||
_retry: true, | ||
}; | ||
|
||
// const tokens: UserAuthenticationReducerState['tokens'] = | ||
// store.getState().authenticationState.tokens; | ||
const tokens: UserAuthenticationReducerState['tokens'] = | ||
store.getState().authenticationState.tokens; | ||
|
||
// if ( | ||
// asyncRequestIsComplete(tokens) && | ||
// error?.response?.status === 401 && | ||
// error?.response?.data === INVALID_ACCESS_TOKEN && | ||
// isTokenValid(tokens.result.refreshToken) && | ||
// !(error.config as any)?._retry | ||
// ) { | ||
// await refresh(tokens.result.refreshToken)( | ||
// store.dispatch, | ||
// store.getState, | ||
// userAuthenticationExtraArgs, | ||
// ); | ||
// return AppAxiosInstance(originalRequest); | ||
// } | ||
// if ( | ||
// asyncRequestIsComplete(tokens) && | ||
// error?.response?.status === 401 && | ||
// error?.response?.data === INVALID_ACCESS_TOKEN && | ||
// !isTokenValid(tokens.result.refreshToken) | ||
// ) { | ||
// await logout()(store.dispatch, store.getState, userAuthenticationExtraArgs); | ||
// localStorage.removeItem(LOCALSTORAGE_STATE_KEY); | ||
// history.go(0); | ||
// } | ||
// return Promise.reject(error); | ||
// }; | ||
if ( | ||
asyncRequestIsComplete(tokens) && | ||
error?.response?.status === 401 && | ||
error?.response?.data === INVALID_ACCESS_TOKEN && | ||
isTokenValid(tokens.result.refreshToken) && | ||
!(error.config as any)?._retry | ||
) { | ||
await refresh(tokens.result.refreshToken)( | ||
store.dispatch, | ||
store.getState, | ||
userAuthenticationExtraArgs, | ||
); | ||
return AppAxiosInstance(originalRequest); | ||
} | ||
if ( | ||
asyncRequestIsComplete(tokens) && | ||
error?.response?.status === 401 && | ||
error?.response?.data === INVALID_ACCESS_TOKEN && | ||
!isTokenValid(tokens.result.refreshToken) | ||
) { | ||
await logout()(store.dispatch, store.getState, userAuthenticationExtraArgs); | ||
localStorage.removeItem(LOCALSTORAGE_STATE_KEY); | ||
history.go(0); | ||
} | ||
return Promise.reject(error); | ||
}; | ||
|
||
// AppAxiosInstance.interceptors.response.use( | ||
// (response) => response, | ||
// responseErrorInterceptor, | ||
// ); | ||
export const addAxiosInterceptors = () => { | ||
AppAxiosInstance.interceptors.response.use( | ||
(response) => response, | ||
responseErrorInterceptor, | ||
); | ||
|
||
// AppAxiosInstance.interceptors.request.use((config) => { | ||
// const tokens: UserAuthenticationReducerState['tokens'] = | ||
// store.getState().authenticationState.tokens; | ||
// if (asyncRequestIsComplete(tokens)) { | ||
// config.headers['X-Access-Token'] = tokens.result.accessToken; | ||
// } | ||
// return config; | ||
// }); | ||
|
||
export {}; | ||
AppAxiosInstance.interceptors.request.use((config) => { | ||
const tokens: UserAuthenticationReducerState['tokens'] = | ||
store.getState().authenticationState.tokens; | ||
if (asyncRequestIsComplete(tokens)) { | ||
config.headers['X-Access-Token'] = tokens.result.accessToken; | ||
} | ||
return config; | ||
}); | ||
}; |
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
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