I've got @ngrx/store and effects working fine, however, I just realized there will be a a lot of API calls (in effects), and if any of those returns a 401 error I should redirect the user to the login page. My problem is: I don't want to check that in every single effect, that would be a ton extra code for the same thing. Let's say for example I have a code like this:
Sample effect
@Effect() getMe$ = this.actions$
.ofType(GET_ME)
.map(action => action.payload)
.switchMap(payload => this.userService.me()
.map(res => ({ type: GET_ME_SUCCESS, payload: res }))
.catch(() => Observable.of({ type: GET_ME_FAILURE }))
);
userService.me()
me(): Observable<User> {
return this.apiService.get(`/auth/me`);
}
apiService.get()
get(endpoint: string): Observable<any> {
return this.http.get(`${this.base}${endpoint}`, this.options())
.map(res => res.json());
}
This works perfectly fine, but I'm not sure how to handle the case when the API return 401. Where should I redirect the user globally in that case? Should I create an action for that case? Where should I dispatch that action then? Or am I doing it completely wrong?
Any help in the right direction would be appreciated!