Jakub Klimek
Jakub Klimek

Reputation: 593

How to get user's id from auth0 in React

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?

Upvotes: 4

Views: 3113

Answers (2)

Nghi Pham
Nghi Pham

Reputation: 1

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

Upvotes: 0

DeBelg
DeBelg

Reputation: 86

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

Upvotes: 7

Related Questions