Maaz Zindani

Loading the index…

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

useSession throws because there is no SessionProvider above it

Error[next-auth]: `useSession` must be wrapped in a <SessionProvider />

Short answer

useSession reads the session from React Context, and SessionProvider is what creates that context. The error means the component calling useSession is rendered outside a provider. Add SessionProvider once, above every component that uses the hook, in pages/_app for the Pages Router or in a layout for the App Router.

  • Applies to next-auth 4.24 and Auth.js v5, Next.js 15, React 19
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find where the failing component sits in the tree, and search for any SessionProvider in the project. If there is none, or it is only wrapped around part of the app, one of the causes below applies.

    grep -rn "SessionProvider" --include=*.tsx --include=*.jsx --include=*.js --include=*.ts . --exclude-dir=node_modules --exclude-dir=.next
  2. 2Match what you see, then jump to its fix

    1. 1The project has a pages/ folder and the grep shows no SessionProvider, or it is in a single page onlyPages Router: SessionProvider is missing from pages/_appFix
    2. 2The project uses app/ and useSession is called in a component marked "use client"App Router: a client component uses the hook with no provider in the layoutFix
    3. 3The failing file is a Server Component, a route handler or a page that never opts into the clientThe component runs on the server, where the hook cannot workFix

What does the error look like?

  • [next-auth]: `useSession` must be wrapped in a <SessionProvider />

Cause 1 of 3

Pages Router: SessionProvider is missing from pages/_app

1Check

The next-auth client docs say to make sure SessionProvider is added to pages/_app.js. It shares the session between all useSession calls through React Context.

cat pages/_app.js 2>/dev/null || cat pages/_app.tsx

2Fix

Wrap the app once and pass the session that Next.js gives the page.

// pages/_app.js
import { SessionProvider } from "next-auth/react"

export default function App({
  Component,
  pageProps: { session, ...pageProps },
}) {
  return (
    <SessionProvider session={session}>
      <Component {...pageProps} />
    </SessionProvider>
  )
}

3Verify

Reload the page that called useSession; the error is gone and status becomes authenticated or unauthenticated.

npm run dev

Confirmed by next-auth.js.org

Did this fix it?

Cause 2 of 3

App Router: a client component uses the hook with no provider in the layout

1Check

Auth.js v5 says client components that read the session through context need to be wrapped in a SessionProvider. Its imports from next-auth/react carry the use client directive, so the provider can be rendered from your layout.

cat app/layout.tsx

2Fix

Render SessionProvider around the children in the root layout, or around the part of the tree that holds the components using useSession.

// app/layout.tsx
import { SessionProvider } from "next-auth/react"

export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en">
      <body>
        <SessionProvider>{children}</SessionProvider>
      </body>
    </html>
  )
}

3Verify

Open the page; the component renders and useSession returns a status.

npm run dev

Confirmed by authjs.dev

Did this fix it?

Cause 3 of 3

The component runs on the server, where the hook cannot work

1Check

useSession is a client hook that needs the provider's context. For server code in the App Router, the next-auth docs recommend getServerSession, and Auth.js v5 uses the auth() function.

grep -rn "useSession" app lib 2>/dev/null

2Fix

In a Server Component read the session on the server instead of calling the hook. In v4 use getServerSession with your options, in v5 call auth().

// Auth.js v5, Server Component
import { auth } from "@/auth"

export default async function Page() {
  const session = await auth()
  return <p>{session?.user?.name}</p>
}

3Verify

The page renders without importing useSession.

npm run build

Confirmed by authjs.dev

Did this fix it?

How do I stop it coming back?

  • Add SessionProvider once, at the top of the tree, not inside individual pages.
  • Use the hook only in client components and read the session with getServerSession or auth() on the server.
  • After upgrading to Auth.js v5, re-read its migration guide, because server and client session access changed.

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

next/link throws React.Children.only on its children

Error: React.Children.only expected to receive a single React element child.

React.Children.only accepts exactly one React element and throws for anything else, including an array with one item.

3 causes, each with a fix

Web apps

Hydration failed after adding next-themes dark mode

Console reports a server and client mismatch on the html element

next-themes changes the html element and knows the theme only in the browser, so server HTML and client HTML differ.

3 causes, each with a fix

Next step

Want this handled for good?

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