I came across this answer and am tweaking it to work with my authentication system: Angular 6 AuthGuard
However, I'm facing an issue where after successful authentication, instead of redirecting to the specified URL, the auth guard leads to a blank page. It works fine when there is no token, as it redirects to the login page.
Although I can see the console output for the "valid token" message, the redirection does not occur. Interestingly, if I include "return true;" at the beginning of the handler function, the forwarding works as expected.
Below is the code snippet from my auth guard service that's causing the problem:
authenticationHandler(): boolean {
if (this.myToken) {
this._auth.validateToken(this.myToken)
.subscribe((result) => {
if (result.value) {
// TODO
console.log('AuthGuard: valid token');
this.loggedIn.next(true);
return true;
} else {
this.loggedIn.next(false);
this._router.navigate(['/login']);
return false;
}
});
} else {
this.loggedIn.next(false);
this._router.navigate(['/login']);
return false;
}
}
canActivate(): boolean {
return this.authenticationHandler();
}