login_client package provides a robust OAuth2-compliant authentication client with automatic token refresh, credential storage, and easy integration into existing Flutter apps.
What is LoginClient?
LoginClient is an HTTP client wrapper that:- Implements OAuth2 authentication flows
- Automatically refreshes expired tokens
- Securely stores credentials
- Handles 401 errors with automatic retry
- Provides a stream of credential changes for logout detection
Installation
Add dependencies
Add both The
login_client and login_client_flutter to your pubspec.yaml:login_client_flutter package provides FlutterSecureCredentialsStorage for secure credential storage.Basic Setup
Initialize LoginClient
Create a LoginClient instance with your OAuth settings and credentials storage:Always call
initialize() after creating the LoginClient to restore previously saved credentials.OAuth Settings
TheOAuthSettings class configures your OAuth2 authentication:
| Parameter | Description | Required |
|---|---|---|
authorizationUri | The OAuth2 token endpoint URL | Yes |
clientId | Your application’s client identifier | Yes |
clientSecret | Secret for confidential clients | No |
scopes | List of permission scopes to request | No |
Authentication Strategies
LoginClient supports multiple OAuth2 authentication strategies.Resource Owner Password Strategy
The most common strategy for mobile apps - authenticate with username and password:- Basic Login
- With Loading State
- With Form Validation
Client Credentials Strategy
For machine-to-machine authentication without user credentials:This strategy requires
clientSecret to be set in OAuthSettings.Custom Grant Strategy
For custom OAuth2 grant types:Raw Credentials Strategy
Use existing OAuth2 credentials directly:Making Authenticated Requests
LoginClient extendshttp.Client, so you can use it like a regular HTTP client:
Automatic Token Refresh
When a request receives a 401 response, LoginClient automatically:- Refreshes the access token using the refresh token
- Retries the original request with the new token
- Logs out the user if refresh fails
Managing Authentication State
Check Login Status
Listen to Credential Changes
Use the credentials stream to react to authentication state changes:Manual Token Refresh
Force a token refresh (useful for updating scopes):Logout
Log out the user and clear stored credentials:Credential Storage
Secure Storage (Recommended)
UseFlutterSecureCredentialsStorage for production apps:
- iOS: Keychain
- Android: EncryptedSharedPreferences
- Web: Web Crypto API
- Linux: libsecret
- Windows: Credential Manager
- macOS: Keychain
In-Memory Storage (Testing Only)
For testing or development, use in-memory storage:Custom Credential Storage
Implement your own storage mechanism:Integration with CQRS
LoginClient works seamlessly with the CQRS package:Advanced Features
Custom HTTP Client
Provide a custom HTTP client for advanced networking:Custom Logging
Add custom logging to debug authentication issues:Access Current Credentials
Read the current credentials directly:Complete Example
Best Practices
Always initialize LoginClient on app start
Always initialize LoginClient on app start
Call
await loginClient.initialize() before running your app to restore saved credentials:Handle credential changes for auto-logout
Handle credential changes for auto-logout
Listen to
onCredentialsChanged to detect session expiration:Use FlutterSecureCredentialsStorage in production
Use FlutterSecureCredentialsStorage in production
Never use
InMemoryCredentialsStorage in production. Always use secure storage:Dispose LoginClient when done
Dispose LoginClient when done
Call
dispose() to clean up resources:Next Steps
- Integrate with CQRS for backend communication
- Add analytics tracking to monitor authentication events
- Set up custom lint rules for code quality
