sforkowany z mirror/soapbox
Porównaj commity
1 Commity
develop
...
unauthoriz
Autor | SHA1 | Data |
---|---|---|
Alex Gleason | b604ad7a96 |
|
@ -1029,7 +1029,7 @@ export function accountLookup(acct, cancelToken) {
|
|||
dispatch({ type: ACCOUNT_LOOKUP_SUCCESS, account });
|
||||
return account;
|
||||
}).catch(error => {
|
||||
dispatch({ type: ACCOUNT_LOOKUP_FAIL });
|
||||
dispatch({ type: ACCOUNT_LOOKUP_FAIL, 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 soundsMiddleware from './middleware/sounds';
|
||||
import unauthorizedMiddleware from './middleware/unauthorized';
|
||||
import appReducer from './reducers';
|
||||
|
||||
export const store = configureStore({
|
||||
reducer: appReducer,
|
||||
middleware: [
|
||||
thunk,
|
||||
unauthorizedMiddleware(),
|
||||
errorsMiddleware(),
|
||||
soundsMiddleware(),
|
||||
],
|
||||
|
|
Ładowanie…
Reference in New Issue