I'm trying to get an observable in Angular 4 project\ The get request is okay but I'm getting pushed away to 403 unauthorized page while I'm the admin, I'm trying to understand the problem of this, I debugged the code but I cant get it, The code run perfectly fine but I'm getting pushed to 403 anyway...
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;
}
}