Search code examples
angularangular4-router

How to restrict access to Login Route if user is already logged in in Angular 4?


I have successfully implement AuthGuardService which restrict access to a protected route if user is not logged in.

What I am trying to achieve is, if user is already logged in and accessed the Login Route, I want it to redirect to another route like homepage.


Solution

  • You could perform a simple check in the ngOnInit of the login component like so and redirect to another page of your choice if they are authenticated already:

    ngOnInit() {
       if (this._authService.isLoggedIn) {
          this._router.navigate(['/apps']);
       }
    }
    

    That worked for me!