Actions after create the user? (Registration+creation)

Hi there,

I’m a new user of keycloak, few days and studying…

Some material teaching actions hooks after user create? I need to create a database for some user created in my keycloak. It’s for a multi tenant saas app.

What’s the best way for delivery multi databases or multi schemas with keycloak after user registration?

1 Like

I had the same question and did a lot of searching. Ultimately, unless you can write your own SPI (service provider interface) in Java, you are out of luck. If you can, you write an SPI event listener that listens for new user registration, then triggers an external action (create user in app database, etc.).

Our solution instead was to disable registration through Keycloak, and have a separate registration app, which created user in Keycloak and the backend API database; or not at all. Likewise we created an admin CLI that could do the same thing.

I have some base classes for listening to user create events if you’re willing to implement your own event listener:

The example there should be easy to follow:

public class MyUserAddRemove extends UserEventListenerProviderFactory {

  @Override
  public String getId() {
    return "ext-event-myuseraddremove";
  }

  @Override
  UserChangedHandler getUserChangedHandler() {
    return new UserChangedHandler() {
      @Override
      void onUserAdded(KeycloakSession session, RealmModel realm, UserModel user) {
        log.infof("User %s added to Realm %s", user.getUsername(), realm.getName());
      }

      @Override
      void onUserRemoved(KeycloakSession session, RealmModel realm, UserModel user) {
        log.infof("User %s removed from Realm %s", user.getUsername(), realm.getName());
	  }
    };
  }

}
1 Like