useSession throws because there is no SessionProvider above it
[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.
Fix it fast
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=.next2Match what you see, then jump to its fix
- 1
The 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
The 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
The 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
- 1
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.tsx2Fix
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 devConfirmed by next-auth.js.org
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.tsx2Fix
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 devConfirmed by authjs.dev
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/null2Fix
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 buildConfirmed by authjs.dev
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
getSession finds no user inside a Next.js API route
An API route that checks the session answers 401 for a signed-in user
getSession is meant for the browser and does an extra fetch when used on the server.
3 causes, each with a fixWeb apps
ESLint says an element in a list is missing its key prop
react/jsx-key
react/jsx-key fires when an element returned from map, Array.from or an array literal has no key.
3 causes, each with a fixWeb apps
There is no tailwind.config.js after installing Tailwind 4 and HeroUI in Next.js
HeroUI components render unstyled
Nothing is broken: Tailwind CSS 4 is configured in CSS and no longer creates tailwind.config.js.
3 causes, each with a fixWeb apps
react-i18next shows the wrong plural form for a count, or prints the key instead of the text
t('item') prints the key item instead of a translated sentence
i18next picks a plural from the count option and looks for keys ending in _one, _other and so on.
4 causes, each with a fixWeb 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 fixWeb 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