Nostr WoT

Autenticar una sesión HTTP

Autenticar una sesión HTTP

Usa @nostr-wot/auth en el servidor y /client en el navegador. Monta las cuatro rutas del ejemplo y llama a loginWithSigner desde una acción del usuario. La sesión se crea con un evento NIP-98 de tipo 27235.

terminal
$npm install @nostr-wot/auth@5.0.0 nostr-wot-sdk@3.0.0 nostr-tools

Ejemplo

typescript
// Server only: lib/auth.ts
import { createAuthService } from "@nostr-wot/auth";
import { createNextHandlers } from "@nostr-wot/auth/next";

const secret = process.env.NOSTR_AUTH_SECRET;
if (!secret) throw new Error("Configure NOSTR_AUTH_SECRET on the server");
export const handlers = createNextHandlers(createAuthService({
  secret, expectedVerifyUrl: "https://example.com/api/auth/verify",
  jwtIssuer: "https://example.com", jwtTtlSec: 3600,
}), { cookie: true, cookieAttrs: { secure: true, sameSite: "Lax" } });

// Each export belongs in its own Next.js route.ts file:
// app/api/auth/challenge/route.ts: export const POST = handlers.challenge;
// app/api/auth/verify/route.ts:    export const POST = handlers.verify;
// app/api/auth/me/route.ts:        export const GET = handlers.me;
// app/api/auth/logout/route.ts:    export const POST = handlers.logout;
// Import { handlers } from "@/lib/auth" in each route.

// Separate browser module, called by a login button:
// import { loginWithSigner } from "@nostr-wot/auth/client";
// import { Nip07Signer } from "nostr-wot-sdk/signers";
// await loginWithSigner({ baseUrl: "/api/auth", signer: new Nip07Signer() });

Notas de integración

Sustituye example.com por el origen y la URL de verificación exactos. Genera un secreto aleatorio robusto en el servidor; no lo expongas con NEXT_PUBLIC. Verifica la sesión y los permisos en las rutas protegidas. El desafío predeterminado no mantiene estado: el uso único estricto requiere almacenamiento de consumo único. El tipo 22242 autentica relays, no sesiones HTTP.

Referencia y opciones adicionales