dio_refresh 1.0.2 copy "dio_refresh: ^1.0.2" to clipboard
dio_refresh: ^1.0.2 copied to clipboard

A flutter package for intercepting requests and automatically fetching refresh tokens on API failures

example/lib/main.dart

import 'package:dio/dio.dart';
import 'package:dio_refresh/dio_refresh.dart';

void main() {
  final dio = Dio();

  // Define the TokenManager instance.
  final tokenManager = TokenManager.instance;
  tokenManager.setToken(
    TokenStore(
      accessToken: "authToken",
      refreshToken: "refreshToken",
    ),
  );

  // Add the DioRefreshInterceptor.
  dio.interceptors.add(DioRefreshInterceptor(
    tokenManager: tokenManager,
    authHeader: (tokenStore) {
      if (tokenStore.accessToken == null) {
        return {};
      }
      return {
        'Authorization': 'Bearer ${tokenStore.accessToken}',
      };
    },
    shouldRefresh: (response) =>
        response?.statusCode == 401 || response?.statusCode == 403,
    onRefresh: (dio, tokenStore) async {
      final response = await dio.post('/refresh', data: {
        'refresh_token': tokenStore.refreshToken,
      });
      return TokenStore(
        accessToken: response.data['accessToken'],
        refreshToken: response.data['refreshToken'],
      );
    },
  ));
}
4
likes
160
points
264
downloads

Publisher

verified publisheriamdipanshus.in

Weekly Downloads

A flutter package for intercepting requests and automatically fetching refresh tokens on API failures

Repository (GitHub)
View/report issues

Topics

#dio #http #network #interceptor #middleware

Documentation

API reference

License

MIT (license)

Dependencies

dio, flutter

More

Packages that depend on dio_refresh