# Call NextJS Auth0 protected API from React Native app

**URL:** <https://community.auth0.com/t/call-nextjs-auth0-protected-api-from-react-native-app/128384>\
**Category:** Get Help\
**Tags:** authentication-sessions, sessions\
**Created:** [February 26, 2024, 11:31pm UTC](https://community.auth0.com/t/call-nextjs-auth0-protected-api-from-react-native-app/128384 "2024-02-26T23:31:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Outbound5070](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@Outbound5070](https://community.auth0.com/u/Outbound5070)\
**Post date:** [February 26, 2024, 11:31pm UTC](https://community.auth0.com/t/call-nextjs-auth0-protected-api-from-react-native-app/128384/1 "2024-02-26T23:31:54Z")

</div>

I have a NextJS app that’s protected by the `withMiddlewareAuthRequired` function from the `@auth0/nextjs-auth0/edge` package. The `middleware.js` file looks like this.

```auto
import { withMiddlewareAuthRequired } from '@auth0/nextjs-auth0/edge'

export default withMiddlewareAuthRequired()

```

I want to make calls to the server actions of this app from a React Native app that uses the `react-native-auth0` package.

I’ve tried making the call by adding the `idToken` to the Authorization header.

And I’ve tried manually constructing the cookie based on what I see when I inspect the API calls to the server actions in the browser network tab.

```auto
const credentials = await auth0.credentialsManager.getCredentials()
const cookie = `appSession=${credentials.idToken}`

const res = fetch(nextJsServerActionUri, {
  headers: {
    Cookie: cookie,
  },
})

const res = fetch(nextJsServerActionUri, {
  headers: {
    Authorization: `Bearer ${credentials.idToken}`,
    'Content-Type': 'application/json',
  },
})

```

Neither of these is working. I’m getting 401 unauthorized responses.

The call succeeds if I copy the cookie directly from the network tab in the browser console when making a call to the NextJS server action, and paste it into the `cookie` variable in the code above.

It seems the cookie I’m trying to build is missing something. There are a lot of values that I’m not sure where they’re coming from.

Am I on the right track?

---

<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:** [February 27, 2024, 12:08am UTC](https://community.auth0.com/t/call-nextjs-auth0-protected-api-from-react-native-app/128384/3 "2024-02-27T00:08:02Z")

</div>

Hey there @Outbound5070 welcome to the community!

> [@Outbound5070](#):
>
> I want to make calls to the server actions of this app from a React Native app that uses the `react-native-auth0` package.
> 
> I’ve tried making the call by adding the `idToken` to the Authorization header.

I’m unfortunately not familiar with using `nextjs-auth0` to protect an API in this way, but the standard approach in all our [backend SDKs](https://auth0.com/docs/libraries#backend) is to pass an _access token_ and verify it as opposed to an ID token. Here are some quickstarts which may be helpful:

> **[Auth0 Backend/API Quickstarts](https://auth0.com/docs/quickstart/backend)**
>
> Browse backend/api quickstarts to learn how to quickly add authentication to your app.

---

<div class="post-metadata">

**Author:** ![Outbound5070](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@Outbound5070](https://community.auth0.com/u/Outbound5070)\
**Post date:** [February 27, 2024, 3:45pm UTC](https://community.auth0.com/t/call-nextjs-auth0-protected-api-from-react-native-app/128384/4 "2024-02-27T15:45:02Z")

</div>

Hey @ty.frith thank you for getting back to me so quickly.

I’ve tried adding every value I get from `auth0.credentialsManager.getCredentials()` to the `Authorization` header and none of them work.

The NextJS endpoint is using a cookie to authenticate.

It has many values. Including:

```auto
_xsrf=
username-localhost-8888=
appSession=

```

Is there a way to find out how those cookie values are generated so I can create my own?

---

<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:** [February 29, 2024, 12:18am UTC](https://community.auth0.com/t/call-nextjs-auth0-protected-api-from-react-native-app/128384/5 "2024-02-29T00:18:42Z")

</div>

No problem, happy to help where I can!

Please see my response to your other topic:

> [@How does the auth0-nextjs withMiddlewareAuthRequired function work?](https://community.auth0.com/t/how-does-the-auth0-nextjs-withmiddlewareauthrequired-function-work/128535/3):
>
> Hey again @Outbound5070 ! As far as I can tell what you are looking for isn’t supported:

As far as I can tell the `nextjs-auth0` SDK isn’t going to play nicely with `react-native-auth0` - `credentialsManager` deals with tokens only.

---

<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:** [March 14, 2024, 12:19am UTC](https://community.auth0.com/t/call-nextjs-auth0-protected-api-from-react-native-app/128384/6 "2024-03-14T00:19:21Z")

</div>

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