Next.js with Auth.js
Sign users in to a Next.js app with Tessera. About five minutes, three small files.
On this page
Sign users in to a Next.js app with Tessera. About five minutes, three small files.
Requires: Node.js 20 or later and a Next.js 15 project that uses the App Router.
1. Create the app in Tessera
Sign in to the console at https://platform.kurthlabs.com/tessera with your invitation (see your first sign-in), open your realm and add an app. Choose Next.js and where it runs: this computer for now.
The console then shows what you need, each with a copy button:
- the issuer,
https://login.kurthlabs.com/realms/YOUR_REALM - the client ID
- the redirect URI,
http://localhost:3000/api/auth/callback/tessera - the client secret, once. Copy it now, or download the
.envfile. Tessera cannot show it again.
Put the values in .env.local as shown under Environment below, and keep that file out of git.
If you have the tessera CLI (during early access it is by invitation), tessera init does this step: it registers the app, writes .env.local and prints the code for your stack. The console path above needs no download.
2. Add the provider
Install Auth.js v5, pinned to an exact version (a bare next-auth installs v4, which this code does not match, and v5 is still in beta):
npm install --save-exact next-auth@5.0.0-beta.32auth.ts
import NextAuth from "next-auth";
export const { handlers, auth, signIn, signOut } = NextAuth({
providers: [{
id: "tessera",
name: "Tessera",
type: "oidc",
issuer: process.env.TESSERA_ISSUER,
clientId: process.env.TESSERA_CLIENT_ID,
clientSecret: process.env.TESSERA_CLIENT_SECRET,
}],
});app/api/auth/[...nextauth]/route.ts
import { handlers } from "@/auth";
export const { GET, POST } = handlers;3. Add a sign-in button
import { auth, signIn } from "@/auth";
export default async function Page() {
const session = await auth();
if (session?.user) return <p>Signed in as {session.user.email}</p>;
return (
<form action={async () => { "use server"; await signIn("tessera"); }}>
<button>Sign in</button>
</form>
);
}Run npm run dev and open the page. Click Sign in, approve on Tessera, and you come back with "Signed in as" and your email.
Environment
The console gives you the first three values (the downloaded .env file has them, and so does tessera init). Put them in .env.local:
TESSERA_ISSUER=https://login.kurthlabs.com/realms/YOUR_REALM
TESSERA_CLIENT_ID=YOUR_CLIENT_ID
TESSERA_CLIENT_SECRET=YOUR_CLIENT_SECRET
AUTH_SECRET=YOUR_SESSION_SECRETAUTH_SECRET is yours, not Tessera's: it signs your app's own session cookie. Generate one with openssl rand -base64 32.
The redirect URI registered for this app is http://localhost:3000/api/auth/callback/tessera. For production, add your production address to the app's redirect URIs in the console (the same path on https://your-app.example); the local one can stay.