Expose session context
A Pages Router application needs useSession() in nested client components.
Auth.js · Component
SessionProvider is a client-side React Context provider for the Next.js Pages Router. It makes session data available to descendants and coordinates session updates across browser tabs and windows.
SessionProvider is a client-only React Context provider for Next.js Pages Router applications. It makes session data available to descendants, synchronizes session state across open tabs and windows, and can refetch when windows regain focus or when a configured interval elapses.
function SessionProvider(props): Element| Name | Type | Description |
|---|---|---|
props | SessionProviderProps | Provider properties, including an optional session value and session-refetch configuration. |
Element
import { SessionProvider } from "next-auth/react"
import Dashboard from "./Dashboard"
export default function App({ session }) {
return (
<SessionProvider session={session}>
<Dashboard />
</SessionProvider>
)
}Wrap the client-side application so Dashboard and its descendants can use useSession().
A Pages Router application needs useSession() in nested client components.
Several tabs must reflect sign-in or sign-out changes.
The application needs periodic or focus-based session refreshes.
It is a React Context provider that wraps a Pages Router application and exposes session data to descendant components. It is intended for client-side use, commonly together with useSession().
Follow-up: What should an App Router application prefer? Use the auth() export for server-side session access rather than relying on SessionProvider.
The provider synchronizes session state across open tabs and windows. With refetchOnWindowFocus enabled, focus changes and authentication-state changes trigger updates.
Follow-up: How can polling be controlled? Set refetchInterval to a number of seconds; zero, the default, disables polling.
Pass the server-provided session through the session prop when initializing the provider, as shown for the Pages Router. Provider options can also customize useSession behavior.
Follow-up: When might customization be useful? Short session expiry times or a need to change refetch behavior are cases called out by the documentation.
useSessionSessionProviderPropsauthNextAuth