-
Notifications
You must be signed in to change notification settings - Fork 439
Description
Checklist
- The issue can be reproduced in the nextjs-auth0 sample app (or N/A).
- I have looked into the Readme, Examples, and FAQ and have not found a suitable solution or answer.
- I have looked into the API documentation and have not found a suitable solution or answer.
- I have searched the issues and have not found a suitable solution or answer.
- I have searched the Auth0 Community forums and have not found a suitable solution or answer.
- I agree to the terms within the Auth0 Code of Conduct.
Description
When calling getSession(req) from inside a Next.js Middleware, the following check inside the library fails:
nextjs-auth0/src/server/client.ts
Line 362 in 0e83495
| if (req instanceof NextRequest) { |
// src/server/client.ts:362
if (req instanceof NextRequest) {
...
}This is because in the Middleware context, request object is not an instance of NextRequest, but rather NextRequestHint, which is structurally similar but does not inherit from NextRequest. As a result, the condition returns false, and the wrong branch of logic is executed (falling back to createRequestCookies(req) which expects a different shape).
reqis of typeNextRequest, but it’s not an instance ofNextRequest. Its prototype isNextRequestHint.
As a result, if I invoke getSession(req), providing req: NextRequest, I get an error:
⨯ TypeError: Headers.append: "append(name, value) {
webidl.brandCheck(this, _Headers);
webidl.argumentLengthCheck(arguments, 2, "Headers.append");
const prefix = "Headers.append";
name = webidl.converters.ByteString(name, prefix, "name");
value = webidl.converters.ByteString(value, prefix, "value");
return appendHeader(this, name, value);
}" is an invalid header value.
at refreshAccessTokenEndpointHandler (src/server__new/auth/providers/auth0/refresh-access-token/refresh-access-token-endpoint-handler.ts:36:35)
at auth0RouterMiddleware (src/server__new/auth/providers/auth0/router-middleware.ts:40:47)
You need a better way to distinguish between a middleware usage and pages router usage than simple instanceof check (which doesn't work):
async getSession(req) {
if (req) {
// middleware usage
if (req instanceof NextRequest) {
return this.sessionStore.get(req.cookies);
}
// pages router usage
return this.sessionStore.get(this.createRequestCookies(req));
}
// app router usage: Server Components, Server Actions, Route Handlers
return this.sessionStore.get(await cookies());
}Reproduction
middleware.ts
// imports, config
export async function middleware(request: NextRequest) {
const authRes = await auth0.middleware(request);
if (request.nextUrl.pathname.startsWith("/auth")) {
return authRes;
}
const session = await auth0.getSession(request);
if (!session) {
// user is not authenticated, redirect to login page
return NextResponse.redirect(
new URL("/auth/login", request.nextUrl.origin)
);
}
// the headers from the auth middleware should always be returned
return authRes;
}Additional context
getAccessToken method is affected as well because it utilizes getSession under the hood.
Related:
- Headers.append: xxx is an invalid header value #2219
- bugfix: ignore enumerable functions when copying headers in Pages-router #2225
nextjs-auth0 version
4.9.0
Next.js version
15.4.2-canary.26
Node.js version
v22.15.0