Skip to main content

Server-Side Setup

1. Configure BetterAuth

2. Environment Variables in apps/server/.env

Client-Side Implementation

1. Auth Client Setup

2. Social Login Buttons

Authentication Flow

  1. User clicks social provider button
  2. Redirect to provider’s login page
  3. User authenticates with provider
  4. Provider redirects back with code
  5. Exchange code for tokens
  6. Create or update user account
  7. Create session
  8. Redirect to dashboard

Security Features

  1. OAuth Security
    • State parameter validation
    • PKCE for mobile apps
    • Secure token storage
    • Token refresh handling
  2. Account Security
    • Email verification
    • Account linking
    • Session management
    • Rate limiting
  3. Data Security
    • Secure user data storage
    • Privacy compliance
    • Data encryption
    • Access control

Best Practices

  1. Error Handling
    • Show user-friendly error messages
    • Log authentication errors
    • Handle edge cases
    • Provide fallback options
  2. User Experience
    • Clear provider buttons
    • Loading states
    • Success/error feedback
    • Account linking options
  3. Security
    • HTTPS in production
    • Rate limiting
    • Input validation
    • Token validation

Adding New Providers

To add a new social provider:
  1. Register your application with the provider
  2. Get client ID and secret
  3. Add provider configuration to BetterAuth
  4. Create provider button component
  5. Handle provider-specific scopes
  6. Test the integration
Example for adding GitHub:

Common Issues & Solutions

  1. Callback URL Issues
    • Verify callback URLs in provider dashboard
    • Check CORS configuration
    • Validate redirect URIs
  2. Token Issues
    • Check token expiration
    • Verify token storage
    • Handle token refresh
  3. Account Linking
    • Handle existing accounts
    • Manage multiple providers
    • Resolve conflicts

Next Steps