# How to decode and access custom claims from idToken using react-native-auth0 SDK

**URL:** <https://community.auth0.com/t/how-to-decode-and-access-custom-claims-from-idtoken-using-react-native-auth0-sdk/76175>\
**Category:** Get Help\
**Tags:** react-native-auth0, custom-claims, id\_token\
**Created:** [January 11, 2022, 1:56pm UTC](https://community.auth0.com/t/how-to-decode-and-access-custom-claims-from-idtoken-using-react-native-auth0-sdk/76175 "2022-01-11T13:56:55Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jw-85](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/jw-85/32/34324_2.png) [@jw-85](https://community.auth0.com/u/jw-85)\
**Post date:** [January 11, 2022, 1:56pm UTC](https://community.auth0.com/t/how-to-decode-and-access-custom-claims-from-idtoken-using-react-native-auth0-sdk/76175/1 "2022-01-11T13:56:55Z")

</div>

Hi community,

I need a bit of assistance…  
I am trying to decode the idToken returned from `auth0.webAuth.authorize()` using the **rect-native-auth0 SDK** as I have included some custom claims via a Login Action Flow (external user ID and token for 3rd party API).

This is the function that I am calling in my React Native app to log the user in:

```auto
export function Auth0Login() {
    auth0.webAuth
      .authorize({
        scope: "openid email"
      })
      .then(res => {
        console.log('authorise reponse: ' + JSON.stringify(res));
        storeTokens({ token: res.accessToken })
      .catch(error => console.log(JSON.stringify(error)));
};

```

What I would like to do is decode the idToken after authorising, the store the custom claims _externalId_ and _externalToken_ alongside the users Auth0 token using my storeTokens() function (react-native-encrypted-storage library).

I am a bit of a coding novice so please go easy on me 🙂

This is my Login Action for reference, makePostRequestAsync() calls my API with event.user data, creates a user if one doesn’t exist and returns _drftid_ and _drfttoken_ to be added to login claims plus stored in Auth0 app\_metadata:

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

    await makePostRequestAsync(event.user)
        .then(function (res) {
            api.idToken.setCustomClaim('drftid', res.drftid);
            api.idToken.setCustomClaim('drfttoken', res.drfttoken);
            api.user.setAppMetadata('drftid', res.drftid);
            api.user.setAppMetadata('drfttoken', res.drfttoken);
            console.log('Request: ' + JSON.stringify(event.user) + '.Response: ' + JSON.stringify(res));
        });

};

```

---

<div class="post-metadata">

**Author:** ![rueben.tiow](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rueben.tiow/32/11646_2.png) [@rueben.tiow](https://community.auth0.com/u/rueben.tiow)\
**Post date:** [January 11, 2022, 5:29pm UTC](https://community.auth0.com/t/how-to-decode-and-access-custom-claims-from-idtoken-using-react-native-auth0-sdk/76175/3 "2022-01-11T17:29:55Z")

</div>

Hi @jw-85,

Welcome to the Auth0 Community!

I understand you have some questions about decoding your ID Token and Post-Login Action.

First, to [validate your ID tokens](https://auth0.com/docs/secure/tokens/json-web-tokens/validate-json-web-tokens), you have the option to use one of the [JWT.io libraries](https://jwt.io/libraries) to parse and inspect them.

As for your Post-Login Action, I noticed that it still needs a namespace to append your custom claims. In this case, you will need to [create a namespaced custom claim](https://auth0.com/docs/secure/tokens/json-web-tokens/create-namespaced-custom-claims), which can be any non-Auth0 HTTP or HTTPS URL as the namespace identifier.

However, be mindful that Auth0 domains cannot be used as a namespace identifier, which includes

- [auth0.com](http://auth0.com)
- [webtask.io](http://webtask.io)
- webtask.run.

See below.

```auto
exports.onExecutePostLogin = async (event, api) => {
  const namespace = 'https://my-app.example.com';
  if (event.authorization) {
    await makePostRequestAsync(event.user)
        .then(function (res) {
            api.idToken.setCustomClaim(`${namespace}/drftid`, res.drftid);
            api.idToken.setCustomClaim(`${namespace}/drfttoken`, res.drfttoken);
            api.user.setAppMetadata('drftid', res.drftid);
            api.user.setAppMetadata('drfttoken', res.drfttoken);
            console.log('Request: ' + JSON.stringify(event.user) + '.Response: ' + JSON.stringify(res));
        });
  }
};

```

Once this is complete, you can decode your custom claims in the ID token.

Please let me know if you have any questions.

Thanks.

---

<div class="post-metadata">

**Author:** ![jw-85](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/jw-85/32/34324_2.png) [@jw-85](https://community.auth0.com/u/jw-85)\
**Post date:** [January 11, 2022, 8:50pm UTC](https://community.auth0.com/t/how-to-decode-and-access-custom-claims-from-idtoken-using-react-native-auth0-sdk/76175/4 "2022-01-11T20:50:26Z")

</div>

Thank you @rueben.tiow I will give that a go in my React Native project now, is there any benefit to using one [JWT.io](http://JWT.io) libraries over another? I found [jwt-decode - npm (npmjs.com)](https://www.npmjs.com/package/jwt-decode) which looks like its pretty easy to use!

In the mean time though I quickly took a look at my React web project and noticed that `const { user } = useAuth0();` object from the **auth0/auth0-react SDK** returns the claims successfully… don’t suppose you could save me a lot of Googling and help me extract them from the object - the namespace is not playing nicely when trying to set a const?

`{"https://drft.io/id":"e2987fb1-a3d9-4ef2-95e2-ea9f78bc54c6","https://drft.io/token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiZTI5ODdmYjEtYTNkOS00ZWYyLTk1ZTItZWE5Zjc4YmM1NGM2In0.C1WjoDJN8Vh_tjoSJCiH8MQovWqhBp-3zjtXSyXO5u8","nickname":"jon","name":"jxxx@xxx.com","picture":"https://s.gravatar.com/avatar/1d148d017bsc7d872184cdfsd7d59eb9ebee?s=480&r=pg&d=https%3A%2F%2Fcdn.auth0.com%2Favatars%2Fjo.png","updated_at":"2022-01-11T20:16:13.732Z","email":"jxxx@xxx.com","email_verified":false,"sub":"auth0|61dd49a63e9c1c7s9006986bf00"}`

---

<div class="post-metadata">

**Author:** ![jw-85](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/jw-85/32/34324_2.png) [@jw-85](https://community.auth0.com/u/jw-85)\
**Post date:** [January 11, 2022, 8:57pm UTC](https://community.auth0.com/t/how-to-decode-and-access-custom-claims-from-idtoken-using-react-native-auth0-sdk/76175/5 "2022-01-11T20:57:49Z")

</div>

I worked it out (woohoo) 🤓

`user["https://drft.io/id"]`

---

<div class="post-metadata">

**Author:** ![rueben.tiow](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rueben.tiow/32/11646_2.png) [@rueben.tiow](https://community.auth0.com/u/rueben.tiow)\
**Post date:** [January 11, 2022, 9:12pm UTC](https://community.auth0.com/t/how-to-decode-and-access-custom-claims-from-idtoken-using-react-native-auth0-sdk/76175/6 "2022-01-11T21:12:10Z")

</div>

Hi @jw-85,

Thank you for your response.

> [@jw-85](#):
>
> is there any benefit to using one [JWT.io](http://JWT.io) libraries over another?

Not really, you’re encouraged to use any middleware or one of the existing open-source third-party libraries to parse and validate JWTs.

To get the ID token claims using the `useAuth0()` hook, you will need to use the `getIdTokenClaims` method to get the custom claims using the Post-Login Action, as you mentioned earlier. See [https://auth0.github.io/auth0-react/interfaces/auth0\_context.auth0contextinterface.html#getidtokenclaims](https://auth0.github.io/auth0-react/interfaces/auth0_context.auth0contextinterface.html#getidtokenclaims).

> [@jw-85](#):
>
> I worked it out (woohoo) 🤓

I’m glad everything works now!

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

Thank you.

---

<div class="post-metadata">

**Author:** ![jw-85](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/jw-85/32/34324_2.png) [@jw-85](https://community.auth0.com/u/jw-85)\
**Post date:** [January 11, 2022, 11:29pm UTC](https://community.auth0.com/t/how-to-decode-and-access-custom-claims-from-idtoken-using-react-native-auth0-sdk/76175/7 "2022-01-11T23:29:50Z")

</div>

I have just tried that jwt-decode package in my React Native app and I can successfully access the custom id and token claims there too now - thank you for all of your help!

Next on my list is to try and replicate the React isAuthenticated and isLoading states that comes with auth0-react useAuthO hook in React Native so if a person force closes an app and opens it again they aren’t sent back to the logged out page - as far as I am aware, the react-native-auth0 SDK does not have these… is that right @rueben.tiow?

---

<div class="post-metadata">

**Author:** ![rueben.tiow](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rueben.tiow/32/11646_2.png) [@rueben.tiow](https://community.auth0.com/u/rueben.tiow)\
**Post date:** [January 21, 2022, 12:17am UTC](https://community.auth0.com/t/how-to-decode-and-access-custom-claims-from-idtoken-using-react-native-auth0-sdk/76175/8 "2022-01-21T00:17:00Z")

</div>

Hi @jw-85,

Thank you for the updates and I’m glad you were successfully able to decode the token in your app!

> [@jw-85](#):
>
> as far as I am aware, the react-native-auth0 SDK does not have these… is that right @rueben.tiow?

That is correct. The react-native-auth0 SDK only performs the authorization code flow with PKCE.

As mentioned in [this issue](https://github.com/auth0/react-native-auth0/issues/311), it is generally recommended and best practice to keep the user’s access token stored securely and use it while it’s valid (unexpired/unrevoked) to determine if the user’s session is active. If it becomes invalid, you have the option of renewing the session by requesting a new access token using a previously obtained refresh token.

I hope this answers your questions and concerns.

Thanks.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [February 5, 2022, 12:17am UTC](https://community.auth0.com/t/how-to-decode-and-access-custom-claims-from-idtoken-using-react-native-auth0-sdk/76175/9 "2022-02-05T00:17:44Z")

</div>

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