Skip to content

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.

  • Node.js 20 or later.
  • A confidential client on your issuer with client_secret_basic, the redirect URI http://localhost:3000/api/auth/callback/authwise, the post-logout redirect URI http://localhost:3000/, and a minted secret. See Register a client.
Terminal window
npx create-next-app@latest authwise-nextjs --ts --app --no-tailwind --no-eslint \
--no-src-dir --import-alias "@/*" --use-npm --yes
cd authwise-nextjs
npm install next-auth@beta

This 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:

Terminal window
cat > .env.local <<ENV
AUTHWISE_ISSUER=https://login.example.com
AUTHWISE_CLIENT_ID=CLIENT_ID
AUTHWISE_CLIENT_SECRET=$(cat client-secret.txt)
AUTH_SECRET=$(openssl rand -base64 32)
ENV

Replace https://login.example.com with your issuer’s URL and CLIENT_ID with the client’s ID.

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;

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>
);
}
Terminal window
npm run dev

Open 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.

  • AUTH_SECRET is required. Auth.js encrypts its session cookie and the PKCE state with it.
  • issuer must equal the issuer’s URL exactly, with no trailing slash.
  • Auth.js’s own signOut only 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 cacheComponents on. A page that calls auth() then fails to prerender, so this sample leaves next.config.ts empty.
  • In production outside Vercel, set AUTH_TRUST_HOST=true or AUTH_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_token and account.refresh_token in the jwt callback and refresh there when expires_at has passed. See Keep the session alive.
  • 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.