🚀 Exciting Announcement: I've developed a Flutter plugin designed to streamline the secure storage and efficient management of access and refresh tokens! 🗝️
Explore it here: Flutter Secure Token Manager
If you prefer not to use the plugin, you can proceed with this example. Happy coding! 🌟
Efficiently handle access token expiration with a single request, optimizing performance during concurrent API calls.
-
Copy the
TokenManager
class into your project. -
If your token structure differs, modify the
Token
class within theTokenManager
according to your specific requirements. -
Update the
isTokenExpired
method if your authentication process varies. This method checks if the current token is expired. -
Modify the
renewAccessToken
method to align with your token renewal logic. This method is invoked when the token expires, and a new token needs to be obtained. -
Ensure to call the
setToken
method after a successful login or when the app is opened (if the user has an active session). This initializes the token manager with the user's current token. -
Include the following line in the headers of your API call method to attach the authorization token:
"Authorization": "Bearer ${await TokenManager.instance.getAccessToken()}"
This ensures that the authorization header is added to your API calls requiring authentication. It doesn't matter which API client you use (http, dio, or others); make sure this header is included in the request.