I have implemented the functionality where I am able to get the request and control the authorization of the page, I want to redirect to login page in case of false request.
public canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
return this.authSer.isAuthenticated().pipe(map((response) => {
console.log('I ma here');
if (response.status === 200) {
return true;
} else {
console.log('Error getting data');
return false;
}
}), catchError((error) => of(false))
);
}
How can I route to the login page from here? I am using angular 6
I know the question specifically states Angular 6, I just want to mention that as of angular 7.1 You can return an UrlTree instead or a boolean from your guard - as well as the
Promise
andObservable
equivalents -, and that will serve as an automatic redirect. So in your case:I also highly recommend upgrading to Angular 7 using their Guide. It's pretty painless and it will enable you with new features as well as bringing a bunch of bugfixes.