如何在拦截器中传递userid Angular 8

How to pass userid in interceptor Angular 8

好的,

我有一个 Angular 8 应用程序和一个 HttpMaintenanceInterceptor,但我没有为它使用 cookie。但是一个:

authService中的getAccessToken方法,像这样:

  getAccessToken(): string {
    return this._user ? this._user.access_token : null;
  }

而且我有一个 get 方法。 get 方法如下所示:

getDossierEntry(type: String = '' ): Observable<DossierEntry[]> {
  const entryType = type === '' ? 'all' : 'type/' + type;
  return this.http.get<DossierEntry[]>('/api/patient/${patientUUID}/DossierEntry/' + entryType);
}

但现在的问题是属性:

patientUUID

始终为空。或者这是它的输出:

http://localhost:4200/api/patient/$%7BpatientUUID%7D/DossierEntry/type/physical"

所以我尝试在 HttpMaintenanceInterceptor 中发送 patientUUID。

HttpMaintenanceInterceptor 看起来像这样:


export class HttpMaintenanceInterceptor implements HttpInterceptor {
  needsAuthenticatedUser = true;
  route: string;

  constructor(private auth: AuthService) {}

  intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    const accessToken = this.auth.getAccessToken();

    if (accessToken != null) {      
      console.log(accessToken);
      const duplicate = request.clone({
        setHeaders: { Authorization: `Bearer  ${accessToken}` }

      });

      const user$ = this.auth.loginStatus()
      .pipe( take( 1 ) );

       user$.pipe(
         map( user => {
          console.log('hello there nice to see you!!');
          let parsedRoute = this.route;
          if ( this.needsAuthenticatedUser ) {
            if ( !user ) {
              throw Error( 'Tried to call api that requires login without a user profile present' );
            } else {
              parsedRoute = parsedRoute.replace('{userId}', user.profile.sub);
           //   console.log('User Sub ' + user.profile.sub);
              console.log('User participant ' + user.profile.participant);

              parsedRoute = parsedRoute.replace('{patientUUID}', user.profile.participant);
            }
          }
          return environment.ApiOrigin + parsedRoute;
        } ),
      );


      return next.handle(duplicate);
    } else {     
      return next.handle(request);
    }
  } 
}

但我没有得到 patientUUID。

但是我得到了accessToken:console.log(accessToken);看起来像这样:

mIwM2U2MGNhMzgwYzczMzA2NjIcHM6Ly9k.....

所以我的问题是如何传递patientUUID?这样在 api 请求中 patientUUID 将不再为空。

谢谢

好的,我改成了这个:

getDossierEntry(type: String = '' ): Observable<DossierEntry[]> {
  const entryType = type === '' ? 'all' : 'type/' + type;
  return this.http.get<DossierEntry[]>(`/api/patient/{patientUUID}/DossierEntry/` + entryType);
}

但这不是我认为的问题。

因为问题是这样的:

console.log('hello there nice to see you!!');

它没有到达那条线。

应该使用反引号而不是简单的引号

'/api/patient/${patientUUID}/DossierEntry/'

应该是

`/api/patient/${patientUUID}/DossierEntry/`

使用 parsedRoute.replace

时同样如此
const user$ = this.auth.loginStatus()
      .pipe( take( 1 ) );    
       user$.pipe(
         map( user => {
          console.log('hello there nice to see you!!');
          let parsedRoute = this.route;
          if ( this.needsAuthenticatedUser ) {
            if ( !user ) {
              throw Error( 'Tried to call api that requires login without a user profile present' );
            } else {
              parsedRoute = parsedRoute.replace('{userId}', user.profile.sub);
           //   console.log('User Sub ' + user.profile.sub);
              console.log('User participant ' + user.profile.participant);

              parsedRoute = parsedRoute.replace('{patientUUID}', user.profile.participant);
            }
          }
          return environment.ApiOrigin + parsedRoute;
        } ),
      );

这部分代码将永远不会执行,因为您没有订阅可观察对象。这就是为什么 console.log 的值永远不会打印到控制台