Below are my code, what im tring to do is im trying to get return value directly into canActivate return, but the problem is it will run the checking first before run "this._authService.isAdmin(token)" method. i know it like that because of async. But how to prevent that? i already run of idea. thanks in advance.
i already try putting that method on construtor but the result still same.
import { Injectable } from '@angular/core';
import { Router, CanActivate } from '@angular/router';
import { Observable } from 'rxjs/Observable';
import { AuthenticationService } from "../_services/authentication.service";
@Injectable()
export class AdminGuard implements CanActivate {
isAdmin: boolean = false;
constructor(
private _authService: AuthenticationService,
private router: Router) {
}
canActivate(): Observable<boolean> | Promise<boolean> | boolean {
var token = this._authService.getJwtToken();
this._authService.isAdmin(token).subscribe(
res => {
//res will return true or false value
this.isAdmin = res;
}
);
if(this.isAdmin){
return true;
}
this.router.navigate(['/']);
return false;
}
}