I need to wait until the getPermissions() observable is complete before calling the checkPermissions() method. But for the life of me I can't get it...
I've tried using async/await too but that doesn't seem to work for me either?
I need to have my permissions, before I can check them, right. Thoughts?
If there's a better way, I'm all ears.
Much appreciated.
import { Injectable } from '@angular/core';
import { Router, CanActivate, ActivatedRouteSnapshot } from '@angular/router';
import { AuthService } from './auth.service';
@Injectable()
export class RoleGuardService implements CanActivate {
constructor(public auth: AuthService,public router: Router) { }
canActivate(route: ActivatedRouteSnapshot): boolean {
//This will be passed from the route config, on the data property
const expectedRole = route.data.expectedRole;
var hasPerm = this.loadAndCheckPermissions(expectedRole);
console.log('done! ' + hasPerm);
return hasPerm;
}
loadAndCheckPermissions(expectedRole) {
var hasPermission = false;
localStorage.clear();
var myPermissions = localStorage.getItem('user-permissions');
if (myPermissions === null) {
console.log("PERMISSIONS from Server!");
//You can't wait for an Observable or Promise to complete.
//You can only subscribe to it to get notified when it completes or emits an event.
this.auth.getPermissions()
.subscribe(res => {
localStorage.setItem('user-permissions', JSON.stringify(res.body));
//Check permissions now
//hasPermission = this.checkPermissions(expectedRole);
});
} else {
hasPermission = this.checkPermissions(expectedRole);
}
console.log('loadAndCheckPermissions ' + hasPermission);
return hasPermission;
}
//Check a permission here
checkPermissions(expectedRole) {
return this.auth.checkPermission(expectedRole);
}
}