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.

  1. Environment

    .env.local
    YOURSELF_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/callback
  2. Add the helper

    One file, one dependency (jose). It does PKCE, state and nonce checks, ID token verification, and a signed session cookie.

    sh
    npm install jose
    curl -fsSL https://yourself.mees.sh/sdk/yourself-auth.ts -o lib/yourself-auth.ts
  3. Configure it

    lib/auth.ts
    import { 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);
    }
  4. 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.ts
    import { 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 });
    }
  5. Keep everything behind sign-in

    Next.js 16 calls this file proxy.ts with an exported proxy function. On Next.js 15 name it middleware.ts and export middleware.

    proxy.ts
    import { 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).*)"] };
  6. Use the user

    app/page.tsx
    import { 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>
      );
    }
  7. 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_activity MCP tool (or the dashboard) shows an app.signed_in event.