# Sending userID from JWT token to Backend from NextJS app

**URL:** <https://community.auth0.com/t/sending-userid-from-jwt-token-to-backend-from-nextjs-app/117601>\
**Category:** Get Help\
**Tags:** apis, application\
**Created:** [October 2, 2023, 9:32pm UTC](https://community.auth0.com/t/sending-userid-from-jwt-token-to-backend-from-nextjs-app/117601 "2023-10-02T21:32:24Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![virajvaitha1995](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/virajvaitha1995/32/51319_2.png) [@virajvaitha1995](https://community.auth0.com/u/virajvaitha1995)\
**Post date:** [October 2, 2023, 9:32pm UTC](https://community.auth0.com/t/sending-userid-from-jwt-token-to-backend-from-nextjs-app/117601/1 "2023-10-02T21:32:24Z")

</div>

Hi,

I wanted to send the user\_id to my backend , and believe the most secure way to do this is via the JWT token that is deserialised to retrieve ‘sub’ field.

The documentation isnt clear enough for my uneducated self unfortunately.

Detailed requirements:

From my NextJS web app users can upload videos, and params, which are sent to my backend (fastapi endpoint to sent data to sql/blob storage)

My endpoint uploads videos to a storage account, and i want t use the user\_id to map and determine where it is stored.

For this reason, i wanted to basically say, pass the jwt token in the header of the response.

Please can someone help me.

---

<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:** [October 2, 2023, 10:22pm UTC](https://community.auth0.com/t/sending-userid-from-jwt-token-to-backend-from-nextjs-app/117601/3 "2023-10-02T22:22:42Z")

</div>

Hey @virajvaitha1995 welcome to the community!

I assume you are using [nextjs-auth0](https://github.com/auth0/nextjs-auth0/tree/main)? If so, you probably want to take a look at the following example which outlines how to access an external API from an API route:

> <https://github.com/auth0/nextjs-auth0/blob/main/EXAMPLES.md#access-an-external-api-from-an-api-route>

---

<div class="post-metadata">

**Author:** ![virajvaitha1995](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/virajvaitha1995/32/51319_2.png) [@virajvaitha1995](https://community.auth0.com/u/virajvaitha1995)\
**Post date:** [October 3, 2023, 8:45pm UTC](https://community.auth0.com/t/sending-userid-from-jwt-token-to-backend-from-nextjs-app/117601/4 "2023-10-03T20:45:47Z")

</div>

I don’t think so.

I thought that when a user logs in, it created some special token that contains user\_id, and other information.

I wanted to send that encoded information (believed to be JWT token) from client side to server side, to help keep the user\_id more ‘secure’, especially as it is used to identify users and their content in blob storage.

I don’t think this example you are showing here would let me send the JWT in my post request from server to client?

---

<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:** [October 3, 2023, 10:33pm UTC](https://community.auth0.com/t/sending-userid-from-jwt-token-to-backend-from-nextjs-app/117601/5 "2023-10-03T22:33:19Z")

</div>

> [@virajvaitha1995](#):
>
> I thought that when a user logs in, it created some special token that contains user\_id, and other information.

That’s exactly correct, when a user logs in there is typically an ID and access token returned. The ID token is designed to be used client-side whereas the access token is meant to be passed to an external API/service which validates it.

> [@virajvaitha1995](#):
>
> I wanted to send that encoded information (believed to be JWT token) from client side to server side, to help keep the user\_id more ‘secure’, especially as it is used to identify users and their content in blob storage.

The [access token](https://auth0.com/docs/secure/tokens/access-tokens#sample-access-token) you send to your API will have a `sub` claim that is equivalent to the `user_id` of the user granted the token.

> [@virajvaitha1995](#):
>
> I don’t think this example you are showing here would let me send the JWT in my post request from server to client?

I am bit confused whether you are asking about sending the token from client (nextjs) to server or the opposite - The code I shared previously adds the jwt as an authorization header in the request to an external API. It’s a bit different than [how a typical SPA SDK works](https://auth0.com/docs/quickstart/spa/react/02-calling-an-api) in that the API route in your nextjs app serves as a proxy between frontend and your external API. For more details on this I recommend checking out this Github [issue](https://github.com/auth0/nextjs-auth0/issues/358).

---

<div class="post-metadata">

**Author:** ![virajvaitha1995](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/virajvaitha1995/32/51319_2.png) [@virajvaitha1995](https://community.auth0.com/u/virajvaitha1995)\
**Post date:** [October 3, 2023, 10:58pm UTC](https://community.auth0.com/t/sending-userid-from-jwt-token-to-backend-from-nextjs-app/117601/6 "2023-10-03T22:58:04Z")

</div>

firstly thank you for your responses and patience with me.

Let me clarify.

My frontend stack is: NextJS, Typescript, React and TailwindCSS, deployed on its own compute.

I have a backend (FastAPI app hosted on a different compute), that is responsbile for communicating with SQL Server & my Frontend Application.

As you can see in the code below:

```auto
import axios from 'axios';
import type { PostUploadData, PostUploadInput, PostUploadOptions } from './types';
// import { getAccessToken } from '@auth0/nextjs-auth0'; // Import the Auth0 function

const UPLOAD_URL = 'https://myAPIURL.azurewebsites.net/upload';
const USER_ID = '456'; #Hard coded, but i want to switch this to the Access Token via header, and decode at the backend once recieved.
const QUALITY = 0;

export const postUpload = async ({
  uploaded_file,
  title,
  src_language = 'en',
  dest_language = 'en',
}: PostUploadInput): Promise<PostUploadData> => {
  // Get and log the access token
  // const { accessToken } = await getAccessToken(); // Make sure to provide necessary arguments if required
  // console.log("Access Token:", accessToken);

  // ... your existing code
  const formData = new FormData();
  formData.append('uploaded_file', uploaded_file);
  formData.append('title', title);
  formData.append('user_id', USER_ID);

  const res = await axios.post(
    `${UPLOAD_URL}?src_language=${src_language}&dest_language=${dest_language}&quality=${QUALITY}`,
    formData,
    { headers: { 'Content-Type': 'multipart/form-data' } }
  );

  return res.data;
};

```

Right now im literally just trying to get the access token and print it to see i can access it but it’s not running.

Ideally i can just pass that access token (which depends on the user logged in), and pass it into the header of the request.

Thanks,

Viraj

---

<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:** [October 3, 2023, 11:11pm UTC](https://community.auth0.com/t/sending-userid-from-jwt-token-to-backend-from-nextjs-app/117601/7 "2023-10-03T23:11:40Z")

</div>

Thanks for clarifying!

Have you had a chance to take a look at our [React](https://auth0.com/docs/quickstart/spa/react/interactive) and/or [Next.js](https://auth0.com/docs/quickstart/webapp/nextjs/interactive) quickstarts and subsequent sample applications ([React](https://github.com/auth0-samples/auth0-react-samples) , [Next.js](https://github.com/auth0-samples/auth0-nextjs-samples)) - Both outline how to get an access token for a user. It might be helpful to check those out for comparison purposes.

---

<div class="post-metadata">

**Author:** ![virajvaitha1995](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/virajvaitha1995/32/51319_2.png) [@virajvaitha1995](https://community.auth0.com/u/virajvaitha1995)\
**Post date:** [October 4, 2023, 5:14pm UTC](https://community.auth0.com/t/sending-userid-from-jwt-token-to-backend-from-nextjs-app/117601/8 "2023-10-04T17:14:15Z")

</div>

The quickstart doesn’t show how to do this, and there is no detailed documentation on this topic.

They give one example with a simple Get Request, on an outdated nextjs version that uses page router.

I thought the Access Token is created on authorisation/authentication, so i was hoping i could just access this, like Header: getAccessToken().

I’ll have to switch to a different library i think

---

<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:** [October 19, 2023, 5:14pm UTC](https://community.auth0.com/t/sending-userid-from-jwt-token-to-backend-from-nextjs-app/117601/9 "2023-10-19T17:14:40Z")

</div>

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