GithubHelp home page GithubHelp logo

binhlam10796 / dio_refresh_token Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 11 KB

Flutter package for managing and refreshing tokens using Dio. Includes token storage, automatic header injection, and customizable refresh strategies.

License: MIT License

Dart 100.00%

dio_refresh_token's Introduction

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

Install the package:

flutter pub get

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:

  1. Import the necessary packages:
import 'package:dio/dio.dart';
import 'package:dio_refresh_token/dio_refresh_token.dart';
  1. Create and configure your Dio instance:
final dio = Dio();
  1. Create an instance of TokenManager:
final tokenManager = TokenManagerImpl();
  1. 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'],
);

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'},
      );
    }
  • 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']
  • 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']
  1. Add the TokenInterceptor to your Dio instance:
dio.interceptors.add(TokenInterceptor(
  tokenManager: tokenManager,
  tokenRefreshStrategy: tokenRefreshStrategy,
));
  1. Now you can use Dio as usual:
final response = await dio.get('https://api.example.com/data');
print(response.data);

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();
}

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.

dio_refresh_token's People

Contributors

binhlam10796 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.