[keycloak-user] KeycloakSession users() returns UserRepresentation with no name after REGISTER event

Matija Mazi matija.mazi at gmail.com
Tue Jul 4 14:10:53 EDT 2017


Hi,

I'm using an EventListenerProvider to catch user registration events (when
the user registers via Keycloak's Registration Form) and send the
registered user's details to an outside system.

Here's the relevant code:

public class LocoinsEventListenerProvider implements EventListenerProvider {
    private final KeycloakSession session; // provided in constructor

@Override public void onEvent(Event evt) {
    if (evt.getType() == EventType.REGISTER &&
Objects.equals(evt.getRealmId(), this.realm)) {
        final String userId = evt.getUserId();
        final RealmModel realm = session.realms().getRealm(evt.getRealmId());

final UserModel user = session.users().getUserById(userId, realm);
log.info("Customer registered, notifying application: id = {}, name = {}
{}, email = {}", userId, user.getFirstName(), user.getLastName(),
user.getEmail());
} } }

The problem is that user.getFirstName(), user.getLastName() are both
null (but user.getEmail() holds the correct value).

If I check the Keycloak database directly after this (table
USER_ENTITY), FIRST_NAME and LAST_NAME are both there.

Any ideas why this might be? How should I get what the first and last
name entered by the users?

Thanks!

P.S. If I create the user via the Keycloak REST API client and then
catch the corresponding Admin event (I don't get a REGISTER event in
this case),
the UserRepresentation obtained in the same way as above holds all the
data (first, last name etc.):

final UserRepresentation user = new UserRepresentation();
user.setUsername(email);
user.setEmail(email);
user.setFirstName(name);

kc.realm(*"MyRealm"*).users().create(user);


More information about the keycloak-user mailing list