I'm trying to compare defined role
(in my app.ts
) and revived role from WebServer
. But when I'm not logged I have problem with compare values:
This is what I'm doing:
export class RoutingAuthorizeStep {
public readonly userIdentity: UserIdentityModel;
constructor (userIdentity: UserIdentityModel) {
this.userIdentity = userIdentity;}
run(navigationInstruction: NavigationInstruction, next: Next) : Promise<any> {
let requiredRoles = navigationInstruction.getAllInstructions()
.map(i => i.config.settings.roles)[0] as string[];
//requiredRoles is example 'superUser'
let isUserInRole = requiredRoles?
requiredRoles.some(r => r === this.userIdentity.role) : true;
}
}
When I checked into debug:
console.log(this.userIdentity.role);
I have this message:
aurelia-logging-console.js?dc89:45 ERROR [app-router] TypeError:
Cannot read property 'role' of undefined
at RoutingAuthorizeStep.run (routing-authorize-step.ts?008f:30)
at next (aurelia-router.js?e32b:433)
I'm not an Aurelia dev but this seems like a simple JS problem unless i'm wrong. assuming the error is in the "isUserInRole" you could do this.
let isUserInRole = requiredRoles?
requiredRoles.some(r => this.userIdentity && r === this.userIdentity.role) : true;
}
Basically just check if your userIdentity exists before checking its role.
Hope this helps!