Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

The NextAuth session shows old data until the page is refreshed

ErrorNext-auth session doesn't update until I refresh the page or navigate to another page

Short answer

NextAuth does not refetch the session on its own after you change a user field. Call update() from useSession, which sends the new data to the server, and handle trigger 'update' in the jwt callback with JWT sessions or the session callback with database sessions. Server Components also need router.refresh().

  • Applies to next-auth 4, Next.js 15, Next.js 16
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Check which session strategy is configured and whether any callback reads the trigger value.

    grep -rn "strategy\|trigger\|callbacks" app lib auth.* pages/api/auth 2>/dev/null
  2. 2Match what you see, then jump to its fix

    1. 1The profile saves, the database has the new name, but useSession still returns the old one.Nothing tells the client to reload the session after the changeFix
    2. 2update() resolves, yet the old value comes back.The jwt or session callback ignores the update triggerFix
    3. 3The first tab updates, a second tab stays stale.Another tab or window still holds the old sessionFix
    4. 4Client components show the new data, parts rendered on the server do not.Server Components still render the old sessionFix

What does the error look like?

  • Next-auth session doesn't update until I refresh the page or navigate to another page

Cause 1 of 4

Nothing tells the client to reload the session after the change

1Check

The session lives in the browser state and is refetched on window focus or on an interval. A save in your own API does not trigger either.

grep -rn "useSession\|SessionProvider\|refetch" app components pages

2Fix

Call update() from useSession after the save. With no argument it reloads the session from the server; with an object it sends that data to be merged.

'use client'
import { useSession } from 'next-auth/react'

export function NameForm() {
  const { data: session, update } = useSession()
  async function save(name: string) {
    await fetch('/api/profile', { method: 'POST', body: JSON.stringify({ name }) })
    await update({ name })
  }
  return <button onClick={() => save('New name')}>{session?.user?.name}</button>
}

3Verify

Click the button; the name in the header changes without a reload.

npm run dev

Confirmed by next-auth.js.org

Did this fix it?

Cause 2 of 4

The jwt or session callback ignores the update trigger

1Check

With the JWT strategy, update() runs the jwt callback with trigger set to 'update'. With the database strategy it runs the session callback instead. If the callback does not handle that trigger, nothing changes.

grep -n -A8 "jwt(\|async jwt\|async session" lib/auth.ts

2Fix

Handle the trigger in the matching callback. The docs warn to validate client-sent data on the server before saving it.

callbacks: {
  jwt({ token, trigger, session }) {
    if (trigger === 'update' && session?.name) {
      token.name = session.name
    }
    return token
  },
}

3Verify

Call update({ name: 'Test' }) and read the session; the name is Test.

curl -s -b cookies.txt http://localhost:3000/api/auth/session

Confirmed by next-auth.js.org

Did this fix it?

Cause 3 of 4

Another tab or window still holds the old session

1Check

The update() method does not sync between tabs. The refetchInterval and refetchOnWindowFocus options do.

grep -rn "refetchOnWindowFocus\|refetchInterval" app components pages

2Fix

Leave refetchOnWindowFocus at its default of true, or set refetchInterval, in seconds and lower than the session maxAge.

<SessionProvider refetchInterval={5 * 60} refetchOnWindowFocus>
  {children}
</SessionProvider>

3Verify

Change data in tab one, switch to tab two and see the new value.

npm run dev

Confirmed by next-auth.js.org

Did this fix it?

Cause 4 of 4

Server Components still render the old session

1Check

update() refreshes the client session. Server Components are rendered on the server and need a new render.

grep -rn "getServerSession\|auth()" app

2Fix

Call router.refresh() from next/navigation after update(). It re-renders Server Components and keeps client state.

import { useRouter } from 'next/navigation'
const router = useRouter()
await update({ name })
router.refresh()

3Verify

The server-rendered part of the page shows the new value.

npm run dev

Confirmed by nextjs.org

Did this fix it?

How do I stop it coming back?

  • Write session changes in one helper that saves, calls update() and then router.refresh().
  • Validate everything passed to update() on the server before storing it.
  • Keep SessionProvider high enough in the tree that every component using useSession is inside it.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.