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 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_NON_API_BASE = process.env.REACT_APP_MYRADIO_NONAPI_BASE!;
export const MYRADIO_BASE_URL = process.env.REACT_APP_MYRADIO_BASE!; export const MYRADIO_BASE_URL = process.env.REACT_APP_MYRADIO_BASE!;
@ -41,11 +43,17 @@ export async function myradioApiRequest(
const json = await res.json(); const json = await res.json();
if (json.status === "OK") { if (json.status === "OK") {
return json.payload; return json.payload;
} else {
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 { } else {
console.error(json.payload); console.error(json.payload);
throw new ApiException(json.payload); throw new ApiException(json.payload);
} }
} }
}
export async function broadcastApiRequest<TRes = any>( export async function broadcastApiRequest<TRes = any>(
endpoint: string, endpoint: string,

View file

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