Quickstart: Next.js
This page gets a Next.js application signing people in through your issuer with Auth.js: a generic OpenID Connect provider with PKCE, a protected page that shows the person’s ID, and a sign-out that ends the issuer session too. It applies Sign users in with OpenID Connect and takes about ten minutes.
Before you begin
Section titled “Before you begin”- Node.js 20 or later.
- A confidential client on your issuer with
client_secret_basic, the redirect URIhttp://localhost:3000/api/auth/callback/authwise, the post-logout redirect URIhttp://localhost:3000/, and a minted secret. See Register a client.
Create the project
Section titled “Create the project”npx create-next-app@latest authwise-nextjs --ts --app --no-tailwind --no-eslint \ --no-src-dir --import-alias "@/*" --use-npm --yescd authwise-nextjsnpm install next-auth@betaThis page was verified with Next.js 16.4 and Auth.js 5.0 beta 32.
Create .env.local with the issuer, the client, and a secret for Auth.js’s
own cookies:
cat > .env.local <<ENVAUTHWISE_ISSUER=https://login.example.comAUTHWISE_CLIENT_ID=CLIENT_IDAUTHWISE_CLIENT_SECRET=$(cat client-secret.txt)AUTH_SECRET=$(openssl rand -base64 32)ENVReplace https://login.example.com with your issuer’s URL and CLIENT_ID
with the client’s ID.
Configure Auth.js
Section titled “Configure Auth.js”Create auth.ts at the project root:
import NextAuth from "next-auth";
declare module "next-auth" { interface Session { idToken?: string; // kept for RP-initiated logout (id_token_hint) }}
export const { handlers, auth, signIn, signOut } = NextAuth({ providers: [ { id: "authwise", name: "Authwise", type: "oidc", issuer: process.env.AUTHWISE_ISSUER, clientId: process.env.AUTHWISE_CLIENT_ID, clientSecret: process.env.AUTHWISE_CLIENT_SECRET, authorization: { params: { scope: "openid profile email" } }, checks: ["pkce", "state"], }, ], callbacks: { jwt({ token, profile, account }) { if (profile?.sub) token.sub = profile.sub; // the id_token's `sub` claim if (account?.id_token) token.idToken = account.id_token; return token; }, session({ session, token }) { session.user.id = token.sub!; session.idToken = token.idToken as string | undefined; return session; }, },});The provider’s id sets the callback path, /api/auth/callback/authwise,
which must be the redirect URI registered on the client. Auth.js reads the
endpoints from the issuer’s discovery document. The jwt callback keeps
the ID token’s sub, the person’s stable ID, and the raw ID token for
sign-out; the session callback exposes them to pages.
Mount the route handlers at app/api/auth/[...nextauth]/route.ts:
import { handlers } from "@/auth";
export const { GET, POST } = handlers;Add the pages
Section titled “Add the pages”app/page.tsx shows a sign-in button, as a server action:
import { auth, signIn } from "@/auth";import Link from "next/link";
export default async function Home() { const session = await auth(); return ( <main> <h1>Home</h1> {session ? ( <p> Signed in. <Link href="/profile">Profile</Link> </p> ) : ( <form action={async () => { "use server"; await signIn("authwise", { redirectTo: "/profile" }); }} > <button type="submit">Sign in</button> </form> )} </main> );}app/profile/page.tsx is the protected page. Its sign-out clears the
Auth.js session and then sends the browser to the issuer’s end-session
endpoint, so the issuer session ends as well:
import { auth, signOut } from "@/auth";import { redirect } from "next/navigation";
export default async function Profile() { const session = await auth(); if (!session) redirect("/"); return ( <main> <h1>Profile</h1> <p> sub: <code id="sub">{session.user?.id}</code> </p> <p>email: {session.user?.email}</p> <form action={async () => { "use server"; const session = await auth(); await signOut({ redirect: false }); // clears the Auth.js session cookie // End the issuer's session too (OIDC RP-initiated logout). const params = new URLSearchParams({ id_token_hint: session?.idToken ?? "", post_logout_redirect_uri: "http://localhost:3000/", }); redirect(`${process.env.AUTHWISE_ISSUER}/logout?${params}`); }} > <button type="submit">Sign out</button> </form> </main> );}Run it
Section titled “Run it”npm run devOpen http://localhost:3000/, click Sign in, and sign in on the hosted
page. The profile page shows sub: u-…. Sign out returns you to the
home page, and the next Sign in shows the hosted login again.
Good to know
Section titled “Good to know”AUTH_SECRETis required. Auth.js encrypts its session cookie and the PKCE state with it.issuermust equal the issuer’s URL exactly, with no trailing slash.- Auth.js’s own
signOutonly clears its cookie. Without the redirect to the issuer’s end-session endpoint, the next sign-in completes silently because the issuer session is still alive. The profile page keeps the ID token for that redirect. - Next.js 16 scaffolds
cacheComponentson. A page that callsauth()then fails to prerender, so this sample leavesnext.config.tsempty. - In production outside Vercel, set
AUTH_TRUST_HOST=trueorAUTH_URL, or Auth.js refuses the host. - With the JWT session and no adapter, Auth.js does not refresh the access
token. To call an API with it, store
account.access_tokenandaccount.refresh_tokenin thejwtcallback and refresh there whenexpires_athas passed. See Keep the session alive.
What’s next
Section titled “What’s next”- Send the access token as a bearer token to the API the client’s audience names, and validate it there against the issuer’s JWKS.
- The Node.js quickstart shows the refresh grant in a plain Express server if you need it outside Auth.js.