我正在尝试在 Angular 4 项目中获得一个可观察的\获取请求还可以,但是当我是管理员时,我被推到 403 未经授权的页面,我试图了解这个问题,我调试了代码,但我无法得到它,代码运行得很好,但无论如何我都会被推到 403 ......
isAdmin(): Observable<object> {
if (!this._isAdmin) {
this._isAdmin = this.http.get('./api/account/admin')
.map(
(res: Response) => res.json())
.publishReplay(1)
.refCount()
.catch((error: any) => {
this.router.navigate(['/error/403']);
return Observable.throw(error.json().error || 'Server error');
})
}
if (!this._isAdmin) {
this.router.navigate(['/error/403']);
return Observable.throw('Server error');
}
return this._isAdmin;
}
canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): boolean {
console.log("check");
const allowedRoles = next.data.permittedRoles as Array<string>;
this.authService.isAdmin().subscribe(
(data: any) => {
if (!data) {
this.router.navigate(['/error/403']);
return false;
}
allowedRoles.forEach((element: any) => {
if (data.RoleType === element) {
if (data.IsAdmin) {
return true;
}
}
}),
(err: any) =>
{
console.log(err);
this.router.navigate(['/error/403']);
return false;
}});
this.router.navigate(['/error/403']);
return false;
}
}