question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:closed
  • Created 5 years ago
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
jpaniortecommented, Nov 27, 2018

See compatibility table:

keycloak-angular Angular Keycloak SSO-RH
1.3.x 4 and 5 3 7
2.x.x 4 and 5 4 -
3.x.x 6 3 7
4.x.x 6 4 -
5.x.x 7 3 7
6.x.x 7 4 -

If you use keycloak-angular v6.x.x with angular 7.x works fine. My test:

app.init.ts

return (): Promise<any> => keycloak.init({
        config: {
            url: 'http://localhost:8080/auth',
            realm: 'realm-name',
            clientId: 'client-name'
        },
        initOptions: {
            onLoad: 'login-required',
            checkLoginIframe: false
        },
        enableBearerInterceptor: true,
        loadUserProfileAtStartUp: true,
        bearerExcludedUrls: ['/assets']
    });

app.component.ts

ngOnInit(): void
    {
        this.keycloakAngular
            .isLoggedIn()
            .then( loggedIn => {
                if( loggedIn ) {
                    console.log(this.keycloakAngular.getUsername());
                }
            })
            .catch( reason => console.log ( reason ));
    }

Other option is delete loadUserProfileAtStartUp: true and call function this.keycloakAngular.loadUserProfile()

app.component.ts

ngOnInit(): void
    {
        this.keycloakAngular
            .isLoggedIn()
            .then( loggedIn => {
                if( loggedIn ) {
                    this.keycloakAngular.loadUserProfile()
                        .then(profile => {
                            console.log('UserProfile: ', profile);
                        })
                        .catch( reason => {console.log( reason )});
                }
            })
            .catch( reason => console.log ( reason ));
    }
1reaction
jpaniortecommented, Nov 28, 2018

Hi @koldoon,

I replicate the scenario and works as expected. In my case:

  • keycloak-angular: “^6.0.0”
  • Angular: ^7.0.0
  • ClientId have the view_account scope.

Do you have the same versions/config?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting currently logged on user when running as admin
I've written an installer using NSIS and I need it to install some files (DLLs etc.) in a standard location like C:/Program Files/App...
Read more >
If you don't see a login window when you start your Mac
If your Mac starts up without displaying the login window, it's set up to bypass the login window and automatically log in a...
Read more >
Run a Program Without User Being Logged On - Server Fault
You can run a program when no one is logged in, by using the Windows Task Scheduler. However, if your application needs to...
Read more >
How to Get Logged-in User's Details with Spring Security
1. Display the default username · 2. Display any user's information (first name, last name, fullname…) · 3. Display user's assigned roles ·...
Read more >
Django Tutorial Part 8: User authentication and permissions
In this tutorial, we'll show you how to enable user authentication in the LocalLibrary website, create your own login and logout pages, add ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found