sforkowany z mirror/soapbox
Porównaj commity
1 Commity
develop
...
unauthoriz
Autor | SHA1 | Data |
---|---|---|
![]() |
b604ad7a96 |
|
@ -1029,7 +1029,7 @@ export function accountLookup(acct, cancelToken) {
|
||||||
dispatch({ type: ACCOUNT_LOOKUP_SUCCESS, account });
|
dispatch({ type: ACCOUNT_LOOKUP_SUCCESS, account });
|
||||||
return account;
|
return account;
|
||||||
}).catch(error => {
|
}).catch(error => {
|
||||||
dispatch({ type: ACCOUNT_LOOKUP_FAIL });
|
dispatch({ type: ACCOUNT_LOOKUP_FAIL, error });
|
||||||
throw error;
|
throw error;
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
|
@ -0,0 +1,17 @@
|
||||||
|
import type { AnyAction } from 'redux';
|
||||||
|
import type { ThunkMiddleware } from 'redux-thunk';
|
||||||
|
|
||||||
|
const isSkipped = (action: AnyAction): boolean => !!action.skipUnauthorized;
|
||||||
|
const isUnauthorized = (action: AnyAction): boolean => action.error?.response?.status === 401;
|
||||||
|
|
||||||
|
/** Redirect to login if the API returns a 401. */
|
||||||
|
export default function unauthorizedMiddleware(): ThunkMiddleware {
|
||||||
|
return () => next => action => {
|
||||||
|
if (isUnauthorized(action) && !isSkipped(action)) {
|
||||||
|
window.location.href = '/login';
|
||||||
|
return undefined;
|
||||||
|
} else {
|
||||||
|
return next(action);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
|
@ -4,12 +4,14 @@ import thunk, { ThunkDispatch } from 'redux-thunk';
|
||||||
|
|
||||||
import errorsMiddleware from './middleware/errors';
|
import errorsMiddleware from './middleware/errors';
|
||||||
import soundsMiddleware from './middleware/sounds';
|
import soundsMiddleware from './middleware/sounds';
|
||||||
|
import unauthorizedMiddleware from './middleware/unauthorized';
|
||||||
import appReducer from './reducers';
|
import appReducer from './reducers';
|
||||||
|
|
||||||
export const store = configureStore({
|
export const store = configureStore({
|
||||||
reducer: appReducer,
|
reducer: appReducer,
|
||||||
middleware: [
|
middleware: [
|
||||||
thunk,
|
thunk,
|
||||||
|
unauthorizedMiddleware(),
|
||||||
errorsMiddleware(),
|
errorsMiddleware(),
|
||||||
soundsMiddleware(),
|
soundsMiddleware(),
|
||||||
],
|
],
|
||||||
|
|
Ładowanie…
Reference in New Issue