I have an Angular application with in it a UserService and a guard. That guard is using the UserService to create a user and using that user based on its role to decide to return true or false. See the following code:
This is the guard:
canActivate(): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
return new Observable<boolean>(obs => {
this.userService.getUser().subscribe(user => {
if (user.roles.includes(Roles.IT_PROFESSIONAL)) {
obs.next(true);
} else {
this.router.navigate(['/']);
obs.next(false);
}
});
});
}
This is the UserService Class:
private static user: User;
public getUser(): Observable<User> {
if (!UserService.user) {
UserService.user = new User();
this.getGroups()
.subscribe((response: any) => {
response.value.map((v: any) => {
this.setRol(v); // use v to push v to the role array of the user
});
return of(UserService.user);
});
}
else{
return of(UserService.user);
}
}
private getGroups(): Observable<any> { // Get groups for user
return this.http.get("https://graph.microsoft.com/v1.0/me/memberOf");
}
When I run the app it says "Cannot read property 'subscribe' of undefined". The problem is that the guard subscribes on the getUser observable, but this that I gets the user observable immediately. Why doesn't it wait for the UserService to return the user with the statement "return of(UserService.user)"?