I'm using JWT to authenticate users. Part of routs in my angular2 app are protected by CanActivate Guard that will redirect to login page if user is not logged in. Now I'm implementing logout button and would like to redirect to login page. I want to redirect only if current route should not be visible to non logged-in users (e.g. account page). If user is for example in home page I don't want to redirect to login page.
It would be perfect if I could just check if current route have guard and trigger this guard if it exist. It is possible to do this ?
Guard:
public canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
if (this.loginService.getCurrentUserName()) {
// logged in so return true
return true;
}
// not logged in so redirect to login page with the return url
this.router.navigate(['/login'], { queryParams: { returnUrl: state.url }});
return false;
}