NextJS 15 + @auth0/next-js 4.4.2 Login issues

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.

Hi! @tnolan and welcome back to the Auth0 Community!

The snippet you provided seems correct according to the NextJS documentation, so I believe the problem is somewhere else. As a first thing to try, please disable your Actions in the Auth0 dashboard to ensure they don’t interfere, and try again to authenticate a user on the app running on the hosted environment, where the problem occurs.

You have mentioned that this problem started to happen recently. Could you try again on an older version of NextJS? I’m thinking perhaps the version upgrade is not the issue, but another change you have made recently.

Also, could you please check that when you upgraded the NextJS version, you have followed the steps described in this migration guide for NextJS 4.x.x?

If none of the above solves your issue, please reproduce the error once again so that we can check the logs in hopes of pinpointing the cause.

Thank you for your post, and don’t hesitate to ask more! :slight_smile: