Autenticar uma sessão HTTP
Autenticar uma sessão HTTP
Use @nostr-wot/auth no servidor e /client no navegador. Monte as quatro rotas e chame loginWithSigner por uma ação do usuário. A sessão usa um evento NIP-98 de tipo 27235.
terminal
$npm install @nostr-wot/auth@5.0.0 nostr-wot-sdk@3.0.0 nostr-tools
Exemplo
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 integração
Substitua example.com pela origem e URL de verificação exatas. Gere um segredo aleatório forte no servidor, nunca em NEXT_PUBLIC. Verifique sessão e permissões nas rotas protegidas. O desafio não guarda estado: uso único estrito exige armazenamento de consumo. O tipo 22242 autentica relays, não HTTP.