# Api authentication with credentials from a react-native app

**URL:** <https://community.auth0.com/t/api-authentication-with-credentials-from-a-react-native-app/114290>\
**Category:** Get Help\
**Tags:** react-native-auth0, sdks-quickstarts\
**Created:** [August 23, 2023, 5:39am UTC](https://community.auth0.com/t/api-authentication-with-credentials-from-a-react-native-app/114290 "2023-08-23T05:39:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![davitoc](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/davitoc/32/44253_2.png) [@davitoc](https://community.auth0.com/u/davitoc)\
**Post date:** [August 23, 2023, 5:39am UTC](https://community.auth0.com/t/api-authentication-with-credentials-from-a-react-native-app/114290/1 "2023-08-23T05:39:34Z")

</div>

Hi there,

I have a react-native app and I’m using the react-native-auth0 SDK to login a user and get credentials when the login is successful.

```auto
const onLogin = async (callback) => {
    try {
      await authorize().then(async () =>{
        const credentials = await getCredentials();
        await AsyncStorage.setItem('token', credentials.idToken);
        if (callback) {
            callback(); // Call the callback function if provided
          }
      });
    } catch (e) {
      console.log(e);
    }
  };

```

How I expect to use that token is adding it as an auth bearer to all requests to the API, and verifying the token in a NodeJS backend using the library jsonwebtoken.

```auto
module.exports = function(req, res, next) {
    const token = req.header("authorization").replace('Bearer ','');
    if (!token) return res.status(401).json({ message: "Auth Error" });
  
    try {
      var cert = fs.readFileSync(path.resolve(__dirname, '.public key')); // get public key
      const decoded = jwt.verify(token, cert);
      req.user = decoded.user;
      next();
    } catch (e) {
      console.error(e);
      res.status(500).send({ message: "Invalid Token" });
    }
  };

```

If I use the idToken the JWT verification works as expected and I can decode the JWT and access user info. The reason why I’m not sure if my approach is correct is because [this page](https://auth0.com/docs/secure/tokens/id-tokens) says that you should never use Id tokens to obtain direct access to APIs, however, if I use credentials.accessToken the verification fails with a “malformed JWT error”.

Any guidance on the topic is appreciated.

Cheers,

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [August 24, 2023, 12:44am UTC](https://community.auth0.com/t/api-authentication-with-credentials-from-a-react-native-app/114290/3 "2023-08-24T00:44:06Z")

</div>

Hey there @davitoc welcome to the community!

> [@davitoc](#):
>
> If I use the idToken the JWT verification works as expected and I can decode the JWT and access user info. The reason why I’m not sure if my approach is correct is because [this page](https://auth0.com/docs/secure/tokens/id-tokens) says that you should never use Id tokens to obtain direct access to APIs, however, if I use credentials.accessToken the verification fails with a “malformed JWT error”.

That’s correct, you shouldn’t use an ID token against an API but rather an access token - Are you passing an [audience](https://auth0.github.io/react-native-auth0/interfaces/WebAuthorizeParameters.html#audience) param anywhere when initiating `WebAuth`?

Let us know!

---

<div class="post-metadata">

**Author:** ![davitoc](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/davitoc/32/44253_2.png) [@davitoc](https://community.auth0.com/u/davitoc)\
**Post date:** [August 30, 2023, 10:19pm UTC](https://community.auth0.com/t/api-authentication-with-credentials-from-a-react-native-app/114290/4 "2023-08-30T22:19:18Z")

</div>

Hi Tyf,

Thanks for bringing that up. I wasn’t passing an audience param when initiating `WebAuth`.

I have done so now in addition to follow up a guide for the [Mobile + API](https://auth0.com/docs/get-started/architecture-scenarios/mobile-api) implementation.

```auto
  const onLogin = async (callback) => {
    const authorizeConfig ={
      issuer: 'API issuer,
      audience:'API audience'
    };

    try {
      await authorize(authorizeConfig).then(async () =>{
        const credentials = await getCredentials();

        await AsyncStorage.setItem('token', credentials.accessToken);
        if (callback) {
            callback(); // Call the callback function if provided
          }
      });
    } catch (e) {
      console.log(e);
    }
  };

```

Got it working 🙌

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [August 30, 2023, 11:18pm UTC](https://community.auth0.com/t/api-authentication-with-credentials-from-a-react-native-app/114290/5 "2023-08-30T23:18:20Z")

</div>

Hey @davitoc that’s great news thanks for sharing here! 🚀

---

<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:** [September 13, 2023, 11:18pm UTC](https://community.auth0.com/t/api-authentication-with-credentials-from-a-react-native-app/114290/6 "2023-09-13T23:18:53Z")

</div>

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