No way to get logged user when application starts up.
See original GitHub issue- [x] bug report -> please search for issues before submitting
- [ ] feature request
Versions.
keycloak-angular: 4.0.2 angular: 7.0.0
Repro steps.
I’m trying to simply show logged username in the application header. So I init the library as follows (and was taken from documentation):
In app.module providers:
{ provide: APP_INITIALIZER, useFactory: appInitializer, multi: true, deps: [KeycloakService] },
init func impl:
function appInitializer( kk: KeycloakService ): () => Promise<any> {
return () => kk.init( {
initOptions: {
onLoad: 'check-sso',
checkLoginIframe: true
},
loadUserProfileAtStartUp: true,
enableBearerInterceptor: true
} );
}
In the main component I’m trying to get that info:
constructor( private keycloakAngular: KeycloakService ) {
}
ngOnInit(): void {
this.keycloakAngular
.isLoggedIn()
.then( result => {
this.loggedIn = result;
this.userName = this.loggedIn
? this.keycloakAngular.getUsername()
: '';
} )
.catch( reason => console.log( reason ) );
}
As the application starts it’s being redirected to keycloak service (it’s ok), then back and… either I was logged in or not before I’ve got nothing as a result. And only when I go to some protected by keycloak route, my application redirects (again) and username is retrieved.
What am I doing wrong? How can I get currently logged user (if so) when application just starts? Thank you!
I’ve tried to save taken tokens to localStorage and then read and put them in init() function - in this case at the beginning everything was ok, but if refresh token is obsolete after some time, application crashes with unhandled 401 realm response with no way to continue. And I can’t find a way to handle that error: it is happening somewhere inside the library and throws as Zone exception.
Desired functionality.
Please add/fix the way to load logged user info during startup оr add documentation/workaround examples for such case.
Issue Analytics
- State:
- Created 5 years ago
- Comments:10 (2 by maintainers)
Top GitHub Comments
See compatibility table:
If you use keycloak-angular v6.x.x with angular 7.x works fine. My test:
app.init.ts
app.component.ts
Other option is delete
loadUserProfileAtStartUp: true
and call functionthis.keycloakAngular.loadUserProfile()
app.component.ts
Hi @koldoon,
I replicate the scenario and works as expected. In my case:
Do you have the same versions/config?