-
Notifications
You must be signed in to change notification settings - Fork 0
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
m.rastegar
committed
Apr 16, 2022
1 parent
40966cd
commit 168de75
Showing
5 changed files
with
355 additions
and
306 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 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,14 @@ | ||
import axios from 'axios'; | ||
import { setupInterceptorsTo } from './interceptors'; | ||
|
||
const request = setupInterceptorsTo( | ||
axios.create({ | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
}), | ||
); | ||
|
||
export const requestWithotAuth = setupInterceptorsTo(axios.create()); | ||
|
||
export default request; |
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,76 @@ | ||
import axios, { | ||
AxiosError, | ||
AxiosInstance, | ||
AxiosRequestConfig, | ||
AxiosResponse, | ||
} from 'axios'; | ||
import Cookies from 'js-cookie'; | ||
|
||
// import { useDispatch } from 'react-redux'; | ||
// import { customerActions } from 'store'; | ||
|
||
const onRequest = (config: AxiosRequestConfig): AxiosRequestConfig => { | ||
const accessToken = Cookies.get('accessToken'); | ||
if (accessToken && accessToken !== 'null') { | ||
config.headers['Authorization'] = `Bearer ${accessToken}`; | ||
} else { | ||
delete config.headers['Authorization']; | ||
} | ||
|
||
return config; | ||
}; | ||
|
||
const onRequestError = (error: AxiosError): Promise<AxiosError> => { | ||
return Promise.reject(error); | ||
}; | ||
|
||
const onResponse = (response: AxiosResponse): AxiosResponse => { | ||
return response; | ||
}; | ||
|
||
const onResponseError = async (error: any): Promise<any> => { | ||
// console.log('error.response:>> ', error.response); | ||
// dispatch(customerActions.postTokenWebLoginFailure(errObject(error.response))); | ||
if (!error.response) { | ||
return { | ||
status: 503, | ||
data: null, | ||
problem: 'Network Error', | ||
}; | ||
} | ||
if (error.response) { | ||
if (error.response.status === 403) { | ||
console.log('please login :>> '); | ||
} | ||
if (error.response.status === 401) { | ||
// Access Token was expired | ||
const accessToken = Cookies.get('accessToken'); | ||
const refreshToken = Cookies.get('refreshToken'); | ||
|
||
try { | ||
const rs = await axios.post('/refresh-token-api', { | ||
RefreshToken: refreshToken, | ||
AccessToken: accessToken, | ||
}); | ||
|
||
const { AccessToken, RefreshToken } = rs.data; | ||
|
||
Cookies.set('accessToken', AccessToken); | ||
Cookies.set('refreshToken', RefreshToken); | ||
|
||
return rs.data; | ||
} catch (_error) { | ||
return Promise.reject(_error); | ||
} | ||
} | ||
} | ||
return error.response; | ||
}; | ||
|
||
export const setupInterceptorsTo = ( | ||
axiosInstance: AxiosInstance, | ||
): AxiosInstance => { | ||
axiosInstance.interceptors.request.use(onRequest, onRequestError); | ||
axiosInstance.interceptors.response.use(onResponse, onResponseError); | ||
return axiosInstance; | ||
}; |
Oops, something went wrong.