I have a fetch Epic that may return a 401 or 403 if the user doesn't have access to a server-side resource. I want to redirect during this Epic if this is the case.
Right now I'm doing this:
export const fetch$ = <T = any>(req: IRequest) =>
from(performFetch<T>(req)).pipe(
switchMap(res => of(res)),
catchError(e => {
if (e.status === 401 || e.status === 403) {
window.location.replace(`/login?fwd=${encodeURIComponent(window.location.pathname)}`);
return NEVER;
}
return of(e);
})
);
Where performFetch
is just a simple function that does the fetch and returns a promise.
I'm using window.location.replace
and so far it's working fine, but someone told me that it would mess up React.
I tried using connected-react-router
and return a push
action, but it wasn't doing the redirect.
Am I safe to continue doing it this way, or is there a better way?