mirror of
https://github.com/suyiiyii/nonebot-bison.git
synced 2025-06-04 02:26:11 +08:00
fix bug
This commit is contained in:
parent
9a42df8da7
commit
0a57ca7f81
@ -1,8 +1,14 @@
|
||||
import axios from "axios";
|
||||
import axios, {AxiosError} from "axios";
|
||||
import {Store} from "src/store";
|
||||
import { clearLoginStatus } from 'src/store/loginSlice';
|
||||
// import { useContext } from 'react';
|
||||
// import { LoginContext } from "../utils/context";
|
||||
|
||||
export const baseUrl = '/bison/api/'
|
||||
let store: Store
|
||||
export const injectStore = (_store: Store) => {
|
||||
store = _store
|
||||
}
|
||||
|
||||
// const loginStatus = useContext(LoginContext);
|
||||
axios.interceptors.request.use(function (config) {
|
||||
@ -37,4 +43,9 @@ axios.interceptors.response.use(function (response) {
|
||||
// }
|
||||
// response.data = parseToMap(data);
|
||||
return response;
|
||||
}, function(error: AxiosError) {
|
||||
if(error.response && error.response.status === 401) {
|
||||
store.dispatch(clearLoginStatus());
|
||||
}
|
||||
return Promise.reject(error);
|
||||
});
|
||||
|
@ -5,7 +5,9 @@ import App from './App';
|
||||
import './index.css';
|
||||
import reportWebVitals from './reportWebVitals';
|
||||
import store from './store';
|
||||
import {injectStore} from 'src/api/utils';
|
||||
|
||||
injectStore(store);
|
||||
ReactDOM.render(
|
||||
<React.StrictMode>
|
||||
<Provider store={store}>
|
||||
|
@ -15,3 +15,4 @@ export default store;
|
||||
|
||||
export type RootState = ReturnType<typeof store.getState>;
|
||||
export type AppDispatch = typeof store.dispatch;
|
||||
export type Store = typeof store;
|
||||
|
@ -60,7 +60,10 @@ export const loginSlice = createSlice({
|
||||
name: 'auth',
|
||||
initialState,
|
||||
reducers: {
|
||||
doLogin: loginAction
|
||||
doLogin: loginAction,
|
||||
doClearLogin: (state) => {
|
||||
state.login = false
|
||||
}
|
||||
},
|
||||
extraReducers: (builder) => {
|
||||
builder.addCase(login.fulfilled, loginAction);
|
||||
@ -70,7 +73,7 @@ export const loginSlice = createSlice({
|
||||
}
|
||||
})
|
||||
|
||||
export const { doLogin } = loginSlice.actions
|
||||
export const { doLogin, doClearLogin } = loginSlice.actions
|
||||
|
||||
export const loadLoginState = (): ThunkAction<void, RootState, unknown, AnyAction> =>
|
||||
(dispatch, getState) => {
|
||||
@ -94,6 +97,12 @@ export const loadLoginState = (): ThunkAction<void, RootState, unknown, AnyActio
|
||||
}
|
||||
}
|
||||
|
||||
export const clearLoginStatus = (): ThunkAction<void, RootState, unknown, AnyAction> =>
|
||||
(dispatch) => {
|
||||
localStorage.removeItem('loginInfo')
|
||||
localStorage.removeItem('token')
|
||||
dispatch(doClearLogin())
|
||||
}
|
||||
export const loginSelector = (state: RootState) => state.login
|
||||
|
||||
export default loginSlice.reducer
|
||||
|
Loading…
x
Reference in New Issue
Block a user