mirror of
https://github.com/jeffvli/feishin.git
synced 2026-05-15 04:51:06 +02:00
Update libs
This commit is contained in:
+30
-13
@@ -1,23 +1,23 @@
|
||||
/* eslint-disable no-underscore-dangle */
|
||||
import Axios from 'axios';
|
||||
import { authApi } from '../api/authApi';
|
||||
import { authApi } from '../api/auth.api';
|
||||
|
||||
export const api = Axios.create({
|
||||
export const ax = Axios.create({
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
withCredentials: false,
|
||||
});
|
||||
|
||||
api.interceptors.request.use(
|
||||
ax.interceptors.request.use(
|
||||
(config) => {
|
||||
const { serverUrl, accessToken } = JSON.parse(
|
||||
const { state } = JSON.parse(
|
||||
localStorage.getItem('authentication') || '{}'
|
||||
);
|
||||
|
||||
config.baseURL = `${serverUrl}/api`;
|
||||
config.baseURL = `${state.serverUrl}/api`;
|
||||
config.headers = {
|
||||
Authorization: `Bearer ${accessToken}`,
|
||||
Authorization: `Bearer ${state.accessToken}`,
|
||||
'Content-Type': 'application/json',
|
||||
};
|
||||
return config;
|
||||
@@ -26,27 +26,28 @@ api.interceptors.request.use(
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
api.interceptors.response.use(
|
||||
|
||||
ax.interceptors.response.use(
|
||||
(res) => {
|
||||
return res;
|
||||
},
|
||||
async (err) => {
|
||||
if (err.response && err.response.status === 401) {
|
||||
const { config } = err;
|
||||
const auth = JSON.parse(localStorage.getItem('authentication') || '{}');
|
||||
|
||||
if (err.response.data.error.message === 'jwt expired' && !config.sent) {
|
||||
config.sent = true;
|
||||
|
||||
const auth = JSON.parse(localStorage.getItem('authentication') || '{}');
|
||||
|
||||
const { accessToken } = (
|
||||
await authApi.refresh(auth.serverUrl, {
|
||||
await authApi.refresh(auth.state.serverUrl, {
|
||||
refreshToken: auth.refreshToken,
|
||||
})
|
||||
).data;
|
||||
|
||||
localStorage.setItem(
|
||||
'authentication',
|
||||
JSON.stringify({ ...auth, accessToken })
|
||||
JSON.stringify({ ...auth, state: { ...auth.state, accessToken } })
|
||||
);
|
||||
|
||||
config.headers = {
|
||||
@@ -57,8 +58,24 @@ api.interceptors.response.use(
|
||||
return Axios(config);
|
||||
}
|
||||
|
||||
localStorage.setItem('authentication', '{}');
|
||||
window.location.reload();
|
||||
localStorage.setItem(
|
||||
'authentication',
|
||||
JSON.stringify({
|
||||
...auth,
|
||||
state: { ...auth.state, accessToken: '', refreshToken: '' },
|
||||
})
|
||||
);
|
||||
|
||||
if (err.response.data.error.message === 'No auth token') {
|
||||
localStorage.setItem(
|
||||
'authentication',
|
||||
JSON.stringify({
|
||||
...auth,
|
||||
state: { ...auth.state, accessToken: '', refreshToken: '' },
|
||||
})
|
||||
);
|
||||
// window.location.reload();
|
||||
}
|
||||
}
|
||||
return Promise.reject(err);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user