The NextAuth session shows old data until the page is refreshed
Next-auth session doesn't update until I refresh the page or navigate to another pageShort 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().
Fix it fast
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/null2Match what you see, then jump to its fix
- 1
The 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
update() resolves, yet the old value comes back.The jwt or session callback ignores the update triggerFix - 3
The first tab updates, a second tab stays stale.Another tab or window still holds the old sessionFix - 4
Client components show the new data, parts rendered on the server do not.Server Components still render the old sessionFix
- 1
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 pages2Fix
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 devConfirmed by next-auth.js.org
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.ts2Fix
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/sessionConfirmed by next-auth.js.org
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 pages2Fix
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 devConfirmed by next-auth.js.org
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()" app2Fix
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 devConfirmed by nextjs.org
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
Web apps
The user is undefined inside the NextAuth session callback
session.user.id is undefined
With JWT sessions, NextAuth gives the session callback a token, not a user.
3 causes, each with a fixWeb apps
AuthUserPoolException: Auth UserPool not configured in a Next.js app
Auth UserPool not configured
The error means an Amplify Auth call ran before Amplify.configure did.
3 causes, each with a fixWeb apps
Apollo Server rejects a request as a potential CSRF
HTTP 400 Bad Request from the GraphQL endpoint
Apollo Server's CSRF prevention is on by default.
3 causes, each with a fixWeb apps
Clerk cannot read its publishable key in a Next.js app
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY is undefined
Clerk reads NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY.
3 causes, each with a fixWeb apps
The shell cannot find create-next-app
command not found: create-next-app
create-next-app is not meant to be installed as a shell command.
2 causes, each with a fixWeb apps
ESLint fails with: Cannot find module 'next/babel'
ESLint stops on the first file with a Babel or parser error instead of listing lint results
next/babel is a Babel preset that ships inside the next package.
3 causes, each with a fix