Files
syncrow-app/lib/services/api/http_interceptor.dart
2024-03-13 13:52:22 +03:00

47 lines
1.7 KiB
Dart

import 'package:dio/dio.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:syncrow_app/features/auth/model/token.dart';
class HTTPInterceptor extends InterceptorsWrapper {
// @override
// void onResponse(Response response, ResponseInterceptorHandler handler) async {
// // Pass the response to the next interceptor or response handler.
// return handler.next(response);
// }
//
@override
void onRequest(
RequestOptions options, RequestInterceptorHandler handler) async {
var storage = FlutterSecureStorage();
var token = await storage.read(key: Token.loginAccessTokenKey);
options.headers['Authorization'] = 'Bearer $token';
super.onRequest(options, handler);
}
//
// @override
// void onError(DioException err, ErrorInterceptorHandler handler) async {
// // TODO: Implement error handling logic.
// // This method is called when an error occurs during a request.
// super.onError(err, handler);
// }
//
// /// Validates the response and returns true if it is successful (status code 2xx).
// Future<bool> validateResponse(Response response) async {
// if (response.statusCode != null) {
// if (response.statusCode! >= 200 && response.statusCode! < 300) {
// // If the response status code is within the successful range (2xx),
// // return true indicating a successful response.
// return true;
// } else {
// // If the response status code is not within the successful range (2xx),
// // return false indicating an unsuccessful response.
// return false;
// }
// } else {
// // If the response status code is null, return false indicating an unsuccessful response.
// return false;
// }
// }
}