获取访问令牌

时间:2020-02-18 22:46:50

标签: angular auth0

我需要获取令牌,并且令牌返回很多信息

getTokenSilently$(options?): Observable<string> {
        return this.auth0Client$.pipe(
          concatMap((client: Auth0Client) => from(client.getTokenSilently(options)))
        );
      }


    const token =  auth.getTokenSilently$()

https://auth0.com/docs/quickstart/spa/angular2/02-calling-an-api

enter image description here

1 个答案:

答案 0 :(得分:0)

您必须订阅Observable才能获取令牌,因为getTokenSilenty $方法是可观察的。

使用

AuthService.ts

    getTokenSilently$(options?): Observable<string> {
        return this.auth0Client$.pipe(
          concatMap((client: Auth0Client) => from(client.getTokenSilently(options)))
      );
    }

RoleGuardService.ts

    canActivate(): Observable<boolean> {
      return this.auth.getTokenSilently$().pipe(
        first(),
        map((token) => {
          console.info(token);
          // apply here your code to get the permission
          return true;
         })
       )
     }