# Next.js app getting logged out after refresh

**URL:** <https://community.auth0.com/t/next-js-app-getting-logged-out-after-refresh/194750>\
**Category:** Get Help\
**Tags:** login-experience, new-universal-login-experience\
**Created:** [November 21, 2025, 12:20am UTC](https://community.auth0.com/t/next-js-app-getting-logged-out-after-refresh/194750 "2025-11-21T00:20:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jyrg](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/jyrg/32/71157_2.png) [@jyrg](https://community.auth0.com/u/jyrg)\
**Post date:** [November 21, 2025, 12:20am UTC](https://community.auth0.com/t/next-js-app-getting-logged-out-after-refresh/194750/1 "2025-11-21T00:20:18Z")

</div>

We created a next.js app with the app router and the Auth0 V4 SDK.

We have configured our middleware:

```javascript
import { auth0 } from './lib/auth0';

export async function middleware(req) {
  // Auth only. No slug rewriting/canonicals.
  const publicRoutes = [
    '/', '/robots.txt', '/sitemap.xml', '/favicon.ico',
    '/auth/login', '/login-success',
  ];
  return auth0.middleware(req, { publicRoutes });
}

// Run on everything except Next internals, API, and static assets
export const config = {
  matcher: [
    '/((?!_next/static|_next/image|api|.*\\.(?:png|jpg|jpeg|gif|webp|svg|ico|txt|xml)).*)',
  ],
};

```

and auth0 lib file:

```javascript
// lib/auth0.js

import { Auth0Client } from "@auth0/nextjs-auth0/server";

// Initialize the Auth0 client 
export const auth0 = new Auth0Client({
  noContentProfileResponseWhenUnauthenticated: true,
  enableParallelTransactions: false,
  authorizationParameters: {
    // In v4, AUTH0_SCOPE and AUTH0_AUDIENCE must be explicitly provided
    scope: process.env.AUTH0_SCOPE,
    audience: process.env.AUTH0_AUDIENCE,
  }
});

```

However when changing pages or refreshing a page which uses the useUser() method, the user just gets signed out sometimes. Is there something I am missing here?

Thanks for your help!

---

<div class="post-metadata">

**Author:** ![nik.baleca](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/nik.baleca/32/68749_2.png) [@nik.baleca](https://community.auth0.com/u/nik.baleca)\
**Post date:** [November 25, 2025, 7:07pm UTC](https://community.auth0.com/t/next-js-app-getting-logged-out-after-refresh/194750/3 "2025-11-25T19:07:50Z")

</div>

Hi @jyrg

Welcome to the Auth0 Community!

I am sorry about the delayed answer to your topic.

Can you try to include API routes inside your middleware matcher config? I believe the issue might be caused by the fact that API routes which would usually allow your application to roll the session of the user. Let me know if this works for you or not!

Kind Regards,  
Nik

---

<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:** [December 9, 2025, 7:08pm UTC](https://community.auth0.com/t/next-js-app-getting-logged-out-after-refresh/194750/4 "2025-12-09T19:08:08Z")

</div>

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