Storing and updating user via custom user storage provider

Hello, I have problems understanding how to save and update an external user (via REST API).
More specifically I do not understand why the addUser method of only takes the RealmModel and a string being the username as parameter and not a complete user model with first name, last name etc.

How can I store these details that are present in the keycloak form to create a user to the external backend?

In the same line of thought: why is there no “modifyUser” or similar method, allowing to update those details if needed?

The logic in the UserStorageProvider for creating and updating users is highly misunderstood…

The addUser() just returns an instance of your own, custom user adapter class, usually some implementation of UserModel interface, probably an implementation of AbstractUserAdapterFederatedStorage or AbstractUserAdapter. If it does return null, the user won’t be stored with your storage provider.

Then, the setter methods in your user adapter are responsible for setting the proper data to the object itself, no matter if the user was just added/created or modified.
When committing the Keycloak session/transaction, you can write the data to your API.

See for example the FlintstoneUserAdapter and FlintstonesUserStorageProvider from my example repository. For updating the user through an API, I’m also using a custom KeycloakTransaction implementation to have better control over the lifecycle, because the implementation in Keycloak was not designed to use it via some API.

1 Like

I understand now! Thnx a lot.

1 Like