0

I have a canActivate guard on one of my route.

canActivate(): boolean {
    if(this.isLogged()) {
        return true
    }
    else {
        this._router.navigate(['/login'])
        return false
    }
}

So, I can only activate it if I am logged in. If I am not, I do want to navigate back to the login page. So I did add : this._router.navigate(['/login'])

It seems to work, but I am not sure if this is the right way to proceed. I have seen a couple of stuff using injectors (Redirect to a different component inside @CanActivate in Angular2) but these are pretty old posts. I am in Angular 2.4.1.

Community
  • 1
  • 1
Scipion
  • 11,449
  • 19
  • 74
  • 139
  • 1
    http://stackoverflow.com/questions/38366340/redirect-to-login-route-if-the-user-not-logged-in-angular2-2-0-0-rc-4 --> seems like this is a normal way of solving the user not logged in issue. – Apoorv Kansal Dec 28 '16 at 08:47
  • https://angular.io/docs/ts/latest/guide/router.html#!#guards --> really useful information about guards and has an example about restricting access to the "admin" panel for certain users (analogous to your problem). – Apoorv Kansal Dec 28 '16 at 08:48
  • This is a good way to manage it. Although I'd not check that from the client side but from the server which always holds the truth. – Alex Beugnet Dec 28 '16 at 09:30

0 Answers0