Hi there,
I updated to the latest version of NextJS & auth0/next-js
recently and have been having problems with getting things to work since the upgrade. After creating my middleware.js
as advised here, it looks like:
import { auth0 } from '@/lib/auth0';
import { NextResponse } from 'next/server';
export async function middleware(request) {
const authRes = await auth0.middleware(request);
if (request.nextUrl.pathname === '/api/healthcheck') {
return NextResponse.next();
}
if (request.nextUrl.pathname.startsWith('/auth')) {
return authRes;
}
const session = await auth0.getSession(request);
if (!session) {
console.log('User not authenticated, redirecting to login page');
// user is not authenticated, redirect to login page
return NextResponse.redirect(new URL('/auth/login', request.nextUrl.origin));
}
return authRes;
}
export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico, sitemap.xml, robots.txt (metadata files)
*/
// '/((?!_next/static|_next/image|site.webmanifest|favicon.ico|sitemap.xml|robots.txt|api/healthcheck).*)',
'/((?!api|_next|site.webmanifest|favicon.ico|sitemap.xml|robots.txt|.*\\..*).*)',
],
};
Once I log in, I can see my profile coming back correctly and it looks like I’ve authenticated correctly, however I can then see a request being made to https://<domain>/_next/data/2PCuD0A4gDEKoTBvZV2Rv/auth/logout.json
which I believe is then logging my user out as when I navigate to other pages my call to /auth/profile
now returns a 401 Unauthorized
. Is there a step that I’m somehow missing? When running on localhost
everything seems to be fine it’s just when deployed.