How to get login_count in useUser object in Next.js

Hello there ! I somehow cannot get access to user_metadata, or login_count inside my user object from useUser hook. I’ve been looking at multiple guides, and tutorials, and I don’t really understand what is going on. Can you guys please help me with some simple steps for accessing login_count data in my Next.js application ? I’ve already seen topic which were setting up rules, or actions but none of them were working, and I didn’t get any other data in the user object.

Hi @fuco312,

Welcome to the Auth0 Community!

The useUser hook from the Next.js SDK only returns the following user profile attributes:

  • email_verified?
  • name?
  • nickname?
  • org_id?
  • picture?
  • sub?
  • updated_at?

(Reference: UserProfile | @auth0/nextjs-auth0)

For getting the logins_count for each user, I recommend using the Management API Get a User endpoint with the fields parameter to filter for the logins_count attribute.

Here is an example request:

curl --location -g --request GET 'https://{{auth0_domain}}/api/v2/users/:id?fields=logins_count' \
--header 'Authorization: Bearer {{auth0_token}}'

Alternatively, you could append the user’s logins_count as a custom claim to your token using an Auth0 Post Login Action. Then after login, you can decode the token and find the logins_count claim. See our Adding custom claims to tokens FAQ to learn more.

I hope this helps!

Please let me know if there’s anything else I can do to help.

Thanks,
Rueben

1 Like

Hello @rueben.tiow thank you for replying, I’m trying to create the get request, but I’m not exactly sure where can I get the token from. My request is currently looking like this. The token is a value I’ve got from postman, but I’m not sure if it’s the right token. My results are bad request 400 - Bad HTTP authentication header format

Hi @fuco312

Thank you for your response.

There are a couple of ways to get an Access Token for the Management API, namely, in the Auth0 Dashboard or calling the Management API using the client credentials flow.

Firstly, I recommend going to your Auth0 Dashboard > Applications > APIs and selecting your Auth0 Management API.

In the settings page, click on the API Explorer tab and if setup already, you should be able to copy the token directly from that page:

Alternatively, you can programmatically get an access token by using the Client Credentials flow. For this, please click on the Test tab of your Management API, select your programming language, and copy the corresponding code.

Then once you have gotten your token, you can use it to query the Management API’s Get a User endpoint.

Reference:

I hope the explanation is clear!

Please let me know if you have any additional questions. I’d be happy to help.

Thanks,
Rueben

1 Like

Thank you very much @rueben.tiow , I think that this has explained everything perfectly even to newbie like me ! Once again thank you very much for your time :slight_smile: Have a nice weekend !

1 Like

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