I use JWT to authenticate users. Some of the routes in my angular2 application are protected by CanActivate Guard, which will be redirected to the login page if the user is not logged in. Now I am implementing the exit button and want to redirect to the login page. I want to redirect only if the current route should not be visible to unregistered users (for example, the account page). If the user, for example, on the home page, I do not want to redirect to the login page.
It would be ideal if I could just check if the current route has security and call this guard, if one exists. It can be done?
Guard:
public canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
if (this.loginService.getCurrentUserName()) {
return true;
}
this.router.navigate(['/login'], { queryParams: { returnUrl: state.url }});
return false;
}