Files
better-auth/LEARNING_GUIDE.md
T
Claude e49fc2b9fc docs: add comprehensive learning guides for payment gateway abstraction
Created four detailed guides to help understand better-auth patterns and
apply them to building a payment gateway abstraction library:

- LEARNING_GUIDE.md: 17-file study roadmap organized in 5 phases
- OAUTH_TO_PAYMENT_MAPPING.md: Mental model showing OAuth-to-Payment parallels
- QUICK_START_IMPLEMENTATION.md: Complete implementation guide with code
- STUDY_CHECKLIST.md: Interactive checklist to track learning progress

These guides show how to learn from better-auth's provider abstraction,
plugin system, and configuration patterns to build a "better-payments"
library that makes integrating Stripe, Flutterwave, PayStack, and other
payment gateways as simple as better-auth makes OAuth providers.
2025-11-15 16:22:57 +00:00

12 KiB

Better-Auth Learning Guide for Payment Gateway Project

This guide will help you understand the better-auth codebase to build your payment gateway abstraction library.

📚 Learning Path - Study These Files in Order

PHASE 1: Understanding Provider Abstraction (START HERE)

1. Provider Interface Definition

File: packages/core/src/oauth2/oauth-provider.ts Lines to focus on: 14-83 What to learn:

  • How a provider interface is structured
  • What methods every provider must implement
  • How TypeScript generics are used for type safety
  • The difference between required and optional methods

Key concepts:

- OAuthProvider<T, O> interface
- createAuthorizationURL() - how providers start flows
- validateAuthorizationCode() - how providers verify results
- getUserInfo() - how providers fetch data
- Optional methods (refreshAccessToken, revokeToken)

Your equivalent:

PaymentProvider<T, O>
- createPaymentIntent()
- verifyPayment()
- handleWebhook()
- Optional: refundPayment(), createSubscription()

2. Simple Provider Implementation

File: packages/core/src/social-providers/github.ts Lines to focus on: 1-200 What to learn:

  • How to implement the provider interface
  • How to define provider-specific options
  • How to make API calls to external services
  • How to transform external data to standard format

Key concepts:

- GithubProfile interface (provider-specific data)
- GithubOptions interface (extends ProviderOptions)
- github() factory function
- API endpoint configuration
- Error handling

Why GitHub first: Simpler than Google (no OIDC), shows basic patterns clearly


3. Complex Provider Implementation

File: packages/core/src/social-providers/google.ts Lines to focus on: 1-250 What to learn:

  • Advanced provider features (ID token verification)
  • Provider-specific options (accessType, display, hd)
  • How to handle different authentication flows
  • Token refresh implementation

Key concepts:

- GoogleProfile interface
- GoogleOptions with provider-specific fields
- verifyIdToken() for OIDC
- refreshAccessToken() implementation
- Using helper functions (createAuthorizationURL, validateAuthorizationCode)

4. Provider with Environment Handling

File: packages/core/src/social-providers/paypal.ts Lines to focus on: 1-150 What to learn:

  • How to handle sandbox vs production environments
  • Dynamic endpoint configuration
  • Environment-based API URLs

Key concepts:

- PayPalOptions with environment field
- Conditional endpoint URLs (sandbox vs live)
- Environment-aware token endpoints

Why this matters for you: Payment gateways also have sandbox/production modes!


5. Provider Registry and Exports

File: packages/core/src/social-providers/index.ts Lines to focus on: 1-100 What to learn:

  • How to organize multiple providers
  • Type-safe provider registration
  • Creating enums from provider names
  • Configuration type generation

Key concepts:

- socialProviders object (registry)
- socialProviderList array
- SocialProviders type (for configuration)
- Type-safe provider keys

PHASE 2: Understanding Configuration System

6. Main Configuration Interface

File: packages/core/src/types/init-options.ts Lines to focus on: 1-300 What to learn:

  • How to structure main configuration
  • Nested configuration objects
  • Optional vs required fields
  • Default values and documentation

Key concepts:

- BetterAuthOptions interface
- socialProviders configuration
- emailAndPassword configuration
- plugins array
- advanced options

Your equivalent:

PaymentGatewayOptions
- providers: PaymentProviders
- defaultCurrency
- webhooks
- plugins
- advanced

7. Provider Options Type

File: packages/core/src/oauth2/oauth-provider.ts Lines to focus on: 85-120 What to learn:

  • Base options that all providers share
  • How to allow provider-specific extensions
  • Generic type parameters for flexibility

Key concepts:

- ProviderOptions<Profile> interface
- clientId, clientSecret (common to all)
- getUserInfo override option
- mapProfileToUser customization

PHASE 3: Understanding the Core Factory

8. Main Entry Point

File: packages/better-auth/src/auth/auth.ts Lines to focus on: 1-50 What to learn:

  • How the public API is exposed
  • Simple wrapper pattern
  • Type inference from options

Key concepts:

- betterAuth<Options>() function
- Generic type parameter
- Delegation to createBetterAuth
- Return type: Auth<Options>

Your equivalent:

export const betterPayments = <Options extends PaymentGatewayOptions>(
    options: Options
) => {
    return createPaymentGateway(options);
};

9. Core Factory Function

File: packages/better-auth/src/auth/base.ts Lines to focus on: 1-150 What to learn:

  • How to initialize the system
  • Request handler creation
  • Plugin aggregation
  • Error code collection

Key concepts:

- createBetterAuth() factory
- initFn for async initialization
- handler: async (request: Request)
- Plugin error code aggregation
- Context management

10. Context Creation

File: packages/better-auth/src/context/create-context.ts Lines to focus on: 1-300 What to learn:

  • How to instantiate providers from config
  • Provider validation
  • Context object structure

Key concepts:

- Provider instantiation loop
- Config validation (checking clientId, clientSecret)
- Filtering disabled providers
- Creating provider instances from registry

This is where:

socialProviders: {
    google: { clientId: "...", clientSecret: "..." }
}
// Becomes:
const provider = socialProviders["google"](config);

PHASE 4: Understanding Plugin System

11. Plugin Interface

File: packages/core/src/types/plugin.ts Lines to focus on: 1-200 What to learn:

  • Plugin lifecycle hooks
  • How plugins extend functionality
  • Database schema definition
  • Custom endpoints

Key concepts:

- BetterAuthPlugin interface
- init() lifecycle hook
- endpoints object
- schema definition
- hooks (before/after)
- $ERROR_CODES

12. Simple Plugin Example

File: packages/better-auth/src/plugins/bearer/index.ts Lines to focus on: 1-200 What to learn:

  • How to create a simple plugin
  • Request/response transformation
  • Middleware pattern

Key concepts:

- bearer() plugin factory
- hooks.before and hooks.after
- matcher() function
- handler() implementation
- Header manipulation

Why bearer: Simple, focused plugin that shows core patterns


13. Complex Plugin Example

File: packages/stripe/src/index.ts Lines to focus on: 1-500 What to learn:

  • How to integrate external services
  • Database schema in plugins
  • Custom endpoints
  • Error handling

Key concepts:

- stripe() plugin factory with options
- schema.subscription definition
- Multiple endpoints (get-plans, subscribe, cancel)
- External API integration (Stripe SDK)
- Error codes definition

This is EXACTLY what you're building! A Stripe plugin but as the core concept.


14. Plugin Initialization

File: packages/better-auth/src/context/helpers.ts Lines to focus on: 1-150 (runPluginInit function) What to learn:

  • How plugins are initialized
  • How plugins can modify context and options
  • Plugin composition

Key concepts:

- runPluginInit() function
- Iterating through plugins
- Calling plugin.init()
- Merging options from plugins
- Deep merging with defu

PHASE 5: Understanding Package Structure

15. Monorepo Workspace Config

File: pnpm-workspace.yaml What to learn:

  • How to structure a monorepo
  • Package organization

16. Core Package Structure

File: packages/core/package.json What to learn:

  • How to define exports
  • Dependencies vs peerDependencies
  • Package naming

17. Main Package Structure

File: packages/better-auth/package.json What to learn:

  • How to export plugins
  • Subpath exports pattern
  • Build configuration

🎯 Quick Reference by Concept

Want to learn: Provider Abstraction

Read: Files 1, 2, 3, 4, 5 (in order)

Want to learn: Configuration System

Read: Files 6, 7, 10

Want to learn: Main Entry Point

Read: Files 8, 9

Want to learn: Plugin System

Read: Files 11, 12, 13, 14

Want to learn: Project Structure

Read: Files 15, 16, 17


📝 Study Notes Template

For each file, take notes using this template:

### File: [filename]

**Main Purpose:**
[What does this file do?]

**Key Interfaces/Types:**
- [Type name]: [What it represents]
- [Type name]: [What it represents]

**Key Functions:**
- [Function name]: [What it does]
- [Function name]: [What it does]

**How I'll use this pattern:**
[Your notes on how to apply this to payment gateways]

**Questions:**
[Anything you don't understand]

🚀 Practical Exercise

After studying files 1-5, try this:

Exercise 1: Design your PaymentProvider interface

  • Model it after OAuthProvider
  • Define required methods (create, verify, webhook)
  • Define optional methods (refund, subscription)

Exercise 2: Implement a Stripe provider

  • Model it after google.ts
  • Use the Stripe SDK
  • Follow the same pattern

Exercise 3: Implement a Flutterwave provider

  • Model it after github.ts
  • Use fetch for API calls
  • Transform Flutterwave responses to your standard format

Exercise 4: Create a provider registry

  • Model it after social-providers/index.ts
  • Export all your providers
  • Create type-safe configuration

⏱️ Estimated Time

  • Phase 1 (Provider Abstraction): 3-4 hours
  • Phase 2 (Configuration): 1-2 hours
  • Phase 3 (Core Factory): 2-3 hours
  • Phase 4 (Plugin System): 3-4 hours
  • Phase 5 (Package Structure): 1 hour

Total: ~10-14 hours of focused study


💡 Pro Tips

  1. Don't read everything at once - Follow the phases
  2. Try implementing as you learn - Build your version alongside
  3. Focus on patterns, not details - You don't need to understand every line
  4. Skip complex parts initially - Come back to advanced features later
  5. Compare OAuth flow to Payment flow - They're similar:
    • OAuth: redirect → callback → get token → get user
    • Payment: create intent → redirect → webhook → verify payment

🎓 What You'll Understand

After studying these files, you'll know:

How to create a provider abstraction How to make providers pluggable How to create type-safe configuration How to build a factory function How to create a plugin system How to structure a monorepo How to make your library framework-agnostic


🤔 Common Questions

Q: Do I need to understand everything in better-auth? A: No! Focus on the provider pattern and plugin system. Skip auth-specific logic.

Q: Should I copy the code? A: Copy the patterns, not the code. Adapt it for payments.

Q: What about the database stuff? A: Optional for MVP. Start with just payment provider abstraction.

Q: How similar should my library be? A: Very similar in architecture, different in domain (payments vs auth).


📞 Next Steps

  1. Read Phase 1 files (1-5)
  2. Take notes using the template above
  3. Try Exercise 1-2
  4. Share your progress and ask questions!

Good luck! 🚀