Next.js (App Router)
Route handlers for sign-in and callback, and a proxy that keeps every page behind sign-in.
Register the app first: ask your agent, or create a web app in the dashboard with redirect URI http://localhost:3000/auth/callback.
Environment
.env.localYOURSELF_ISSUER=https://yourself.mees.sh YOURSELF_CLIENT_ID=app_… YOURSELF_CLIENT_SECRET=ysec_… (shown once when the app was created; rotate it if lost) YOURSELF_REDIRECT_URI=http://localhost:3000/auth/callbackAdd the helper
One file, one dependency (
jose). It does PKCE, state and nonce checks, ID token verification, and a signed session cookie.shnpm install jose curl -fsSL https://yourself.mees.sh/sdk/yourself-auth.ts -o lib/yourself-auth.tsConfigure it
lib/auth.tsimport { cookies } from "next/headers"; import { createYourselfAuth } from "./yourself-auth"; export const auth = createYourselfAuth({ issuer: process.env.YOURSELF_ISSUER!, clientId: process.env.YOURSELF_CLIENT_ID!, clientSecret: process.env.YOURSELF_CLIENT_SECRET!, redirectUri: process.env.YOURSELF_REDIRECT_URI!, }); /** The signed-in owner in server components and server actions. */ export async function currentUser() { const store = await cookies(); return auth.verifySession(store.get(auth.cookieName)?.value); }Sign-in routes
Three route handlers. Sign-out is a POST so it can't be triggered by a link on another site.
app/auth/[action]/route.tsimport { auth } from "@/lib/auth"; export async function GET(request: Request, { params }: { params: Promise<{ action: string }> }) { const { action } = await params; if (action === "login") return auth.signIn(request); if (action === "callback") return auth.handleCallback(request); return new Response("Not found", { status: 404 }); } export async function POST(_request: Request, { params }: { params: Promise<{ action: string }> }) { const { action } = await params; if (action === "logout") return auth.signOut(); return new Response("Not found", { status: 404 }); }Keep everything behind sign-in
Next.js 16 calls this file
proxy.tswith an exportedproxyfunction. On Next.js 15 name itmiddleware.tsand exportmiddleware.proxy.tsimport { NextResponse, type NextRequest } from "next/server"; import { auth } from "@/lib/auth"; export async function proxy(request: NextRequest) { if (await auth.getUser(request)) return NextResponse.next(); const login = new URL("/auth/login", request.url); login.searchParams.set("return_to", request.nextUrl.pathname + request.nextUrl.search); return NextResponse.redirect(login); } export const config = { matcher: ["/((?!auth/|_next/|favicon.ico).*)"] };Use the user
app/page.tsximport { currentUser } from "@/lib/auth"; export default async function Home() { const user = await currentUser(); return ( <main> <p>Signed in as {user?.name}</p> <form action="/auth/logout" method="post"><button>Sign out</button></form> </main> ); }Check it works
Start the app and open it. You're sent to yourself, sign in with your passkey, and land back in the app signed in. The
list_activityMCP tool (or the dashboard) shows anapp.signed_inevent.