My guard looks like:
canLoad(
route: Route,
segments: UrlSegment[]): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
if (!this.authService.isLoggedIn) {
this.router.navigateByUrl('welcome');
}
return this.authService.isLoggedIn;
}
and the isLoggedIn getter looks like:
get isLoggedIn() {
const user = JSON.parse(localStorage.getItem('user'));
return (user !== null && user.emailVerified !== false) ? true : false;
}
my guess is that the return value is false because the rest works asynchron
I added a getAuthState method to the authService which returns a boolean to, but asynchron:
async getAuthState() {
const result = await this.afAuth.getRedirectResult();
if (result.user) {
return true;
} else {
return false;
}
}
And updated the guard to look like this:
canLoad(
route: Route,
segments: UrlSegment[]): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
if (!this.authService.isLoggedIn) {
this.router.navigateByUrl('welcome');
} else {
return this.authService.isLoggedIn;
}
if (!this.authService.getAuthState()) {
this.router.navigateByUrl('welcome');
}
return this.authService.getAuthState();
}
and in the login page I added:
this.authService.getAuthState().then(result => {
console.log(result);
if (result) {
console.log('should redirect');
this.router.navigateByUrl('/tabs/home');
}
});
the console logging is true and should redirect but its not redirecting to /tabs/home.
Maybe I have to do a little research..
Here I found a similar issue