# UserRegistrationProvider addUser get firsname, lastname and email

**URL:** <https://forum.keycloak.org/t/userregistrationprovider-adduser-get-firsname-lastname-and-email/7417>\
**Category:** Extending the server\
**Created:** [February 16, 2021, 12:36pm UTC](https://forum.keycloak.org/t/userregistrationprovider-adduser-get-firsname-lastname-and-email/7417 "2021-02-16T12:36:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![cidudp](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/cidudp/32/2402_2.png) [@cidudp](https://forum.keycloak.org/u/cidudp)\
**Post date:** [February 16, 2021, 12:36pm UTC](https://forum.keycloak.org/t/userregistrationprovider-adduser-get-firsname-lastname-and-email/7417/1 "2021-02-16T12:36:11Z")

</div>

In SPI UserStorageProvider the addUser function only has the username parameter, how do I get the first name, last name and email?

---

<div class="post-metadata">

**Author:** ![xgp](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/xgp/32/2589_2.png) [@xgp](https://forum.keycloak.org/u/xgp)\
**Post date:** [February 16, 2021, 6:08pm UTC](https://forum.keycloak.org/t/userregistrationprovider-adduser-get-firsname-lastname-and-email/7417/2 "2021-02-16T18:08:46Z")

</div>

Can you share your code, and what you’re trying to accomplish? There is no `addUser` method in `UserStorageProvider`. Do you mean the one in `UserProvider`/`UserRegistrationProvider`?

If you are trying to create a new user, you can call the `addUser(RealmModel realm, String username)` method, and that will return an UserModel. Then you can call the `setXxx()` methods on that, and those values will be automatically persisted.

---

<div class="post-metadata">

**Author:** ![cidudp](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/cidudp/32/2402_2.png) [@cidudp](https://forum.keycloak.org/u/cidudp)\
**Post date:** [February 16, 2021, 9:15pm UTC](https://forum.keycloak.org/t/userregistrationprovider-adduser-get-firsname-lastname-and-email/7417/3 "2021-02-16T21:15:14Z")

</div>

```
@Override
    public UserModel addUser(RealmModel rm, String username) {
        
        Logger.getLogger(CustomUserStorageProvider.class).info("addUser: realm={" + rm.getName() + "} username={" + username + "}");

        var tenant = this.db.session().createNamedQuery("Tenant.findByAliveName", Tenant.class).setParameter(1, rm.getName()).uniqueResult();
        if (tenant == null) {
            return null;
        }

        var account = new Account();

        account.setAlive(Boolean.TRUE);
        account.setStatId(Status.ENABLED);
        account.setCreationTime(Calendar.getInstance());
        account.setUsername(username);
        account.setTenaId(tenant.getTenaId());

        this.db.begin();

        var id = this.db.save(account);

        this.db.commit();

        return this.getUserById(StorageId.keycloakId(this.model, id.toString()), rm);
    }
```

---

<div class="post-metadata">

**Author:** ![xgp](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/xgp/32/2589_2.png) [@xgp](https://forum.keycloak.org/u/xgp)\
**Post date:** [February 16, 2021, 9:59pm UTC](https://forum.keycloak.org/t/userregistrationprovider-adduser-get-firsname-lastname-and-email/7417/4 "2021-02-16T21:59:37Z")

</div>

> [@cidudp](#):
>
> ```auto
> return this.getUserById(StorageId.keycloakId(this.model, id.toString()), rm);
> 
> ```

This returns the `UserModel`. You can get the first name, last name and email from there.

---

<div class="post-metadata">

**Author:** ![cidudp](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/cidudp/32/2402_2.png) [@cidudp](https://forum.keycloak.org/u/cidudp)\
**Post date:** [February 16, 2021, 10:19pm UTC](https://forum.keycloak.org/t/userregistrationprovider-adduser-get-firsname-lastname-and-email/7417/5 "2021-02-16T22:19:56Z")

</div>

This method is also overwritten and query on the record entered previously

@Override  
public UserModel getUserById(String id, RealmModel rm) {

```
        Logger.getLogger(CustomUserStorageProvider.class).info("getUserById: realm={" + rm.getName() + "} id={" + id + "}");

        var account = this.db.session().byId(Account.class).load(Integer.parseInt(externalId(id)));
        if (account == null) {
            return null;
        }

        return new UsuarioAdapter(account, this.db.session().createNamedQuery("Allow.findByAccoId", Allow.class).setParameter(1, account.getAccoId()).list(), this.session, rm, this.model);
    }
```

---

<div class="post-metadata">

**Author:** ![vijender9423](https://avatars.discourse-cdn.com/v4/letter/v/51bf81/32.png) [@vijender9423](https://forum.keycloak.org/u/vijender9423)\
**Post date:** [February 10, 2023, 11:34am UTC](https://forum.keycloak.org/t/userregistrationprovider-adduser-get-firsname-lastname-and-email/7417/6 "2023-02-10T11:34:24Z")

</div>

@xgp will this return the firstname, lastname and email info sent by provider in case i am using external social IDP like google .  
I think in above case it will only return the info from the custom userStorageProvider and not what we will get from IDP .
