4

I recently implemented user authentication in my react web app using Auth0. And I need to retrieve some information from user object. I did this so far:

cost { user, isAuthenticated } = useAuth0();
<p>{user.name}</p>

But I also want to get user's id, so I can reference them later in my backend. So I did this:

const { user, isAuthenticated } = useAuth0();
<p>{user.user_id}</p>

And when I go to the website I get undefined instead of the user's id.

Is it happening because I use dev-keys for my social connections, or maybe it has to be done differently?

10
  • I assume useAuth0 is a function that needs to be called useAuth0()? useauth.dev/docs/auth0 Commented Mar 16, 2021 at 12:02
  • Yeah, that's right, it was my fault writing this question, but in my code it's passed correctly, so it's not case. Commented Mar 16, 2021 at 12:27
  • Did you try console.log(user)? Commented Mar 16, 2021 at 12:47
  • can this help you? Show User Profile Information Commented Mar 16, 2021 at 12:49
  • Yes, and there is no user_id parameter, but in auth0 docs they say that you can retrieve it from there. Commented Mar 16, 2021 at 12:52

2 Answers 2

7

Not sure if you are still searching for this problem, however I had a similar problem and found this as a solution:

const { user, isAuthenticated } = useAuth0();
<p>{user.sub}</p>

source: https://community.auth0.com/t/how-to-get-user-id-of-a-user-after-login-in-react-hook-useauth0/53309

Sign up to request clarification or add additional context in comments.

3 Comments

I’m sorry, but the asker has already done this in the question.
I don't see where? I might miss a piece of information but asker only tried user.user_id and not 'user.sub'
@JakubKlimek Can you confirm if this answer helps you?
0

It should be the "sub" key. Discord id is 18 characters long. The results prefix it with oauth2|discord|. Which you can truncate out with a function.

const { user, isAuthenticated } = useAuth0();
console.log(user.sub);

The results should be something like:

oauth2|discord|111111111111111111

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.