Quick question about phone number attribute

If I am not using a sms connection, but I am instead using a social login like google. I am unable to change the root phone number attribute correct? My best option would to be to use the user meta data correct?

Hey there @rodyli123 !

I believe this is correct for Google federated users.

Yes, you can certainly store a phone number attribute is user metadata - Further to this, you can easily access this in Actions (event.user.user_metadata) should you wish to include it as a custom claim in tokens.

From my SPA I am able to access user.name and other root attributes. Do I have to make a call to my backend to get the user meta data or am I able to some how do user.metadata.phonenumber?

1 Like

If you add the metadata as a custom claim to ID tokens you I believe you should be able to access from your SPA same as you’d access user.name. An Action might look like:

exports.onExecutePostLogin = async (event, api) => {

  const namespace = 'https://example.com/'; // Use a custom namespace to avoid conflicts with standard claims
  const userMetadata = event.user.user_metadata || {};

  // Add user metadata to ID token
  api.idToken.setCustomClaim(`${namespace}user_metadata`, userMetadata);

  // Add user metadata to Access token
  api.accessToken.setCustomClaim(`${namespace}user_metadata`, userMetadata);
};

This code adds a custom claim to both ID and access tokens which may not be necessary for your use case.

Thank you so much. The only part I’m confused on is how to extract the phone number. Usually I can do user.email and etc to access information about the user. This is what the decoded ID token, I believe, looks like:
{
“https://getphoneuser_metadata”: {
“phoneNumber”: “+13013012400”
},
“given_name”: “blah”,
“family_name”: “blah”,
“nickname”: “blah”,
“name”: “blah”,
“picture”: “blah”,
“updated_at”: “blah”,
“email”: “blah”,
“email_verified”: blah,
“sub”: “blah”
}
But how would I be able to access the phone number would it be user.https://getphoneuser_metadata.phoneNumber?

The work around I think I will mostly likely implement is just decode the ID token and extract it from there in some other function.

Again thanks a lot.

Hey @rodyli123 ! No problem, happy to help :slight_smile:

This is exactly how you can access the phone number from you app. For example, if using auth0-react you can use the useAuth0 hook user Auth state. You can also simplify the metadata by using a non-namespaced custom claim if you’d like.

I have two more follow ups, sorry for the questions.

  1. Originally I had user.https://getphoneuser_metadata.phoneNumber, but it seems like the jsx didn’t like it. I got this error: Unexpected token, expected “}”
    {user.https://getphoneuser_metadata.phoneNumber}

However, I then changed the namespace to getphoneuser and user.getphoneuser.phoneNumber is working. Any idea on how to get it to work with https://getphoneuser_metadata as the namespace?

  1. When I changed the namespace to getphoneuser, is that an example of non-namespace custom claim?

No worries!

{user["https://getphoneuser_metadata"]["phone"]} seems to work for me.

That is correct!

Thanks ty this worked ! :slight_smile:

1 Like

Awesome! Thanks for confirming :slight_smile:

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