Not 100% sure, but I think that you're right and firstName and lastName
are not available in the REGISTER event. That's because REGISTER event
is triggered too early - at the stage when userModel doesn't yet have
firstName and lastName properly set on it.
I think there are some workarounds - for example there is LOGIN event,
which is send right after the REGISTER. You can track LOGIN events,
which were sent right after the REGISTER events. I think that firstName,
lastName should be available in those LOGIN events.
Marek
On 09/01/18 11:01, Thilo Frotscher wrote:
Hi all,
In our project there is a requirement to execute some actions after successful
user registrations. I implemented an EventListenerProvider that listens to events
of type REGISTER. The details of this event type only contain the "username"
of
the user that just registered, but first name and last name are missing.
So I thought I could retrieve this information from the user storage. But no
matter how I try to read the user information from the user storage, firstName
and lastName are always null.
Is this a bug or a feature? When manually logging on to the Admin Console,
I can see that firstName and lastName have been correctly saved. But how
can I programmatically retrieve the first name and last name of the user
that just registered in my event listener?
Sample code:
public void onEvent(Event event) {
if (!EventType.REGISTER.equals(event.getType())) {
LOGGER.info("Ignoring event of type " + event.getType());
return;
}
String realmId = event.getRealmId();
RealmModel realm = session.realms().getRealm(realmId);
String userId = event.getUserId();
Map<String, String> details = event.getDetails();
String username = details.get("username");
printUser(session.users().getUserByUsername(username, realm));
printUser(session.userLocalStorage().getUserByUsername(username, realm));
printUser(session.userCache().getUserByUsername(username, realm));
printUser(session.userStorageManager().getUserByUsername(username, realm));
}
private void printUser(UserModel user) {
if (user==null) {
LOGGER.info("User is null");
} else {
LOGGER.info(user.getFirstName()); // always null
LOGGER.info(user.getLastName()); // always null
LOGGER.info(user.getId());
LOGGER.info(user.getEmail());
LOGGER.info(user.getUsername());
}
}
Actually, I believe firstName and lastName should be part of the event details
in the first place...
Thanks for your help!
Cheers,
Thilo
_______________________________________________
keycloak-user mailing list
keycloak-user(a)lists.jboss.org
https://lists.jboss.org/mailman/listinfo/keycloak-user