[solved] There is a way for remove required username when register an user?

Hello,

I am looking for a way for remove username (not required) when register or even login the user.

My idea is that my “custom application” send a post request to keycloak admin-rest api.

< POST /auth/admin/realms/mylab/users/ HTTP/1.1
< Host: localhost:8080
< User-Agent: python-requests/2.25.1
< Accept-Encoding: gzip, deflate
< Accept: */*
< Connection: keep-alive
< Content-type: application/json
< Authorization: Bearer eyJhbG......etc
< Content-Length: 97
< 
< {"enabled": true, "emailVerified": true, "realmRoles": ["Customer"], "email": "teste1@gmail.com"}
> HTTP/1.1 400 Bad Request
> X-XSS-Protection: 1; mode=block
> X-Frame-Options: SAMEORIGIN
> Referrer-Policy: no-referrer
> Date: Wed, 26 May 2021 18:27:47 GMT
> Connection: keep-alive
> Strict-Transport-Security: max-age=31536000; includeSubDomains
> X-Content-Type-Options: nosniff
> Content-Type: application/json
> Content-Length: 39
> 
{"errorMessage":"User name is missing"}

To solve it I just created a random username with uuid4() :

payload = {
            "username": str(uuid.uuid4()),
            "enabled": True,
            "emailVerified": True,
            "realmRoles": ["Customer"]
        }

But there is a way to do that in some admin dashboard?

Hello @lucassimon
In Keycloak username is a mandatory field , so you cannot simply remove it. As an IAM solution it primarily relies on username for a user to be able to login or perform other actions.
However, Keycloak has another option wherein you can make email to be used as the default username for the user.

Once you enable this setting, you will be able to create the user with the details you mentioned above.
NOTE: Changing Email as Username is an irreversible change for a Realm as far as I know. So proceed with caution.

Also note that ,though you will be able to create a new user this way it’s also possible to completely customise the login flow as per your requirements with the help of a custom SPI you can create your own flow and set it as default flow for login.
That’s a topic for another discussion altogether.

1 Like