I want to check if user logged in before the app is bootstrapped, so I add keycloak.isLoggedIn()
in APP_INITIALIZER
, but it always returns false
even if user is logged in.
Need to clear local storage before login if a user is not authenticated. Or maybe some other idea how to clean the local storage if the user has logged out on another environment
export function appInit(
keycloak: KeycloakService,
auth: AuthenticationService): () => Promise<any> {
return async () => {
try {
const authenticated = await keycloak.isLoggedIn();
if (!authenticated) {
localStorage.clear();
}
await keycloak
.init({
config: {
url: environment.keycloakUrl + '/auth',
realm: environment.keycloakRealm,
clientId: environment.keycloakClientId
},
initOptions: {
onLoad: 'login-required',
silentCheckSsoRedirectUri: window.location.origin + '/assets/silent-check-sso.html'
},
enableBearerInterceptor: true,
})
.then(async (success) => {
Versions. "keycloak-angular": "^9.3.2", "keycloak-js": "^16.1.1",
Tried to check in app.component.ts, but this is too late, need to make it in APP_INITIALIZER
.