I am trying to use authgaurd in angular 4 to limit access to the website based on the userid of the person trying to access it. This is running on a Intranet and all users will be domain users authenticated by microsoft active directory.
My application is running on a linux machine using apache tomcat. In order of the authguard to function the canActivate method will need to first call a IIS webservice to retrieve the current user's ID. This ID will be based to the application to verify the user's account and determine their role in the system.
I have been able to have one subscription call the next subscription and can retrieve the data but I do not know how to implement this in such a way has to have the final boolean passed back to the canActivate method. The canActivate code below just makes the Http call and moves on. What is the correct way to make it wait on the results.
@Injectable()
export class OnlyKnownUsersGuard implements CanActivate {
private isKnownUser: boolean;
private alreadyChecked = false;
constructor(private employeeService: EmployeeService, private httpService: HttpService) { }
canActivate(): Observable<boolean> | Promise<boolean> | boolean {
if ( ! this.alreadyChecked ) {
this.httpService.getLoggedInUser().subscribe(
(data: string) => this.onGetEmpInfo(data) );
this.alreadyChecked = true;
}
return this.isKnownUser;
}
onGetEmpInfo( userId: string) {
this.employeeService.loadEmployeeByUserId(userId).subscribe(
(employee: Employee) => this.isEmployeeFound(employee) ) ;
}
isEmployeeFound(employee: Employee) {
if ( employee instanceof Object ) {
this.isKnownUser = true;
} else {
this.isKnownUser = false;
}
}
}