Some newbie questions of how to handle unauthorised
in redux and redux-observable
I have an application where user must be authenticated in order make requests to an api.
If user provides token that is invalid, endpoint returns 401 http error code.
So how do I elegantly handle this (i'm using fetch)?
Option 1:
- create some kind of function with which I would wrap all api calls in epics which would convert 401 responses to something like
NotLoggedInAction
- create a reducer that handles
NotLoggedInAction
and sets flag on the state that user is not logged in - subscribe to state changes in root component (?) and check for not logged in flag
- if not logged in flag arrives, redirect user to login page (login page is external app, so i would just use
window.location.href
?)
This option is kind of flexible, because before redirecting i can show some popup like "hey your session expired, etc."
But it is also not error prone (I have to always hook up error handling to each call, and add dependency in each epic to NotLoggedInAction
)
Option 2:
- in combineEpics use do
and watch for errors
- if 401 arrives, simply redirect user to login page (via same window.location.href
This Looks much simpler to me, but this is more like a 'hack`?
Am I going the right way? Are the better options?