我正试图将我的keycloak提供程序推入一个可以在一组应用程序中重用的npm模块中,当该模块在基本应用程序中时一切正常,但是从node_module调用时会出现瓶装错误, >
拦截器代码:
type="xyz"
提供者的代码:
import { Injectable } from '@angular/core';
import { HttpInterceptor, HttpRequest, HttpHandler, HttpEvent } from '@angular/common/http';
import { Observable, from } from 'rxjs';
import { KeycloakService } from 'keycloak-angular';
import { switchMap } from 'rxjs/operators';
@Injectable()
export class AuthInterceptor implements HttpInterceptor {
constructor(private keycloakService: KeycloakService) {}
getToken() {
return from(this.keycloakService.getToken());
}
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<any> {
return this.addAuthorizationHeader(req, next)
}
addAuthorizationHeader(
request: HttpRequest<any>,
next: HttpHandler
): Observable<HttpEvent<any>> {
return this.getToken().pipe(
switchMap(token => {
request = request.clone({
setHeaders: {
Authorization: 'bearer ' + token
}
});
return next.handle(request);
})
);
}
}
AuthModule的应用模块
export function initializer(config) {
const keycloak = new KeycloakService();
return (): Promise<any> => {
return new Promise(async (resolve, reject) => {
try {
await keycloak.init({
config,
initOptions,
bearerExcludedUrls: []
});
resolve();
} catch (error) {
reject(error);
}
});
};
}
export const KeycloakProvider: FactoryProvider = {
provide: APP_INITIALIZER,
useFactory: initializer,
multi: true,
deps: [KeycloakService]
};
主应用程序的app.module ... 从'common-utils / dist / common-auth'导入{AuthModule};
import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';
import { NgModule, ModuleWithProviders, APP_INITIALIZER } from '@angular/core';
import { KeycloakAngularModule, KeycloakService } from 'keycloak-angular';
import { AuthInterceptor } from './auth-interceptor';
import { initializer } from './auth-provider';
@NgModule({
imports: [
HttpClientModule,
KeycloakAngularModule,
],
providers: [
{
provide: HTTP_INTERCEPTORS,
useClass: AuthInterceptor,
multi: true
}
]
})
export class AuthModule {
static forRoot(config): ModuleWithProviders {
return {
ngModule: AuthModule,
providers: [
{
provide: APP_INITIALIZER,
useFactory: initializer(config),
multi: true,
deps: [KeycloakService]
}
]
};
}
}
如前所述,如果提供程序位于app文件夹中,但不是来自node_module时,则此设置可以正常工作。不确定问题出在哪里,因为错误已在模块中瓶中,而在应用程序版本中未发生
错误消息:
错误:未捕获(承诺):访问验证期间发生错误。详细信息:TypeError:无法读取未定义的属性“ resourceAccess”