feat: Agent Signup & Verify endpoints

Merged
RE
7mo ago
+1696-0

Summary

  • Add POST /api/agents/signup — unauthenticated endpoint for agent registration
    • agent+ prefix emails get instant API key on first signup
    • All other cases (new normal email or existing account) send a 6-digit verification code via Resend
    • Uniform response shape prevents email enumeration
  • Add POST /api/agents/verify — unauthenticated endpoint for email verification
    • Validates code against hash stored in Privy custom_metadata
    • Rate-limited to 5 attempts, 24h expiry
    • Returns API key on success, clears metadata
  • New Privy REST helpers: createPrivyUser, getPrivyUserByEmail, setPrivyCustomMetadata
  • Zod validation for both request bodies
  • 23 tests across 5 test files (validators, handlers, utility)

Test plan

  • All 23 unit tests pass (pnpm test lib/agents/__tests__/)
  • POST /api/agents/signup with agent+ email returns api_key immediately
  • POST /api/agents/signup with normal email returns api_key: null and sends verification email
  • POST /api/agents/signup with existing account email sends verification code
  • POST /api/agents/verify with correct code returns api_key
  • POST /api/agents/verify with wrong code increments attempts and returns 400
  • POST /api/agents/verify after 5 failures returns 429
  • POST /api/agents/verify with expired code returns 400
  • Returned api_key works on authenticated endpoints (e.g. GET /api/artists)

🤖 Generated with Claude Code


Summary by cubic

Adds unauthenticated agent signup and email verification endpoints for programmatic onboarding. agent+ emails get an instant API key; others verify via a 6‑digit code with strict rate/expiry checks.

  • New Features

    • POST /api/agents/signup: register by email; agent+ gets immediate API key; others get a 6‑digit code. Uniform response shape. CORS and OPTIONS supported.
    • POST /api/agents/verify: validate 6‑digit code via Privy metadata (24h expiry, 5 attempts). On success, issue an API key and clear metadata. CORS and OPTIONS supported.
  • Bug Fixes

    • Expiry safety: treat missing or malformed verification_expires_at as expired (NaN‑guard).
    • Reliability: on internal signup errors, return 500 (body stays generic) to enable retries and monitoring.
    • Correct Privy request body uses { address }; consistent hashing via PRIVY_PROJECT_SECRET; inclusive 6‑digit code range; never return keys if DB insert fails; remove org auto‑assignment from agent signup.

Summary by CodeRabbit

  • New Features

    • Agent signup endpoint: email-based registration that instantly issues account+API key for agent+ prefixed addresses, and creates accounts or sends verification codes for other addresses.
    • Agent verification endpoint: submit a 6-digit code to receive an API key for verified accounts.
    • Email delivery and external user management integrated; CORS preflight supported.
  • Safety / Reliability

    • Verification safeguards: code expiry (24h) and max failed attempts (5).
    • Consistent error handling and fallback responses.