You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi , i have a qustion about how to handle the acess and refresh yokens in my flutter app ,
the sserver when i logged in, it give me the two tokens , but they refresh automaticcaly from the backend logic and , if i discuss with the backend developer about to give me an endpoint to the refresh token to impelemnt a function to refresh my tokens in the background , he say to me that it doesnt need an endpoint , it just need to listen to the changes from the server and update it ech time expires,
i'm using dio in my network services ,
is there any solution in flutter to intercept the refreshed token from server ?
my logic :
import 'dart:io';
import 'package:bookinipay/data/datasources/user_data_source/user_local_data_source.dart';
import 'package:bookinipay/infrastructure/exceptions/exception_handler_mixin.dart';
import 'package:bookinipay/infrastructure/exceptions/http_exception.dart';
import 'package:bookinipay/infrastructure/network_service.dart';
import 'package:bookinipay/infrastructure/response.dart' as response;
import 'package:dio/dio.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:get_it/get_it.dart';
import 'package:talker_dio_logger/talker_dio_logger.dart';
import 'either.dart';
class DioNetworkService extends NetworkService with ExceptionHandlerMixin {
final Dio dio;
// ignore: non_constant_identifier_names
Duration DEFAULT_TIMEOUT = const Duration(seconds: 30);
final kTestMode = Platform.environment.containsKey('FLUTTER_TEST');
DioNetworkService(this.dio) {
// this throws error while running test
//if (!kTestMode) {
dio.options = dioBaseOptions;
// if (kDebugMode) {
dio.interceptors.add(LogInterceptor(requestBody: true, responseBody: true));
dio.interceptors.add(TalkerDioLogger(
settings: const TalkerDioLoggerSettings(
printRequestHeaders: true,
printResponseHeaders: true,
printResponseMessage: true,
),
));
// }
// }
}
BaseOptions get dioBaseOptions => BaseOptions(
baseUrl: baseUrl,
headers: headers,
receiveTimeout: DEFAULT_TIMEOUT,
);
@override
String get baseUrl => dotenv.env['SERVER_LINK']!;
@override
Map<String, Object> get headers => {
'accept': 'application/json',
'content-type': 'application/json',
'access': "':
"${GetIt.instance.get<AuthLocalDataSource>().refreshToken}"
};
@override
Map<String, dynamic>? updateHeader(Map<String, dynamic> data) {
final header = {...data, ...headers};
// if (!kTestMode) {
dio.options.headers = header;
// }
return header;
}
@override
Future<Either<AppException, response.Response>> post(String endpoint,
{Map<String, dynamic>? data, Map<String, dynamic>? queryParameters}) {
final res = handleException(
() => dio.post(
endpoint,
data: data,
queryParameters: queryParameters, // Add this line
options: Options(
headers: {
'access':
"i store it from the login server and then use it for my requests in headers",
'refresh':
"i store it from the login server and then use it for my requests in headers"
},
),
),
endpoint: endpoint,
);
return res;
}
@override
Future<Either<AppException, response.Response>> get(String endpoint,
{Map<String, dynamic>? queryParameters}) {
final res = handleException(
() => dio.get(
endpoint,
queryParameters: queryParameters,
options: Options(
headers: {
'access':
"i store it from the login server and then use it for my requests in headers",
'refresh':
""
},
),
),
endpoint: endpoint,
);
return res;
}
@override
Future<Either<AppException, response.Response>> patch(String endpoint,
{Map<String, dynamic>? queryParameters}) {
final res = handleException(
() => dio.patch(
endpoint,
queryParameters: queryParameters,
options: Options(
headers: {
'access':
"i store it from the login server and then use it for my requests in headers",
'refresh':
"i store it from the login server and then use it for my requests in headers"
},
),
),
endpoint: endpoint,
);
return res;
}
@override
Future<Either<AppException, response.Response>> delete(String endpoint,
{Map<String, dynamic>? queryParameters}) {
final res = handleException(
() => dio.delete(
endpoint,
queryParameters: queryParameters,
options: Options(
headers: {
'access':
"i store it from the login server and then use it for my requests in headers",
'refresh':
"i store it from the login server and then use it for my requests in headers"
},
),
),
endpoint: endpoint,
);
return res;
}
@override
Future<Either<AppException, response.Response>> put(String endpoint,
{Map<String, dynamic>? queryParameters}) {
final res = handleException(
() => dio.put(
endpoint,
queryParameters: queryParameters,
options: Options(
headers: {
'access':
"i store it from the login server and then use it for my requests in headers",
'refresh':
"i store it from the login server and then use it for my requests in headers"
},
),
),
endpoint: endpoint,
);
return res;
}
}
The text was updated successfully, but these errors were encountered:
Hi , i have a qustion about how to handle the acess and refresh yokens in my flutter app ,
the sserver when i logged in, it give me the two tokens , but they refresh automaticcaly from the backend logic and , if i discuss with the backend developer about to give me an endpoint to the refresh token to impelemnt a function to refresh my tokens in the background , he say to me that it doesnt need an endpoint , it just need to listen to the changes from the server and update it ech time expires,
i'm using dio in my network services ,
is there any solution in flutter to intercept the refreshed token from server ?
my logic :
The text was updated successfully, but these errors were encountered: