Fetching user's username and printing to web app

Hey, I’m unable to print a given user’s username onto the page, similar to something like printing the user’s email or nickname with {user.nickname}. Upon further the user’s username is not listed in the /api/auth/me response, and login via username is confirmed to be on and working. I have also read the API scope docs but that has not proved useful as the username is not considered to be part of the user metadata, as listed in the raw JSON output on auth0’s console. So, any ideas how to make the username appear + print it on a NextJS web app? Thanks :slight_smile:

Hey there @horsaen welcome to the community!

The user’s username should be able to be accessed at user.name which I would expect to be included in the /api/auth/me response as well as by using userUser() hook as outlined in our Nextjs quickstart. The sample app has an example of using the useUser() hook:

Hope this helps!

1 Like

Hey !! thank you and thanks for your response !! :slight_smile:

Unfortunately I have already tried this, and while the name is accessible from the /me path, it is not quite what I had hoped for, here’s what is listed in the /me response:

{
"nickname":"atealltheglue",
"name":"atealltheglue@*.com",
"picture":"https://s.gravatar.com/avatar/42bc28341cd33b6740463a25780f5df3?s=480&r=pg&d=https%3A%2F%2Fcdn.auth0.com%2Favatars%2Fat.png",
"updated_at":"2022-11-28T20:33:10.297Z",
"email":"atealltheglue@*.com",
"email_verified":false,
"sub":"xxx",
"sid":"xxx"
}

and this is what is show in the account’s raw JSON file

{
    "created_at": "2022-11-28T20:33:10.297Z",
    "email": "atealltheglue@*.com",
    "email_verified": false,
    "identities": [
        {
            "connection": "Username-Password-Authentication",
            "provider": "auth0",
            "user_id": "63851b06802804f3214d0da7",
            "isSocial": false
        }
    ],
    "name": "atealltheglue@*.com",
    "nickname": "atealltheglue",
    "picture": "https://s.gravatar.com/avatar/42bc28341cd33b6740463a25780f5df3?s=480&r=pg&d=https%3A%2F%2Fcdn.auth0.com%2Favatars%2Fat.png",
    "updated_at": "2022-11-28T21:39:27.462Z",
    "user_id": "auth0|63851b06802804f3214d0da7",
    "username": "horsaen",
    "user_metadata": {
        "sus": true
    },
    "last_ip": "*",
    "last_login": "2022-11-28T20:33:10.291Z",
    "logins_count": 1,
    "blocked_for": [],
    "guardian_authenticators": []
}

my main goal is to somehow access the ‘horsaen’ value, which is not present in the /me response mentioned above, also please note i did try adding a value for the user metadata in an effort to make sure that the username would not be found there ^w^, ignore the actual values :stuck_out_tongue:

so any ideas ? :o

1 Like

Ahh OK gotcha! That makes more sense now, thanks for further explanation :slight_smile:

It should be as simple as adding a custom username claim to the ID Token with a Post Login Action, like:

exports.onExecutePostLogin = async (event, api) => {
    if (event.authorization) {
      api.idToken.setCustomClaim('username', event.user.username);
   }
}

I’m testing in the sample app and after adding the username as a custom claim it is returned in the user profile by the useUser() hook.

Let us know! :crossed_fingers:

1 Like

oohh so the action certainly works and when running it a proper username is returned, however i still get nothing in the /me response :o, did i do something wrong ?

Hmm and you have added the action to Actions → Flows → Login in your Auth0 Dashboard?

Assuming that Action has been deployed and added to the login flow, you should be getting the username in the profile :thinking:

1 Like



is this correct ? :o, still nothing sadly

That looks correct to me!

Have you tried setting up the sample app keyed to the same web app? I’m curious if the profile information is pulled in correctly there - While it uses the hook rather than auth/me the data should be the same.

1 Like

emmm i have no idea what we did but after firing up the sample app and then running the original web app everything works as intended, weird :o. thanks a ton, i really appreciate all the help :))

No problem, happy to help :slight_smile: Good to know it’s functioning as expected for you now!

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