Redirect to login page if we get logged out.

This commit is contained in:
Matthew Stratford 2021-02-27 23:07:31 +00:00 committed by Marks Polakovs
parent 59615f4e06
commit 6412269b0f
2 changed files with 14 additions and 4 deletions

View file

@ -1,4 +1,6 @@
import qs from "qs";
import { getUserError, setCurrentUser } from "./session/state";
import store from "./store";
export const MYRADIO_NON_API_BASE = process.env.REACT_APP_MYRADIO_NONAPI_BASE!;
export const MYRADIO_BASE_URL = process.env.REACT_APP_MYRADIO_BASE!;
@ -42,8 +44,14 @@ export async function myradioApiRequest(
if (json.status === "OK") {
return json.payload;
} else {
console.error(json.payload);
throw new ApiException(json.payload);
if (res.status === 401) {
// We've logged out! Oh no!
store.dispatch(setCurrentUser({ user: null, canBroadcast: false }));
store.dispatch(getUserError("User is no longer logged in."));
} else {
console.error(json.payload);
throw new ApiException(json.payload);
}
}
}

View file

@ -73,6 +73,8 @@ const sessionState = createSlice({
export default sessionState.reducer;
export const { setCurrentUser, getUserError } = sessionState.actions;
export const getCurrentUser = (): AppThunk => async (dispatch, getState) => {
return getState().session.currentUser;
};
@ -91,7 +93,7 @@ export const getUser = (): AppThunk => async (dispatch) => {
});
dispatch(sessionState.actions.setCurrentUser({ user, canBroadcast }));
} catch (e) {
console.log("failed to get user. " + e.toString());
console.log("Failed to get user. " + e.toString());
dispatch(sessionState.actions.getUserError(e.toString()));
}
};
@ -102,7 +104,7 @@ export const getTimeslot = (): AppThunk => async (dispatch) => {
const timeslot = await getCurrentApiTimeslot();
dispatch(sessionState.actions.getTimeslotSuccess(timeslot));
} catch (e) {
console.log("failed to get selected timeslot. " + e.toString());
console.log("Failed to get selected timeslot. " + e.toString());
dispatch(sessionState.actions.getTimeslotError(e.toString()));
}
};