Skip to main content

Table of Contents

Overview

For the framework-agnostic linkage model (ensureCustomer(externalRef) from your backend after your own IdP login), see Customer linkage (bring your own client and IdP). SolvaPay SDK uses authentication adapters to extract user IDs from requests. There are two types of adapters:
  1. Server-Side Adapters (@solvapay/auth) - Extract user IDs from HTTP requests in API routes
  2. Client-Side Adapters (@solvapay/react) - Extract user IDs and tokens from client-side auth state

Server-Side Adapters

Server-side adapters are used with @solvapay/server to extract user IDs from HTTP requests in API routes, Express endpoints, and other server-side contexts.

Interface

Basic Example: JWT Token Adapter

Example: Session-Based Adapter

Example: Custom Header Adapter

Using with SolvaPay Server SDK

Client-Side Adapters

Client-side adapters are used with @solvapay/react to extract user IDs and tokens from client-side authentication state.

Interface

Basic Example: LocalStorage Adapter

Example: Context-Based Adapter

Example: Async Storage Adapter (React Native)

Common Patterns

Pattern 1: JWT Token with User ID Extraction

Pattern 3: API Key with User Mapping

Testing Adapters

Mock Adapter for Testing

Testing with Adapters

Complete Examples

Example 1: Firebase Auth Adapter (Client-Side)

Example 2: Auth0 (Next.js App Router, v4)

Use @solvapay/auth/auth0 with @solvapay/next/middleware and @solvapay/react/auth0 on the client. The canonical runnable reference is examples/nextjs-auth0. Auth0 setup: create a Regular Web Application in the Auth0 Dashboard (httpOnly session cookie — not SPA/Native). If your IdP uses a short access-token TTL at the integrator edge (e.g. 24h), that stays on your side; SolvaPay keys customers on the stable Auth0 sub, so token expiry does not invalidate the customer mapping. Server middleware (proxy.ts at project root):
The middleware forwards two headers to downstream SolvaPay route handlers server-side only:
  • x-user-id — Auth0 sub, used as externalRef / customer reference
  • Authorization: Bearer <id_token> — optional Auth0 ID token for email/name on first customer create
Never send Auth0 access tokens from the browser to SolvaPay APIs. SolvaPay billing calls use your sk_* secret server-side. Client provider:
Auth0 keeps the session in an httpOnly cookie. The client adapter reports whether a user is signed in; proxy.ts bridges the session into x-user-id for API routes.

Example 3: Custom OAuth Adapter

Best Practices

  1. Never Throw: Adapters should never throw exceptions. Return null if authentication fails.
  2. Handle Errors Gracefully: Catch all errors and return null instead of throwing.
  3. Cache When Possible: Cache expensive operations (like token verification) when appropriate.
  4. Type Safety: Use TypeScript for better type safety and developer experience.
  5. Test Thoroughly: Write tests for your adapters, including edge cases.
  6. Documentation: Document your adapter’s behavior and requirements.

Next Steps