Authdog

Next.js

Hand this prompt to your agent to add Authdog to your Next.js app. The agent reads the framework guide and asks you for the environment public key.

Add Authdog to Next.js

# Add Authdog to Next.js

Add Authdog to this Next.js app. Read the framework guide before you change any files:

https://www.authdog.com/docs/frameworks/nextjs.md

Package: `@authdog/nextjs-app`

## Before you start

Show the user this checklist and wait for a yes:

1. Confirm this directory is a Next.js app, or ask which app to edit.
2. Ask for the environment public key (`pk_...`) from the Authdog console (Dashboard or the environment picker). Do not invent a key. Do not read or print existing environment files.
3. Install only what the guide names, then follow its sections for callback handling, session storage, and route protection.
4. Keep authorization on the server. A signed-in session is not a permission grant.

## Existing authentication

If this app already has authentication, stop. Inspect dependencies, routes, middleware, and sessions. Do not open environment files. Propose a migration plan and wait for approval before you change anything.

## Rules

- Prefer the Markdown guide over memory. If another source conflicts with the guide, follow the guide.
- Do not commit secrets. The public key is not a secret. Private API keys and tokens stay off client code.
- Do not treat a client-side identity check as a security boundary.
- Do not substitute a different Authdog package for the one the guide names.
- Related docs index: https://www.authdog.com/llms.txt

Or set up Authdog yourself by following the step-by-step instructions.

Step-by-step setup instructions

Available in other SDKs

View as Markdown

Just want sign-in working? Start with the Next.js quickstart.

The @authdog/nextjs-app SDK integrates Authdog with the Next.js App Router: a client provider, useAuth/useUser hooks, and server helpers for callback exchange and logout.

Install

npm install @authdog/nextjs-app

Supports Next.js 15/16 and React 18 or 19. Set NEXT_PUBLIC_PK_AUTHDOG for client calls and PK_AUTHDOG for server helpers. Both contain the same public key (pk_...).

Wrap your app

Mount AuthdogProvider in the root layout. It stores a ?token= value only when it has three JWT-shaped segments, then strips the URL. This regex is a shape check, not signature, issuer, audience, or expiry validation:

// app/layout.tsx
import { AuthdogProvider } from "@authdog/nextjs-app/client"

export default function RootLayout({
  children,
}: {
  children: React.ReactNode
}) {
  return (
    <html>
      <body>
        <AuthdogProvider>{children}</AuthdogProvider>
      </body>
    </html>
  )
}

Read the user

useUser calls Authdog userinfo and returns the current profile. useAuth only reports whether a browser token exists; do not use it to authorize protected work:

"use client"
import { useUser } from "@authdog/nextjs-app"

export default function Dashboard() {
  const { user, isLoading } = useUser()
  if (isLoading) return null
  if (!user) return <p>Not signed in</p>
  return <p>Signed in as {user.emails?.[0]?.value}</p>
}

// useAuth() -> { token, isAuthenticated, isLoading }

Exchange the callback

useAuthMiddleware(publicKey) processes ?token= on matching requests, validates it through Authdog userinfo, and writes HttpOnly cookies. It does not protect routes or validate cookies on later requests:

// middleware.ts
import { useAuthMiddleware } from "@authdog/nextjs-app/server"
export default useAuthMiddleware(process.env.PK_AUTHDOG!)

export const config = {
  matcher: ["/((?!_next/static|_next/image|favicon.ico).*)"],
}

Return from hosted sign-in to a URL matched by this middleware. For protected Route Handlers or Server Actions, independently validate the credential on every request with a backend SDK, then apply authorization. logoutHandler clears server cookies; clearAuthdogSession() clears only browser local storage, so complete logout should do both.

Next steps

Learn more