Astro (server output)

Endpoints for sign-in and middleware that guards every page.

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

  1. Environment

    .env
    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:4321/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/lib/yourself-auth.ts
  3. Configure it

    src/lib/auth.ts
    import { createYourselfAuth } from "./yourself-auth";
    
    export const auth = createYourselfAuth({
      issuer: import.meta.env.YOURSELF_ISSUER!,
      clientId: import.meta.env.YOURSELF_CLIENT_ID!,
      clientSecret: import.meta.env.YOURSELF_CLIENT_SECRET!,
      redirectUri: import.meta.env.YOURSELF_REDIRECT_URI!,
    });
  4. Endpoints

    src/pages/auth/[action].ts
    import type { APIRoute } from "astro";
    import { auth } from "../../lib/auth";
    
    export const GET: APIRoute = ({ request, params }) => {
      if (params.action === "login") return auth.signIn(request);
      if (params.action === "callback") return auth.handleCallback(request);
      return new Response("Not found", { status: 404 });
    };
    
    export const POST: APIRoute = ({ params }) =>
      params.action === "logout" ? auth.signOut() : new Response("Not found", { status: 404 });
  5. Guard every page

    src/middleware.ts
    import { defineMiddleware } from "astro:middleware";
    import { auth } from "./lib/auth";
    
    export const onRequest = defineMiddleware(async (context, next) => {
      if (context.url.pathname.startsWith("/auth/")) return next();
      const user = await auth.getUser(context.request);
      if (!user) return context.redirect(`/auth/login?return_to=${encodeURIComponent(context.url.pathname)}`);
      context.locals.user = user;
      return next();
    });
  6. 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.