diff --git a/.changeset/react-readme-nextjs-package.md b/.changeset/react-readme-nextjs-package.md new file mode 100644 index 0000000..8f2e9be --- /dev/null +++ b/.changeset/react-readme-nextjs-package.md @@ -0,0 +1,5 @@ +--- +'@seamless-auth/react': patch +--- + +Point the README's Next.js section at `@seamless-auth/nextjs`, which serves the `/auth` routes from a route handler and resolves the session with `getSeamlessSession`. It no longer says a Next.js server package is still planned. diff --git a/AGENTS.md b/AGENTS.md index a5dda90..5d10b2b 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -320,8 +320,9 @@ Next.js and server rendering (#78): `refreshSession({ background: true })`. Never document forwarding browser cookies to the adapter's `/auth/users/me` from a server: an expired access cookie makes the adapter rotate the refresh token in a response the browser - never sees. Server reads go through `getSeamlessUser` in `@seamless-auth/core` -- the server half (a route-handler adapter and session helpers) belongs in a + never sees. Next.js server reads go through `getSeamlessSession` in + `@seamless-auth/nextjs` (other servers use `getSeamlessUser` in `@seamless-auth/core`) +- the server half (the route-handler adapter and session helpers) is the `@seamless-auth/nextjs` package in `seamless-auth-server`, not here Tests live in each package's `tests/` directory and run as two Jest projects diff --git a/packages/react/README.md b/packages/react/README.md index 73c59dc..27c84be 100644 --- a/packages/react/README.md +++ b/packages/react/README.md @@ -119,15 +119,48 @@ The package works in the Next.js App Router. Every module ships with a `'use cli `AuthProvider` can be rendered straight from a server layout, and nothing touches `window` or storage while rendering on the server. +The server half is [`@seamless-auth/nextjs`](https://www.npmjs.com/package/@seamless-auth/nextjs). +It serves the `/auth` routes from a route handler on the application's own origin and reads the +session in server components, so a Next.js app needs no separate backend for auth: + +```bash +npm install @seamless-auth/nextjs @seamless-auth/react +``` + +```ts +// app/auth/[...seamless]/route.ts +import { createSeamlessAuthHandler } from '@seamless-auth/nextjs'; + +export const { GET, POST, PUT, PATCH, DELETE } = createSeamlessAuthHandler({ + authServerUrl: process.env.AUTH_SERVER_URL!, + audience: process.env.AUTH_SERVER_URL!, + cookieSecret: process.env.COOKIE_SIGNING_KEY!, + serviceSecret: process.env.API_SERVICE_TOKEN!, + jwksKid: process.env.JWKS_KID, +}); +``` + ```tsx // app/layout.tsx +import { cookies, headers } from 'next/headers'; +import { getSeamlessSession } from '@seamless-auth/nextjs'; import { AuthProvider } from '@seamless-auth/react'; -export default function RootLayout({ children }: { children: React.ReactNode }) { +export const dynamic = 'force-dynamic'; + +export default async function RootLayout({ children }: { children: React.ReactNode }) { + const session = await getSeamlessSession(await cookies(), { + authServerUrl: process.env.AUTH_SERVER_URL!, + cookieSecret: process.env.COOKIE_SIGNING_KEY!, + serviceSecret: process.env.API_SERVICE_TOKEN!, + jwksKid: process.env.JWKS_KID, + userAgent: (await headers()).get('user-agent') ?? undefined, + }); + return (
-