Angular — How to intercept 401 err response and redirect to login page

Liron Hazan
Aug 22, 2018

--

Below is an example of listening to an http response which was sent by the app to the api and returned with status 401 due to an expired signature of the token — in that case I would like that the user will be routed back to the login page:

  • * Edited in 2021:
    As mentioned in the comments bellow, using catchError operator instead of tapping is an alternative, up to you to decide if:
    1. state an intention of side effect as we route the user to another page — impure action.
    2. state that the code is error prone and the reaction is to navigate to login page.

--

--

Liron Hazan
Liron Hazan

Written by Liron Hazan

"The more I learn, the more I realize how much I don't know"

Responses (3)