# How to automatically refresh, and reuse access tokens using nextJS?

**URL:** <https://community.auth0.com/t/how-to-automatically-refresh-and-reuse-access-tokens-using-nextjs/139514>\
**Category:** Get Help\
**Tags:** reusable-refresh-tokens, sessions\
**Created:** [July 23, 2024, 3:09pm UTC](https://community.auth0.com/t/how-to-automatically-refresh-and-reuse-access-tokens-using-nextjs/139514 "2024-07-23T15:09:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![KevinM](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/kevinm/32/55831_2.png) [@KevinM](https://community.auth0.com/u/KevinM)\
**Post date:** [July 23, 2024, 3:09pm UTC](https://community.auth0.com/t/how-to-automatically-refresh-and-reuse-access-tokens-using-nextjs/139514/1 "2024-07-23T15:09:43Z")

</div>

I am working with a Next.js frontend and an Express backend. I need a solution to automatically create and reuse a user’s access token until it expires. I believe there might be a way to achieve this using the SDK, but I haven’t been able to find it in the past few weeks of working with Auth0 and Next.js.

Currently, I’m using `getAccessToken` to securely send requests to my API. While this works, I’ve noticed that this method generates a new access token each time and does not store anything in the browser. Should I be handling this myself by storing the token in local storage or a cookie, or does Auth0’s Next.js SDK provide a built-in solution for this? I also tried using `getSession`, but it did not refresh the access token automatically.

Here is an example page.jsx making a request

```jsx
// 'use server';
import { getAccessToken } from '@auth0/nextjs-auth0';
const apiServerUrl = process.env.API_SERVER_URL;

const fetchProtectedData = async () => {
  const { accessToken } = await getAccessToken();

  const response = await fetch(`${apiServerUrl}/protected`, {
    headers: {
      'content-type': 'application/json',
      Authorization: `Bearer ${accessToken}`,
    },
  });

  const responseData = await response.json();
  return responseData;
};

export default async function ProtectedPage2({ params }) {
  const data = await fetchProtectedData(params);

  return (
    <div>
      <UserInfo data={data} />
    </div>
  );
}

function UserInfo({ data }) {
  return (
    <div>
      <h2>Protected Route</h2>
      <ul>
        {Object.entries(data).map(([key, value]) => (
          <li key={key}>
            <strong>{key}:</strong> {value}
          </li>
        ))}
      </ul>
    </div>
  );
}

```

Here is my middleware.js

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

export default withMiddlewareAuthRequired();

```

Here is my .env configuration for scope.

```bash

AUTH0_SCOPE='openid offline_access email profile'

```

---

<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:** [July 31, 2024, 11:54pm UTC](https://community.auth0.com/t/how-to-automatically-refresh-and-reuse-access-tokens-using-nextjs/139514/3 "2024-07-31T23:54:17Z")

</div>

Hi @KevinM,

I recommend using refresh tokens to get new access tokens without user interaction. Then to use these access tokens automatically, you can pass in the `refresh: true` option in the request.

Please review [this solution](https://community.auth0.com/t/next-js-13-how-to-access-an-external-api-from-an-api-route-using-app-router-api-route/116459/6) as a reference.

Let me know if you have any questions.

Thanks,  
Rueben

---

<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:** [August 14, 2024, 11:54pm UTC](https://community.auth0.com/t/how-to-automatically-refresh-and-reuse-access-tokens-using-nextjs/139514/4 "2024-08-14T23:54:49Z")

</div>

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