I am writing a code that check if the browser has userInfo. If not, the browser calls an API to get the userInfo from server. Only if there is a valid userInfo, the user is allowed to navigate to the pages.
My problem is that canactivate() does not wait for the call to complete. I've tried Resolver which are mentioned in other threads, but it also didn't work.
So, I put async-await but it is also not working. canactivate() returns before getUser() is completed. Is the syntax of async-await is wrong in the below code?
@Injectable()
export class AuthGuard implements CanActivate {
constructor(private router: Router, private refService: RefService, private userInfoService: UserInfoService) {
}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
this.getUser();
console.info('canActivate completed');
return this.userInfoService.getUserInfo() !== null;
}
async getUser() {
if(!this.userInfoService.getUserInfo()) {
await this.refService.getTamUser().toPromise( ).then(data => {
this.userInfoService.setUserInfo(data);
});
}
}
}