[keycloak-user] REGISTER event: firstName & lastName always NULL

Thilo Frotscher keycloak at frotscher.com
Fri Jan 12 10:18:42 EST 2018


Thanks for your reply. Listening to LOGIN events was not an option for us. There are simply
too many of those LOGIN events, not just the ones following a REGISTER event. It would have
been too complex to find the right LOGIN event among all the others. Also, we would have had
to maintain some kind of state in the listener.

At the end we solved the issue by notifying our application about the REGISTER event and
including dummy values for firstName and lastName. These dummy values are stored in our
application temporarily until the newly registered user logs in to our application for the
first time. Upon logging in an access token will be passed to the application that contains
the correctly populated firstName and lastName. So we just compare the user details contained
in the access token with the user details in our application's database, and update the
database if there is any difference.

An obvious concern with this solution is that we do this check for every request we receive.
But it does not actually cause any significant additional load, not even an additional database
query, as we access for database for every incoming request anyway.

Thilo



Am 09.01.2018 um 21:47 schrieb Marek Posolda:
> 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 at lists.jboss.org
>> https://lists.jboss.org/mailman/listinfo/keycloak-user
> 
> 
> 


More information about the keycloak-user mailing list