Skip to content
New issue

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

flutter dynamic coockies management #2242

Closed
fazaamajdeddine opened this issue Jun 10, 2024 · 1 comment
Closed

flutter dynamic coockies management #2242

fazaamajdeddine opened this issue Jun 10, 2024 · 1 comment

Comments

@fazaamajdeddine
Copy link

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

Copy link
Contributor

@fazaamajdeddine Infra and blank issues are only available for moderators. You're apparently using the wrong issue template.

Infra 和空白 issue 仅供管理人员使用,请选择其他 issue 模板创建 issue。

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Jun 10, 2024
@github-actions github-actions bot locked as off-topic and limited conversation to collaborators Jun 10, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant