localize_token_manager 1.0.1+7
localize_token_manager: ^1.0.1+7 copied to clipboard
A clean, customizable API service package with Dio and refresh token handling
A powerful set of tools for managing authentication tokens in Dart and Flutter applications.
A Dart package for seamless, secure, and automatic management of authentication tokens in your applications. localize_token_manager simplifies token injection, refresh, and multi-tenant support, making it easy to integrate robust authentication flows with minimal setup.
🚀 Features #
- 🔐 Automatic Token Management – Seamless injection and refresh of tokens
- 🔄 Auto Token Refresh – Effortlessly handles expired tokens
- 🛡️ Secure by Design – Follows security best practices
- 📊 Request/Response Logging – Optional, easy-to-enable logging
- 🚀 Type-Safe – Generic response types with built-in JSON parsing
- ⚡ Simple API – Intuitive and easy to integrate
- 🔧 Highly Customizable – Flexible configuration for any use case
- 🌐 Multi-Tenant Support – Effortless handling of multiple tenants or domains
➡️ Getting Started #
1. Install the package #
For Dart:
dart pub add localize_token_manager
For Flutter:
flutter pub add localize_token_manager
2. Import the package #
Add this import to your Dart code:
import 'package:localize_token_manager/localize_token_manager.dart';
3. Initialize and use #
See the Usage section below for a complete example of initialization and making requests.
✅ Usage #
import 'package:localize_token_manager/localize_token_manager.dart';
void main() {
// Initialize the token manager
final tokenManager = LocalizeTokenManager(
baseUrl: 'https://api.example.com',
enabledLogging: true, // Enable request/response logging
);
Future<dynamic> request({
required String endpoint,
required HttpMethod method,
Map<String, dynamic>? body,
Map<String, String>? headers,
}) async {
final LocalizeTokenManager api = LocalizeTokenManager(
baseUrl: Environment.baseUrl,
enabledLogging: true,
);
api.initialize(
tokenProvider: () async => await SecureStorageHelper.getAccessToken(),
refreshToken: () {
final obj = {
"name": "refresh_token",
"requestUrl": 'request_url',
};
return obj;
},
tenantKey: () async => await SecureStorageHelper.getTenantKey(),
onUnauthorized: () async {
// Handle logout / redirect to login
// await LocalizeTokenManager.logOut();
// Use Navigator or your auth state management to go to login screen
print('➡️ Unauthorized - Logging out');
},
);
final response = await api.request(
endpoint: endpoint,
method: method,
body: body,
headers: headers,
);
return response;
}
}