Mike K.
Mike K.

Reputation: 608

Getting an error of "this.appInits[i] is not a function" when putting in an APP_INITIALIZER in my angular app

I'm trying to implement https://www.npmjs.com/package/keycloak-angular into my site, so that my front-end angular app can authenticate a user with Keycloak.

After putting in the code, I keep getting an error of:

ERROR TypeError: this.appInits[i] is not a function
    at ApplicationInitStatus.runInitializers (core.es5.js?de3d:2807)
    at eval (core.es5.js?de3d:4506)

Anyone know why this might be happening? I am using Angular 4.3.2 and keycloak-angular 7.2.0 and keycloak-js 8.0.2.

Here is most of my app.module.ts:

import {NgModule, APP_INITIALIZER} from '@angular/core';
import { KeycloakService, KeycloakAngularModule } from 'keycloak-angular';
import { initializer } from './utils/app-init';
...

@NgModule({
    imports: [
        ...
        KeycloakAngularModule
    ],
    declarations: [
        ...
    ],
    providers: [
        ...
        {
            provide: APP_INITIALIZER,
            useFactory: initializer,
            multi: true,
            deps: [KeycloakService],

            useClass: OdpToastOptions
        }
    ],
    bootstrap: [JhiMainComponent]
})

export class UiAppModule {}

Here is my app-init.ts:

export function initializer(keycloak: KeycloakService): () => Promise<any> {

    keycloak.init({
        config: {
            url: 'https://keycloak.mydomain.com/auth',
            realm: 'myrealm',
            clientId: 'myclient',
        },
        initOptions: {
            onLoad: 'login-required',
            checkLoginIframe: false,
        },
        enableBearerInterceptor: true,
        bearerExcludedUrls: ['/assets', '/clients/public'],
    });

    return (): Promise<any> => keycloak.init();
}

Upvotes: 0

Views: 2408

Answers (1)

S&#233;bastien
S&#233;bastien

Reputation: 12139

You can't have both a useFactory and a useClass in a provider. I guess that useClass: OdpToastOptions should be removed from this provider:

{
    provide: APP_INITIALIZER,
    useFactory: initializer,
    multi: true,
    deps: [KeycloakService],

    useClass: OdpToastOptions // <-- REMOVE LINE
}

This might fix it:

{
    provide: APP_INITIALIZER,
    useFactory: initializer,
    multi: true,
    deps: [KeycloakService],
}

Upvotes: 1

Related Questions