dio_refresh_token

Creator: coderz1093

Last updated:

0 purchases

dio_refresh_token Image
dio_refresh_token Images

Languages

Categories

Add to Cart

Description:

dio refresh token

Dio Token Manager and Refresher #
A Flutter package for managing and refreshing tokens using Dio. Includes token storage, automatic header injection, and customizable refresh strategies.
Features #

Manage access and refresh tokens securely.
Automatically add authorization headers to requests.
Customizable token refresh strategies.
Easy integration with Dio interceptors.

Installation #
Add dio_refresh_token to your pubspec.yaml:
dependencies:
dio_refresh_token: ^0.0.1
copied to clipboard
Install the package:
flutter pub get
copied to clipboard
Usage #
Adding the Interceptor #
To use the TokenInterceptor, you need to add it to your Dio instance. Here's an example of how to do this:

Import the necessary packages:

import 'package:dio/dio.dart';
import 'package:dio_refresh_token/dio_refresh_token.dart';
copied to clipboard

Create and configure your Dio instance:

final dio = Dio();
copied to clipboard

Create an instance of TokenManager:

final tokenManager = TokenManagerImpl();
copied to clipboard

Define your token refresh logic by creating an instance of TokenRefreshStrategyImpl:

final tokenRefreshStrategy = TokenRefreshStrategyImpl(
refreshHandler: (dio, refreshToken) async {
// Implement your refresh token logic here
return Response(
requestOptions: RequestOptions(path: ''),
statusCode: 200,
data: {'access_token': 'newAccessToken', 'refresh_token': 'newRefreshToken'},
);
},
accessTokenExtractor: (response) => response.data['access_token'],
refreshTokenExtractor: (response) => response.data['refresh_token'],
);
copied to clipboard
Parameters:


refreshHandler: A function that takes a Dio instance and a refresh token as parameters. This function should implement the logic to request a new access token using the refresh token. It should return a Response containing the new tokens.
Example:
(dio, refreshToken) async {
// Request new tokens
return Response(
requestOptions: RequestOptions(path: ''),
statusCode: 200,
data: {'access_token': 'newAccessToken', 'refresh_token': 'newRefreshToken'},
);
}
copied to clipboard


accessTokenExtractor: A function that takes a Response as a parameter and extracts the new access token from the response.
Example:
(response) => response.data['access_token']
copied to clipboard


refreshTokenExtractor: A function that takes a Response as a parameter and extracts the new refresh token from the response.
Example:
(response) => response.data['refresh_token']
copied to clipboard



Add the TokenInterceptor to your Dio instance:

dio.interceptors.add(TokenInterceptor(
tokenManager: tokenManager,
tokenRefreshStrategy: tokenRefreshStrategy,
));
copied to clipboard

Now you can use Dio as usual:

final response = await dio.get('https://api.example.com/data');
print(response.data);
copied to clipboard
Using TokenManager #
The TokenManager class provides methods to manage tokens. Here's how to use it:
import 'package:dio_refresh_token/dio_refresh_token.dart';

void main() async {
final tokenManager = TokenManagerImpl();

// Save tokens
await tokenManager.saveAccessToken('your_access_token');
await tokenManager.saveRefreshToken('your_refresh_token');

// Get tokens
final accessToken = await tokenManager.getAccessToken();
final refreshToken = await tokenManager.getRefreshToken();

// Clear tokens
await tokenManager.clearAccessToken();
await tokenManager.clearRefreshToken();
}
copied to clipboard
Contributions #
Contributions are welcome! If you encounter any issues or have suggestions for improvements, please feel free to submit a pull request or create an issue on the GitHub repository.
License #
This project is licensed under the MIT License - see the LICENSE file for details.

License

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Files In This Product:

Customer Reviews

There are no reviews.