# Not really new but how to add username to ID token

**URL:** <https://community.auth0.com/t/not-really-new-but-how-to-add-username-to-id-token/111307>\
**Category:** Get Help\
**Tags:** mfa-api, mfa\
**Created:** [July 14, 2023, 7:11pm UTC](https://community.auth0.com/t/not-really-new-but-how-to-add-username-to-id-token/111307 "2023-07-14T19:11:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![david-gibbs-ig](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/david-gibbs-ig/32/48890_2.png) [@david-gibbs-ig](https://community.auth0.com/u/david-gibbs-ig)\
**Post date:** [July 14, 2023, 7:11pm UTC](https://community.auth0.com/t/not-really-new-but-how-to-add-username-to-id-token/111307/1 "2023-07-14T19:11:48Z")

</div>

There have been queries about this before but could I please have clear and fairly complete example of how to add username to the ID Token in Auth grant flow with PKCE. I think I appreciate why it is not there as a default but this would be helpful to us. I don’t understand the use of namespace in the example answers, I think it’s to prevent collisions on claim name “username” as it is not a standard claim. Is that the case, so that the URL can be something idealised ? Does the namespace need to be a URL or can it be a URI ?

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [July 14, 2023, 10:13pm UTC](https://community.auth0.com/t/not-really-new-but-how-to-add-username-to-id-token/111307/3 "2023-07-14T22:13:00Z")

</div>

Hey there @david-gibbs-ig !

> [@david-gibbs-ig](#):
>
> I don’t understand the use of namespace in the example answers, I think it’s to prevent collisions on claim name “username” as it is not a standard claim. Is that the case, so that the URL can be something idealised ? Does the namespace need to be a URL or can it be a URI ?

`username` isn’t a [restricted claim](https://auth0.com/docs/secure/tokens/json-web-tokens/create-custom-claims#general-restrictions), so you should be able to add it in an ID token without issue - A custom namespace can be a URI or URL but neither are required. For example, the following Post Login action code should add a user’s `username` to their ID token without defining a namespace:

```auto
exports.onExecutePostLogin = async (event, api) => {

  if (event.authorization) {
     api.idToken.setCustomClaim('username', event.user.username)
  }
}

```

Hope this helps to clarify!

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [July 28, 2023, 10:13pm UTC](https://community.auth0.com/t/not-really-new-but-how-to-add-username-to-id-token/111307/4 "2023-07-28T22:13:06Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
