We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
dio
dio: ^5.7.0
Android, iOS
Default Dio
flutter doctor -v
[✓] Flutter (Channel stable, 3.24.3, on macOS 14.3 23D56 darwin-arm64, locale en-IN) • Flutter version 3.24.3 on channel stable at /Users/admin/Documents/key flutter/flutter_sdk/flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision 2663184aa7 (3 months ago), 2024-09-11 16:27:48 -0500 • Engine revision 36335019a8 • Dart version 3.5.3 • DevTools version 2.37.3 [✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0) • Android SDK at /Users/admin/Library/Android/sdk • Platform android-34, build-tools 34.0.0 • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 17.0.10+0-17.0.10b1087.21-11572160) • All Android licenses accepted. [✓] Xcode - develop for iOS and macOS (Xcode 15.3) • Xcode at /Applications/Xcode.app/Contents/Developer • Build 15E204a • CocoaPods version 1.16.2 [✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome [✓] Android Studio (version 2023.3) • Android Studio at /Applications/Android Studio.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 17.0.10+0-17.0.10b1087.21-11572160) [✓] VS Code (version 1.90.0) • VS Code at /Users/admin/Documents/VSCodeConfig/Visual Studio Code.app/Contents • Flutter extension can be installed from: 🔨 https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter [✓] Connected device (6 available) • Vaibhav’s iPhone (mobile) • 00008030-000959400A52402E • ios • iOS 17.6.1 21G93 • Key AI Iphone 15 (mobile) • 00008120-00186CD90244A01E • ios • iOS 18.0.1 22A3370 • iPhone 15 (mobile) • 1EC45351-DD5E-4F6D-A334-E589C7DA3B70 • ios • com.apple.CoreSimulator.SimRuntime.iOS-17-4 (simulator) • macOS (desktop) • macos • darwin-arm64 • macOS 14.3 23D56 darwin-arm64 • Mac Designed for iPad (desktop) • mac-designed-for-ipad • darwin • macOS 14.3 23D56 darwin-arm64 • Chrome (web) • chrome • web-javascript • Google Chrome 131.0.6778.86 [✓] Network resources • All expected network resources are available. • No issues found!
3.5.3
class AppInterceptors extends QueuedInterceptor { // class AppInterceptors extends Interceptor { late final Dio? _dio; // final AuthRepo authRepo = getIt<AuthRepo>(); AppInterceptors(this._dio); @override // ignore: avoid_void_async void onRequest( RequestOptions options, RequestInterceptorHandler handler) async { String fullUrl = options.baseUrl + options.path; // If the request has query parameters, append them if (options.queryParameters.isNotEmpty) { String queryString = Uri(queryParameters: options.queryParameters).query; fullUrl += '?' + queryString; } if (!options.headers.containsKey("Authorization")) { final session = getIt<SessionService>(); final String? token = await session.getAccessToken(); final String? xMemberId = await session.getMemberId(); final String? xUserId = session.user?.id != null ? session.user?.id : ""; if (token == null) { return super.onRequest(options, handler); } options.headers.putIfAbsent("Authorization", () => "Bearer $token"); if (xUserId != null) { options.headers.putIfAbsent("x-user-id", () => xUserId); } log("${options.method} : ${options.path}", name: "Request Type :"); // log(fullUrl,name: "Request URL :"); log(options.headers.toString(), name: "Request Header :"); log(options.queryParameters.toString(), name: "Request Query Parameters :"); log(options.extra.toString(), name: "Request Extras :"); log(options.data.toString(), name: "Request Body :"); log("$token", name: "Request Token :"); log("Authorization Token Added", name: "Request Token Update"); return super.onRequest(options, handler); // super.onRequest(options, handler) return handler.next(options); } else { super.onRequest(options, handler); return handler.next(options); // return super.onRequest(options, handler); } } @override Future onResponse(Response response, ResponseInterceptorHandler handler) { if (response.headers.value("verifyToken") != null) { //if the header is present, then compare it with the Shared Prefs key // SharedPreferences prefs = await SharedPreferences.getInstance(); // var verifyToken = prefs.get("VerifyToken"); // // if the value is the same as the header, continue with the request // if (options.headers.value("verifyToken") == verifyToken) { // return options; // } } if (response.data is Map) { log(jsonEncode(response.data), name: "Response"); } else if (response.data is String) { log(response.data, name: "Response"); } super.onResponse(response, handler); return Future.value(response); // return handler.next(response); } bool _isRefreshing = false; Completer<void>? _refreshCompleter; @override Future onError(DioException err, ErrorInterceptorHandler handler) async { if (err.response != null && err.response?.statusCode == 401) { final SessionService session = getIt<SessionService>(); if (!_isRefreshing) { _isRefreshing = true; _refreshCompleter = Completer<void>(); try { Response<dynamic>? newTokenResponse = await _refreshToken(handler); /* if (newTokenResponse != null) { // Update the request header with the new access token err.requestOptions.headers['Authorization'] = 'Bearer ${newTokenResponse.data["data"]["token"]}'; _refreshCompleter?.complete(); } else { _refreshCompleter?.completeError(err); }*/ } catch (e) { _refreshCompleter?.completeError(e); } finally { _isRefreshing = false; } } try { final accessToken = await session.getAccessToken(); await _refreshCompleter?.future; err.requestOptions.headers['Authorization'] = 'Bearer ${accessToken}'; final response = await _dio!.fetch(err.requestOptions); handler.resolve(response); } catch (e) { handler.reject(err); } } else { handler.next(err); } super.onError(err, handler); // return Future.value(err); // // return Future.value(err); } /* Future<Map<String, dynamic>?> _refreshToken() async { final SessionService session = getIt<SessionService>(); final NavigationService navigationService = getIt<NavigationService>(); final String? refreshToken = await session.getRefreshToken(); final response = await _dio ?.patch("https://dev-auth-api.bprnt.com/api/v1/auth/refresh-token",data: { "refreshToken": refreshToken }); if (response?.statusCode == 200) { await session.saveSession(session.user!.copyWith( refreshToken: response?.data["data"]["refreshToken"], token: response?.data["data"]["token"] )); return response?.data["data"]; } else { navigationService.currentContext?.read<LoginCubit>().logout(isUnauthorised: true); return null; } }*/ Future<Response<dynamic>?> _refreshToken(ErrorInterceptorHandler handler) async { final SessionService session = getIt<SessionService>(); final NavigationService navigationService = getIt<NavigationService>(); try { final String? refreshToken = await session.getRefreshToken(); final response = await _dio?.patch( "${Constants.devAuthBaseUrl}refresh-token", data: {"refreshToken": refreshToken}); debugPrint("refreshToken######"); if (response?.statusCode == 200) { await session.saveSession(session.user!.copyWith( refreshToken: response?.data["data"]["refreshToken"], token: response?.data["data"]["token"])); return response; } else{ navigationService.currentContext?.read<LoginCubit>().logout(isUnauthorised: true); } } catch (e) { handler.reject; log(e.toString()); return null; } return null; } }
final response = await _dio?.patch( "${Constants.devAuthBaseUrl}refresh-token", data: {"refreshToken": refreshToken});
Above api call need to return the api response
The text was updated successfully, but these errors were encountered:
You need to use another Dio instance to refresh the token. https://github.com/cfug/dio/blob/main/example_dart/lib/queued_interceptor_crsftoken.dart
Dio
Sorry, something went wrong.
No branches or pull requests
Package
dio
Version
dio: ^5.7.0
Operating-System
Android, iOS
Adapter
Default Dio
Output of
flutter doctor -v
Dart Version
3.5.3
Steps to Reproduce
Expected Result
Above api call need to return the api response
Actual Result
Above api call need to return the api response
The text was updated successfully, but these errors were encountered: