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
- User clicks social provider button
- Redirect to provider’s login page
- User authenticates with provider
- Provider redirects back with code
- Exchange code for tokens
- Create or update user account
- Create session
- Redirect to dashboard
Security Features
-
OAuth Security
- State parameter validation
- PKCE for mobile apps
- Secure token storage
- Token refresh handling
-
Account Security
- Email verification
- Account linking
- Session management
- Rate limiting
-
Data Security
- Secure user data storage
- Privacy compliance
- Data encryption
- Access control
Best Practices
-
Error Handling
- Show user-friendly error messages
- Log authentication errors
- Handle edge cases
- Provide fallback options
-
User Experience
- Clear provider buttons
- Loading states
- Success/error feedback
- Account linking options
-
Security
- HTTPS in production
- Rate limiting
- Input validation
- Token validation
Adding New Providers
To add a new social provider:- Register your application with the provider
- Get client ID and secret
- Add provider configuration to BetterAuth
- Create provider button component
- Handle provider-specific scopes
- Test the integration
Common Issues & Solutions
-
Callback URL Issues
- Verify callback URLs in provider dashboard
- Check CORS configuration
- Validate redirect URIs
-
Token Issues
- Check token expiration
- Verify token storage
- Handle token refresh
-
Account Linking
- Handle existing accounts
- Manage multiple providers
- Resolve conflicts