In my angular SSR app, i want login component redirect automaticaly to keycloak.
export class LoginComponent implements OnInit {
constructor(
private keycloakService: KeycloakService
) {
}
async ngOnInit(): Promise<void> {
console.log("in my login component oninit");
await this.keycloakService.login();
}
}
Here the keycloakService.login function :
login() {
console.log("redirecting vers keycloak")
return this.keycloak?.login();
}
I registered the keyclaok initialization as APP_INITIALIZER. Here its init method :
async init(): Promise<void> {
if (typeof window === 'undefined') {
return;
}
try {
const { default: Keycloak } = await import('keycloak-js');
const config = { ... };
this.keycloak = new Keycloak(config);
const authenticated = await this.keycloak.init({
checkLoginIframe: false,
onLoad: 'check-sso',
silentCheckSsoRedirectUri: window.location.origin + "/assets/keycloak/silent-check-sso.html",
});
console.log('Keycloak initialized successfully');
if (authenticated) {
let up = (await this.keycloak.loadUserProfile()) as UserProfile;
up.token = this.keycloak.token || '';
up.anonymous = false;
this.setUserProfile(up);
}
} catch (error) {
console.error('Failed to initialize Keycloak', error);
throw error;
}
}
To test my configuration, When I change the onLoad: ‘check-sso’ parameter to onLoad: ‘login-required’, I m redirected to keycloak login page as expected .
In the server logs, the console.log("in my login component oninit") is displayed. But it is not displayed in browser console.
Now I want to be redirected to only if the user navigate to my app login page.
What am I missing?