Keycloak-angular: Events related to authentication

Created on 5 Aug 2019  路  11Comments  路  Source: mauriciovigolo/keycloak-angular

Hi,
I want to monitor events related to authentication, as follows
START, INITIALIZED, LOGGED_IN, LOGGED_OUT, TOKEN_REFRESHED, TOKEN_VALIDITY_CHECKED, TOKEN_EXPIRED, NOT_AUTHENTICATED
how can I do it?

versions : "keycloak-angular": "^1.3.0" , Angular: "^5.2.9" and , keycloak server ; 3.4.2.Final

Enhancement

All 11 comments

??

Hi @dghamir, sorry for the late response.

The following events are provided by keycloak-js:

  • OnAuthError
  • OnAuthLogout
  • OnAuthRefreshError
  • OnAuthRefreshSuccess
  • OnAuthSuccess
  • OnReady
  • OnTokenExpire

You can subscribe to these events by using KeycloakService#keycloakEvents$ which returns an Observable with a KeycloakEvent type.
There was an example showing how to deal with events. I could release a new version of it tonight.

@dghamir, I worked on the example but there are some tricky parts on it. The previous example worked but it was removed because it was complex to understand.

In fact, the KeycloakService is initialized at the beginning and any component or service that wants to get the events should load before the event happening, otherwise, you will not get the OnAuthSuccess or OnReady for example.

I will think in a better way to deal with this event scenarios. Sorry for not being able to deliver the example for you today.

Hi @mauriciovigolo ,
Thank you for your answers

any news about this subject?

I will also be very thankful if you provide a way to deal with the events, especially with the OnReady. I really need this in my current project. Thanks!

I think we should use ReplaySubject(1) for each event types instead of single Subject for all of them.

Is this working ? Definitely need this - I tried it and none of the subscriber code was hit

I think we should use ReplaySubject(1) for each event types instead of single Subject for all of them.

@mauriciovigolo what do you think? I would like to make a MR for this.

Any news on this ? @dghamir did you figure it out ?

It's not possible to listen to these events once the application has been bootstrapped. I am not in favor of introducing a ReplaySubject for this as it might create problems if subscribed to later in the application, for example when loading some code dynamically.

If you want to listen to these events it is recommended that you subscribe to them before initializing the Keycloak service during the bootstrapping of the application. If you need to derive state from these events you can also use the existing state that is available from keycloak-js such as this.keycloak.getKeycloakInstance().authenticated.

For example:

function initializeKeycloak(keycloak: KeycloakService) {
  return async () => {
    from(keycloak.keycloakEvents$).subscribe(event => console.log(event))

    return keycloak.init({
      config: {
        url: 'http://localhost:8080/auth',
        realm: 'master',
        clientId: 'my-app',
      },
      initOptions: {
        enableLogging: true,
        onLoad: 'check-sso',
        silentCheckSsoRedirectUri: window.location.origin + '/assets/silent-check-sso.html',
      },
    });
  }
}

That said, I am willing to improve the situation in regards to event handling. We're thinking of introducing a new KeycloakEvents service which will allow you to subscribe to specific events or all events and optionally ask for a replayed version of the events for more information on this see #274

Was this page helpful?
0 / 5 - 0 ratings