From 6412269b0f5c6297500e0411c0b92dd0aaf9a63c Mon Sep 17 00:00:00 2001 From: Matthew Stratford Date: Sat, 27 Feb 2021 23:07:31 +0000 Subject: [PATCH] Redirect to login page if we get logged out. --- src/api.ts | 12 ++++++++++-- src/session/state.ts | 6 ++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/api.ts b/src/api.ts index d38c398..206f9ee 100644 --- a/src/api.ts +++ b/src/api.ts @@ -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); + } } } diff --git a/src/session/state.ts b/src/session/state.ts index 68d32cf..89c6374 100644 --- a/src/session/state.ts +++ b/src/session/state.ts @@ -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())); } };