I'm using NX to create an Angular security library so that I don't have all my security configured in one location. I'm using NX 16, Angular 16, and Auth0. I'm trying to pass in the Autho0 configurations via forRoot, but, I get the error described in the title. Below is a snippet of my code.
export const OPTIONS = new InjectionToken<string>('OPTIONS');
export interface SecurityModuleOptions {
production: boolean,
auth0Config: {
domain: string,
clientId: string,
audience: string,
useRefreshTokens: boolean,
scope: string
},
httpInterceptor: {
allowedList: string[]
},
redirectUri: string
}
export const initialize = (
options: SecurityModuleOptions,
apollo: Apollo,
httpLink: HttpLink,
authService: AuthService,
authClientConfig: AuthClientConfig
) => {
authClientConfig.set(options.auth0Config);
return authService.isAuthenticated$
.pipe(
filter<boolean>((isAuthenticated) => isAuthenticated),
mergeMap(() => authService.getAccessTokenSilently()
.pipe(
map((token: string) => {
if (!token) {
return;
}
const context = {
headers: {
Authorization: `Bearer ${ token }`
}
};
const http = httpLink.create({ uri: '/graphql' });
const ws = new WebSocketLink({
uri: '/sockets',
options: { reconnect: true }
});
const link = split(
// split based on operation type
({ query }) => {
const { kind, operation } = getMainDefinition(query) as OperationDefinitionNode;
return kind === 'OperationDefinition' && operation === 'subscription';
},
ws,
http
);
apollo.create({
link: ApolloLink.from([setContext(() => context), link]),
cache: new InMemoryCache({ addTypename: false }),
connectToDevTools: !options.production
});
})
)
)
);
};
@NgModule({
imports: [
CommonModule,
AuthModule.forRoot()
],
exports: [AuthModule]
})
export class SecurityModule {
public static forRoot(options: SecurityModuleOptions): ModuleWithProviders<SecurityModule> {
return {
ngModule: SecurityModule,
providers: [
{
provide: OPTIONS,
useValue: options
},
{
provide: APP_INITIALIZER,
useFactory: initialize,
deps: [OPTIONS, Apollo, HttpLink, AuthService, AuthClientConfig],
multi: true
},
{
provide: HTTP_INTERCEPTORS,
useClass: AuthHttpInterceptor,
multi: true
},
{
provide: Window,
useValue: window
}
]
};
}
}
I'm calling the security library in one of the NX apps.
@NgModule({
declarations: [AppComponent],
imports: [
SecurityModule.forRoot(environment),
],
bootstrap: [AppComponent]
})
export class AppModule {}
Is there any other way of doing this? Below is a screenshot of the error I'm getting. Thanks in advance for any help.
EffectsModule.forRoot
is being triggered first before APP_INITIALIZER, but it only happens after I upgrade the following:angular
13.x to 14.x@auth0/angular
1.7.0 to 1.11.1@ngrx/effects
12.x to 14.x The detailed issue was explained here: github.com/ngrx/platform/issues/931#issuecomment-376133976 The sample solution was provided here: github.com/brandonroberts/effects-issue-example – Mistassini