Redirect after login

If a user has not logged in yet and attempts to access a landing page that requires it, I would like to remember this landing page, present the user with the login form, and upon successful login redirect the user back to the saved landing page.

What’s the best way to achieve this?

You want to use the HttpInterceptor:

Here’s a simple example of what you can do in the intercept method that will redirect to a login screen if they are not authenticated:

return next.handle(authRequest)
      .catch(errorResponse => {
        if (errorResponse.status === 401) {
          this.authenticationService.logout(); // this deletes locally stored tokens and redirects to the login screen

        return Observable.throw(errorResponse.error);

In your case, you’d just want to modify the authenticationService logout method to store the page they came from so you could take them back to that page after login.

Not sure what context your app has but you could just do the following: on app start perform an API auth call that validates auth (JWT token) for a user as part of your start cycle, if their token is valid -> direct them to the main page if invalid - push in a login/signup page.