Why Doesn't Keycloak SSO Redirect to Login Page in Angular, After Clearing Storage Even in Incognito Mode?

227 Views Asked by At

I have been exploring the Keycloak Single Sign-On (SSO) setup in Angular using the keycloak-angular package. I followed the documentation on the npm registry for keycloak-angular to set up Keycloak SSO in Angular.

To test the SSO functionality, I created two Angular applications and configured the same Keycloak configuration in both of them.

import { BrowserModule } from '@angular/platform-browser';
import { AppRoutingModule } from './app-routing.module';
import { KeycloakAngularModule, KeycloakService } from 'keycloak-angular';

import { APP_INITIALIZER, NgModule } from '@angular/core';

function initializeKeycloak(keycloak: KeycloakService) {
  return () =>
    keycloak.init({
      config: {
        url: 'http://localhost:8080/',
        realm: 'myrealm',
        clientId: 'SSO',
      },
      initOptions: {
        onLoad: 'check-sso',
        silentCheckSsoRedirectUri:
          window.location.origin + '/assets/silent-check-sso.html',
      },
    });
}

@NgModule({
  declarations: [AppComponent],
  imports: [BrowserModule, AppRoutingModule, KeycloakAngularModule],
  providers: [
    {
      provide: APP_INITIALIZER,
      useFactory: initializeKeycloak,
      multi: true,
      deps: [KeycloakService],
    },
  ],
  bootstrap: [AppComponent],
})
export class AppModule {}

When I initially start my first application, it redirects me to the Keycloak login page, and I successfully log in with valid credentials. After that, when I opened my second application, it did not redirect me to the login page, indicating that SSO was working perfectly.

However, my question is, even if I clear the local storage, session storage, and cache in both Angular applications, it still does not redirect me to the login page. Additionally, even if I clear the session in Keycloak, it does not redirect me to the login page. Even when I open my Angular application in incognito mode, it does not redirect to the login page; instead, it directly renders the home page without any authentication check.

Versions I Am Using

Angular Version keycloak-js Version keycloak-angular Version
16.1.5 20.0.3 14.1.0

I'm confused about what's happening. Can someone please explain it to me?

0

There are 0 best solutions below