superapp
ClientComponents

Auth Provider

Root layout wrapper that provides auth context to all child components.

AuthProvider wraps your application root and provides auth context (session, sign-in state) to all child components. Required for useSession, AuthCard, and UserButton to work.

import { AuthProvider } from '@superapp/auth/components'
import { authClient } from '@/lib/superapp'

export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <AuthProvider authClient={authClient}>
      {children}
    </AuthProvider>
  )
}

Props

PropTypeRequiredDescription
authClientAuthClientYesThe auth client created by createAuth()
navigate(url: string) => voidNoNavigation function for client-side routing
replace(url: string) => voidNoReplace navigation function (no history entry)
LinkReact.ComponentTypeNoLink component for framework-specific navigation
onSessionChange() => voidNoCallback fired on sign-in, sign-out, or token refresh
childrenReact.ReactNodeYesYour application content

Next.js App Router Setup

'use client'

import { AuthProvider } from '@superapp/auth/components'
import { authClient } from '@/lib/superapp'
import { useRouter } from 'next/navigation'
import Link from 'next/link'

export function Providers({ children }: { children: React.ReactNode }) {
  const router = useRouter()

  return (
    <AuthProvider
      authClient={authClient}
      navigate={router.push}
      replace={router.replace}
      Link={Link}
      onSessionChange={() => router.refresh()}
    >
      {children}
    </AuthProvider>
  )
}

Then use it in your root layout:

import { Providers } from './providers'

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

Why navigate and replace?

AuthProvider handles redirects after sign-in and sign-out. Passing your framework's navigation functions lets it do this with client-side routing instead of full page reloads.

PropUsed For
navigateRedirect after sign-in (pushes to history)
replaceRedirect after sign-out (replaces history entry)
LinkInternal links in auth UI components

onSessionChange

The onSessionChange callback fires whenever the auth state changes. Common uses:

  • router.refresh() — Re-run server components to pick up the new session (Next.js App Router).
  • Refetch queries — Invalidate cached data when the user changes.
  • Analytics — Track sign-in/sign-out events.
<AuthProvider
  authClient={authClient}
  onSessionChange={() => {
    router.refresh()
  }}
>
  {children}
</AuthProvider>

On this page