There are a few issues to address here, but the promise itself doesn't seem to be the main problem. Your function signature suggests the possibility of returning three different types,
canActivate(): boolean | Observable<boolean> | Promise<boolean>
However, you only ever return a boolean value. So in reality, you could simplify this to,
canActivate(): boolean
The real issue lies elsewhere. Without seeing your route configuration, it seems like you might be redirecting the user unnecessarily if they are allowed access to the requested route. Route guards should only trigger when a user is attempting to navigate to a page. If the guard allows the navigation (returns true), the user proceeds to the protected page.
But what happens if the guard denies access? You need to specify where to redirect the user in such cases.
A typical implementation might look something like this,
@Injectable()
export class AuthGuard implements CanActivate {
constructor(private router: Router) {}
canActivate(state: RouterStateSnapshot): boolean {
if (!this.localStorage.getObject('isInitialized')) {
//Allow user to proceed to requested page
return true;
} else {
//Redirect user to '/anotherpage' if not logged in
this.router.navigate(['/anotherpage']);
return false;
}
}
}
Next, define your routes as follows,
export const appRoutes: Routes = [
{
path: 'pageProtectedByAuthGuard',
component: YourComponent,
canActivate: [AuthGuard]
}
];
Finally, ensure that you import these routes in your module,
@NgModule({
imports: [RouterModule.forRoot(appRoutes, { enableTracing: false })],
...
})
For more information on CanActivate, visit: https://angular.io/api/router/CanActivate