简体   繁体   English

Angular canActivate Guard with API

[英]Angular canActivate Guard with API

I'm currently working on an Angular Webpage, using an python API in the backend.我目前正在开发一个 Angular 网页,在后端使用 python API。 I wanted to add an canActivate Guard, to ensure that the User is in an Adminlist, laying on another Server.我想添加一个 canActivate Guard,以确保用户位于 Adminlist 中,位于另一台服务器上。

My Problem is, that it seems that the guard doesn't wait for the response of the API, i tested it with a window.alert in the guard itself, and it showed me an "undefined" as Output.我的问题是,警卫似乎不等待 API 的响应,我在警卫本身中使用 window.alert 对其进行了测试,它向我显示了“未定义”作为输出。 When I test my method in my auth.service, I get the correct response in my console.log, so my method seems to return the correct boolean, but my guard seems to not wait for the answer from the API.当我在我的 auth.service 中测试我的方法时,我在我的 console.log 中得到了正确的响应,所以我的方法似乎返回了正确的布尔值,但我的守卫似乎没有等待 API 的答案。

I got the following Code:我得到以下代码:

auth.service.ts: auth.service.ts:

import { HttpClient} from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';

import { IAuth } from './IAuth';

@Injectable({
  providedIn: 'root'
})
export class AuthService {

   constructor(private http: HttpClient) { }

Acc: boolean;
getauth(): Observable<IAuth['Access']> {
return this.http.get<IAuth['Access']>('http://x.x.x.x:5555/auth');
}

get Access(): boolean {
   this.getauth().subscribe( data =>
   this.Acc = data);
return this.Acc;
}

}

And this is my auth.guard.ts:这是我的 auth.guard.ts:

import { Injectable } from '@angular/core';
import { Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Observable } from 'rxjs';

import { AuthService} from './auth.service';

@Injectable({
  providedIn: 'root'

})
export class AuthGuard implements CanActivate {
   constructor (
      private auth: AuthService,
      private router: Router
  ) {}
    canActivate(
       next: ActivatedRouteSnapshot,
       state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
          if (!this.auth.Access) {
             this.router.navigate(['/noaccess']);
             window.alert(this.auth.Access);
             window.alert(this.auth.Acc);
           }
          return true;
  }
}

I hope you guys can help me solving this problem, like I said, I get the wrong response in the guard (undefined) for both, accessing the property and accessing the method.我希望你们能帮我解决这个问题,就像我说的,我在警卫(未定义)中得到错误的响应,访问属性和访问方法。

get Access(): boolean {
   this.getauth().subscribe( data => this.Acc = data);
   return this.Acc;
}

you don't wait for your HTTP call to finish, so your guard doesn't waith either.您不会等待 HTTP 调用完成,因此您的守卫也不会等待。 Use this instead.改用这个。

get Access(): Observable<any> {
   return this.getauth().pipe(tap(data => this.Acc = data));
}

You guard then becomes你守卫然后变成

canActivate(
  next: ActivatedRouteSnapshot,
  state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
    return this.auth.Access.pipe(map(res => {
      if (!res) { this.router.navigate(['/noaccess']); }
      return !!res;
    }));
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM