Autenticare una sessione HTTP
Autenticare una sessione HTTP
Usa @nostr-wot/auth sul server e /client nel browser. Monta le quattro rotte e chiama loginWithSigner da un’azione utente. La sessione usa un evento NIP-98 di tipo 27235.
terminale
$npm install @nostr-wot/auth@5.0.0 nostr-wot-sdk@3.0.0 nostr-tools
Esempio
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() });Note di integrazione
Sostituisci example.com con origine e URL di verifica esatti. Genera un segreto casuale robusto sul server, mai in NEXT_PUBLIC. Verifica sessione e permessi sulle rotte protette. La challenge è stateless: l’uso unico rigoroso richiede un archivio di consumo. Il tipo 22242 autentica relay, non HTTP.