Overview
Flet’s authentication system consists of:- OAuth Providers - Pre-configured providers (Google, GitHub, Azure, Auth0) and base classes for custom providers
- Authorization Service - Handles OAuth flows, token management, and automatic refresh
- Page.login() - Built-in page method to trigger authentication
- User & Token APIs - Access user profile and token data
flet/auth/__init__.py:1
Quick Start
Here’s a minimal OAuth authentication example:OAuth Providers
Flet includes providers for major identity platforms:Google OAuth
- Go to Google Cloud Console
- Create a new project or select existing
- Enable Google+ API
- Create OAuth 2.0 credentials
- Add authorized redirect URI:
http://localhost:8550/oauth_callback
GitHub OAuth
- Go to GitHub Settings → Developer settings → OAuth Apps
- Create a new OAuth App
- Set Authorization callback URL:
http://localhost:8550/oauth_callback
Azure AD OAuth
Auth0 OAuth
Authorization Service
TheAuthorizationService manages the complete OAuth flow:
Location: flet/auth/authorization_service.py:19
Authorization Flow
Location:flet/auth/authorization_service.py:90
- Get authorization URL:
- Exchange code for token:
flet/auth/authorization_service.py:111
- Access token:
flet/auth/authorization_service.py:78
Token Persistence
Save and restore tokens for persistent sessions: Location:flet/auth/authorization_service.py:62
Automatic Token Refresh
Tokens are automatically refreshed when expired: Location:flet/auth/authorization_service.py:188
User Profile
Access authenticated user information:OAuth Token
Access token details:Custom OAuth Provider
Create a custom provider by extendingOAuthProvider:
Location: flet/auth/oauth_provider.py:7
PKCE Support
For public clients, use PKCE (Proof Key for Code Exchange):Authentication with Components
Combine authentication with component-based architecture:Token Storage Best Practices
- Never store tokens in plain text - Use secure storage mechanisms
- Encrypt tokens at rest - Use platform-specific secure storage
- Use short-lived tokens - Rely on refresh tokens for long sessions
- Implement logout - Clear stored tokens on logout
Error Handling
Complete Example: Multi-Provider Auth
Security Considerations
- Use HTTPS in production - Never use OAuth over HTTP
- Validate redirect URLs - Ensure redirect URLs match registered callbacks
- Implement CSRF protection - Validate the
stateparameter - Rotate client secrets - Regularly update OAuth credentials
- Limit token scope - Request only necessary permissions
- Implement token revocation - Provide logout functionality
- Monitor for suspicious activity - Log authentication events
Next Steps
- Build Components with protected routes
- Use Hooks for auth state management
- Implement Testing for auth flows