I'm currently facing an issue while trying to implement a guard using the userService
to retrieve necessary information. The implementation of the UserService
is as follows:
getUserDetails(): Observable<User> {
this.requestUrl = `${configs.bgwssApi}v1/user/details`;
return this.http.get<User>(this.requestUrl).pipe(catchError(this.handleError));
}
isBoardMember(): Observable<boolean> {
this.getUserDetails().pipe(
map(data => {
if (data.userRole !== 'MEMBER') {
return of(false);
}
console.log(data); //Not printed
})
);
return of(true);
}
While the getUserDetails
function works correctly by returning the required information, the issue arises with the isBoardMember
function always returning true without even checking the userRole
. What could possibly be causing this problem? How can I resolve the isBoardMember
method so that it returns the accurate value? This is crucial because my guard conducts a verification like the one shown below, which is functioning properly:
canActivate(route: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean> {
return this.userService.isBoardMember().pipe(
map(data => {
if (data === false) {
this.router.navigate(['main']);
return false;
}
return true;
})
);
}