Hono (Cloudflare Workers, Bun, Node, Deno)

Two routes and one middleware. Works the same on every runtime Hono supports.

Register the app first: ask your agent, or create a web app in the dashboard with redirect URI http://localhost:8787/auth/callback.

  1. Environment

    On Cloudflare Workers put these in .dev.vars locally and set the secret with npx wrangler secret put YOURSELF_CLIENT_SECRET. Elsewhere use .env.

    .dev.vars
    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:8787/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 src/yourself-auth.ts
  3. Wire it up

    Register the auth routes before the middleware so they stay public.

    src/index.ts
    import { Hono } from "hono";
    import { createYourselfAuth, type YourselfAuth, type YourselfUser } from "./yourself-auth";
    
    type Bindings = {
      YOURSELF_ISSUER: string;
      YOURSELF_CLIENT_ID: string;
      YOURSELF_CLIENT_SECRET: string;
      YOURSELF_REDIRECT_URI: string;
    };
    const app = new Hono<{ Bindings: Bindings; Variables: { user: YourselfUser } }>();
    
    let cached: YourselfAuth | undefined;
    const auth = (env: Bindings) => (cached ??= createYourselfAuth({
      issuer: env.YOURSELF_ISSUER,
      clientId: env.YOURSELF_CLIENT_ID,
      clientSecret: env.YOURSELF_CLIENT_SECRET,
      redirectUri: env.YOURSELF_REDIRECT_URI,
    }));
    
    app.get("/auth/login", (c) => auth(c.env).signIn(c.req.raw));
    app.get("/auth/callback", (c) => auth(c.env).handleCallback(c.req.raw));
    app.post("/auth/logout", (c) => auth(c.env).signOut());
    
    app.use("*", async (c, next) => {
      const user = await auth(c.env).getUser(c.req.raw);
      if (!user) return c.redirect(`/auth/login?return_to=${encodeURIComponent(c.req.path)}`);
      c.set("user", user);
      await next();
    });
    
    app.get("/", (c) => c.text(`Hello, ${c.get("user").name}`));
    
    export default app;
  4. Not on Workers?

    On Node or Bun, read the variables from process.env instead of c.env: const env = process.env as Bindings and pass that to auth(env).

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