mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-04-25 12:56:41 +07:00
auth: refactored sagas to use try-catch
This commit is contained in:
parent
7d2511e7e9
commit
c36494c3f9
22 changed files with 400 additions and 424 deletions
|
@ -26,7 +26,7 @@ import playerSaga from '~/redux/player/sagas';
|
|||
import modal, { IModalState } from '~/redux/modal';
|
||||
import { modalSaga } from './modal/sagas';
|
||||
|
||||
import { authOpenProfile, gotAuthPostMessage } from './auth/actions';
|
||||
import { authLogout, authOpenProfile, gotAuthPostMessage } from './auth/actions';
|
||||
|
||||
import boris, { IBorisState } from './boris/reducer';
|
||||
import borisSaga from './boris/sagas';
|
||||
|
@ -36,6 +36,9 @@ import messagesSaga from './messages/sagas';
|
|||
|
||||
import tag, { ITagState } from './tag';
|
||||
import tagSaga from './tag/sagas';
|
||||
import { AxiosError } from 'axios';
|
||||
import { api } from '~/utils/api';
|
||||
import { assocPath } from 'ramda';
|
||||
|
||||
const authPersistConfig: PersistConfig = {
|
||||
key: 'auth',
|
||||
|
@ -116,5 +119,28 @@ export function configureStore(): {
|
|||
|
||||
const persistor = persistStore(store);
|
||||
|
||||
// Pass token to axios
|
||||
api.interceptors.request.use(options => {
|
||||
const token = store.getState().auth.token;
|
||||
|
||||
if (!token) {
|
||||
return options;
|
||||
}
|
||||
|
||||
return assocPath(['headers', 'authorization'], `Bearer ${token}`, options);
|
||||
});
|
||||
|
||||
// Logout on 401
|
||||
api.interceptors.response.use(undefined, (error: AxiosError<{ message: string }>) => {
|
||||
if (error.response?.status === 401) {
|
||||
store.dispatch(authLogout());
|
||||
}
|
||||
|
||||
console.log('Вот что случилось на сервере:', error);
|
||||
throw new Error(
|
||||
error?.response?.data?.message || error?.message || error?.response?.statusText
|
||||
);
|
||||
});
|
||||
|
||||
return { store, persistor };
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue