When working on an Angular Guard, I encountered a challenge where I needed to select two fields from the ngrx store. Here is the code snippet for reference:
@Injectable()
export class RoleGuard implements CanActivate {
constructor(
public router: ActivatedRouteSnapshot,
private store: Store<AppState> ) {}
canActivate(route: ActivatedRouteSnapshot): Observable<boolean> {
const expectedRole = route.data.Role;
return combineLatest(
this.store.pipe(select(isLoggedIn)),
this.store.pipe(select(currentUser)),
).pipe(
tap( ([loggedIn, user]) =>
{
if ( loggedIn && !(user.role.find(expectedRole) >= 0) ) {
this.router.navigateByUrl('/error/403')
};
}
)
);
}
}
Despite my efforts, I encountered the error
Type 'boolean | [any, any]' is not assignable to type 'boolean'
, which is due to the combineLatest returning the result in an array format. I'm seeking a more elegant solution than using combineLatest and avoiding nesting the select observables. Are there any suggestions for better alternatives?