Porównaj commity

...

1 Commity

Autor SHA1 Wiadomość Data
Alex Gleason b604ad7a96
Add WIP unauthorizedMiddleware 2022-05-16 17:01:36 -05:00
3 zmienionych plików z 20 dodań i 1 usunięć

Wyświetl plik

@ -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;
});
};

Wyświetl plik

@ -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);
}
};
}

Wyświetl plik

@ -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(),
],