nextjs-auth0: The access token has expired and a refresh token was not provided. The user needs to re-authenticate. at async middleware

Checklist

Description

I have followed the guides for the v4 access tokens for getting an access token in the middleware if its to be used in a server component, however, the docs seem to stop right before it says how to actually use that access token? I’ve looked at both #1820 #1841 however, neither explain how to actually use the refresh token in the middleware. Right now i’m getting this error:

 ⨯ Error [AccessTokenError]: The access token has expired and a refresh token was not provided. The user needs to re-authenticate.
    at async middleware (src/middleware.ts:22:22)
  20 |     return NextResponse.redirect(`${origin}/auth/login?to=${path}`);
  21 |   }
> 22 |   const accessToken = await auth0.getAccessToken(request, authRes);
     |                      ^
  23 |   authRes.headers.set("x-access-token", accessToken.token);
  24 |   // the headers from the auth middleware should always be returned
  25 |   return authRes;

It’s unclear how the refresh token should actually be provided. Am I still using the wrong method? Any guidance would be appreciated, thank you!

Reproduction

Set this as your middleware, wait for an access token expiry

import { NextResponse, type NextRequest } from "next/server";

import { auth0 } from "./lib/auth0";

export async function middleware(request: NextRequest) {
  const path = request.nextUrl.pathname;
  const isProtectedRoute = path.startsWith("/app");

  const authRes = await auth0.middleware(request);
  // authentication routes — let the middleware handle it
  if (path && path.startsWith("/auth")) {
    return authRes;
  }

  const { origin } = new URL(request.url);
  const session = await auth0.getSession(request);

  // user does not have a session — redirect to login
  if (!session && isProtectedRoute) {
    return NextResponse.redirect(`${origin}/auth/login?to=${path}`);
  }
  const accessToken = await auth0.getAccessToken(request, authRes);
  authRes.headers.set("x-access-token", accessToken.token);
  // the headers from the auth middleware should always be returned
  return authRes;
}

Additional context

No response

nextjs-auth0 version

4.0.0-beta.14

Next.js version

^15.1.0

Node.js version

v22.10.0

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 7
  • Comments: 21 (5 by maintainers)

Most upvoted comments

I have done all of this already, i didn’t override default scopes, I set refresh tokens enabled in the grant types, and i have configured callback URLs as well. Still failing. Steps provided in the original issue.

I’m wondering the same thing. The docs says it’s recommended to get the accessToken in a middleware if you plan to use the token in a Server Component, but doesn’t say how to use it. The middleware example gets the accessToken, but doesn’t use it for anything (?).

I’m trying to use the access token in an API route that acts as a proxy between the app/client and our backend APIs. Not as easy as it seems…

@frederikprijck i’m still experiencing this bug and have done both of those remediations

If you did not have offline_access, the SDK is unable to refresh any token. So your application is expected to work until the access token is expired, and then it is expected to tell u it can not refresh as it doesnt have a refresh token.

When u add offline_access, and login again, it should work.