mauriciovigolo / keycloak-angular

Easy Keycloak setup for Angular applications.
MIT License
730 stars 280 forks source link

angular crashes with check-sso #544

Open clinche opened 9 months ago

clinche commented 9 months ago

Bug Report or Feature Request (mark with an x)

- [x] bug report

Versions.

    "@angular/common": "^17.0.0",
    "keycloak-angular": "^15.1.0",
    "keycloak-js": "^23.0.6",

Repro steps.

i followed the tutorial on how to setup everything, it works fine when im logged in, but when im not the angular app crashes with ERROR Not Authenticated. i tried to add catches everywhere but with no luck, the original code crashing the app was

export function KeycloakFactory(keycloak: KeycloakService) {
  return () => {
    return keycloak.init({
      config: {
        url: environment.keycloakUrl,
        realm: environment.keycloakRealm,
        clientId: environment.keycloakClientId,
      },
      initOptions: {
        onLoad: "check-sso",
        silentCheckSsoRedirectUri: window.location.origin + "/assets/silent-check-sso.html",
      },
    });
  };
}

but even this atrocity dont catch anything

export function KeycloakFactory(keycloak: KeycloakService) {
  return async () => {
    try {
      return await keycloak
        .init({
          config: {
            url: environment.keycloakUrl,
            realm: environment.keycloakRealm,
            clientId: environment.keycloakClientId,
          },
          initOptions: {
            onLoad: "check-sso",
            silentCheckSsoRedirectUri: window.location.origin + "/assets/silent-check-sso.html",
          },
        })
        .catch((error) => {
          console.error("Keycloak init failed", error);
        });
    } catch (e) {
      console.error(e);
    }
  };
}

i added the good providers as when im logged in, again, it works fine

const keycloakProviders = [
  importProvidersFrom(KeycloakAngularModule),
  {
    provide: APP_INITIALIZER,
    useFactory: KeycloakFactory,
    deps: [KeycloakService],
    multi: true,
  },
];

The log given by the failure.

ERROR Not authenticated core.mjs:11806:22
    Angular 14
    RxJS 6
    Angular 21
    setupCheckLoginIframe keycloak.mjs:1232
    onLoad keycloak.mjs:229
    processInit keycloak.mjs:297
    Angular 25
    check3pCookiesSupported keycloak.mjs:1305
    Angular 23
    init keycloak.mjs:322
    Angular 5
    KeycloakFactory app.config.ts:33
    __async chunk-RXFX7ESX.js:37
    ZoneAwarePromise Angular
    __async chunk-RXFX7ESX.js:21
    KeycloakFactory app.config.ts:29
    Angular 11
    <anonymous> main.ts:5

Desired functionality.

when using check-sso dont throw an error when user is not authenticated and allow the app to not crash, or throw an error in a way that can be catched