Reputation: 179
I have some issue in my auth guard when I request for some data from the server and my decide for routing is dependent on the response from server how can I wait for the server response for the next step?
this is my code:
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
if (!this._sessionService.user) {
this._router.navigate(['login']);
return false;
}
this._userServiceProxy.get(this._sessionService.userId).subscribe((user: UserDto) => {
if (user["result"].shouldChangePassword) {
this._router.navigate([this.routeToChangePassword()]);
}
if ((!route.data || !route.data["permission"])) {
return true;
}
if ((this._permissionChecker.isGranted(route.data["permission"]))) {
return true;
}
this._router.navigate([this.selectBestRoute()]);
return false;
})
}
Upvotes: 0
Views: 3376
Reputation: 429
Try this
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
if (!this._sessionService.user) {
this._router.navigate(['login']);
return false;
}
return this._userServiceProxy.get(this._sessionService.userId).pipe(map((user: UserDto) => {
if (user["result"].shouldChangePassword) {
this._router.navigate([this.routeToChangePassword()]);
}
if ((!route.data || !route.data["permission"])) {
return true;
}
if ((this._permissionChecker.isGranted(route.data["permission"]))) {
return true;
}
this._router.navigate([this.selectBestRoute()]);
return false;
}))
}
Returned the observable from canActivate
and use pipe + map instead of subscribe
.
Upvotes: 1