Redirect to login page if we get logged out.
This commit is contained in:
parent
59615f4e06
commit
6412269b0f
2 changed files with 14 additions and 4 deletions
|
@ -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!;
|
||||
|
@ -41,10 +43,16 @@ export async function myradioApiRequest(
|
|||
const json = await res.json();
|
||||
if (json.status === "OK") {
|
||||
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 {
|
||||
console.error(json.payload);
|
||||
throw new ApiException(json.payload);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function broadcastApiRequest<TRes = any>(
|
||||
|
|
|
@ -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()));
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue