Trying to get a unique ID for authenticated users

Hi,

I have a web app and I’m using Okta for login & signup and then storing extra profile details in my local database in a users table.

Now I need to get the user’s unique ID from Okta in order to link it to a unique record in my user’s table.

So what I thought was unique was the .sub field when you call

this.props.auth.getUser()

But this turns out to be not the case, so looking thru the documentation if you do:

const accessToken = await this.props.auth.getAccessToken();
const userinfo = await this.props.auth.getUser(accessToken);

Then doing the getUser call while giving it your accessToken it retrieves the unique id,

Though it still returns it in the sub attribute of the userinfo object.

Am I right?

Really hope this is the case?

Yes, that is correct.
The sub claim for a user returned from the userInfo endpoint returns a unique ID for a user in your Okta org. You can be assured that no other user in your org will have the same id.

You can verify this by calling the “Get user by ID” API by passing the sub field obtained from the user.
https://developer.okta.com/docs/api/resources/users#get-user-with-id

1 Like

this.props.auth.getUser() does what you’re suggesting (pulls out the accessToken, then makes the request). this.props.auth.getUser() doesn’t take any arguments.

The sub field is unique, so a simple this.props.auth.getUser() should return what you expect.

Thanks, thought it should.

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