Authentication Best Practices
This guide covers secure authentication practices for integrating with Fanfare.API Key Management
Types of Keys
| Key Type | Format | Usage | Exposure |
|---|---|---|---|
| Publishable Key | pk_test_... / pk_live_... | Client-side SDK | Safe for clients |
| Secret Key | sk_test_... / sk_live_... | Server-side API | Never expose |
Publishable Keys
Publishable keys are designed for client-side use:- Initialize SDK
- Enter experiences
- Check queue status
- Consumer authentication
- Cannot access admin functions
- Cannot read other users’ data
- Rate limited more aggressively
Secret Keys
Secret keys provide full API access:Secure Key Storage
Environment Variables:- AWS Secrets Manager
- Google Secret Manager
- HashiCorp Vault
- Vercel Environment Variables
Key Rotation
Rotate keys periodically or after potential exposure:- Generate new key in dashboard
- Update your application
- Test thoroughly
- Revoke old key
Consumer Authentication
Authentication Methods
Fanfare supports multiple consumer authentication methods:| Method | Use Case | Security Level |
|---|---|---|
| Guest | Anonymous access | Basic |
| Email OTP | Verified email | Medium |
| Phone OTP | Verified phone | Medium |
| SSO | Enterprise | High |
Guest Authentication
For anonymous access with device binding:OTP Authentication
For verified consumer identity:- Codes expire after 10 minutes
- Limited retry attempts
- Rate limited requests
- Invalid codes logged for monitoring
Session Management
Token Security
JWT Structure
Consumer tokens are JWTs with these claims:Token Handling
Don't store tokens in localStorage for sensitive apps
Don't store tokens in localStorage for sensitive apps
localStorage is vulnerable to XSS attacks. For high-security applications, consider httpOnly cookies.
Don't include tokens in URLs
Don't include tokens in URLs
URLs can be logged, cached, and shared. Use headers for token transmission.
Do handle token expiration
Do handle token expiration
Do validate tokens server-side
Do validate tokens server-side
When processing handoff tokens, always validate on your server.
Server-Side Authentication
Validating Consumer Requests
When consumers interact with your backend:Webhook Authentication
Verify webhook signatures:Security Checklist
Development
- Use test keys only in development
- Never commit keys to version control
- Add
.envto.gitignore - Use separate test and production keys
Production
- Use live keys in production
- Store keys in secrets manager
- Enable key rotation policy
- Monitor for unauthorized access
- Set up alerts for authentication failures
Code Review
- No hardcoded credentials
- Secret keys only in server-side code
- Token handling follows best practices
- Webhook signatures verified
- Error messages don’t leak sensitive info
Common Mistakes
Exposing secret keys
Wrong: Including sk_ keys in client bundles
Right: Only use pk_ keys client-side
Not validating handoffs
Wrong: Trusting client-sent handoff tokens Right: Always validate tokens server-side
Ignoring token expiration
Wrong: Assuming tokens are always valid Right: Handle expiration and refresh
Skipping webhook verification
Wrong: Processing webhooks without checking signature
Right: Always verify webhook signatures
Related Resources
- Security Overview - Security architecture
- Webhook Debugging - Webhook security
- API Errors - Auth error handling