configureAuthenticationBasicInjection function

Future<void> configureAuthenticationBasicInjection(
  1. AppEnvironment environment,
  2. BasicAuthenticationConfig config, {
  3. Logger? logger,
  4. Logger? httpLogger,
  5. AuthenticationService customService(
    1. IHttpClient client
    )?,
})

This functions register: SharedPreferences if not registered previously IHttpClient if not registered previously BasicAuthenticationConfig if not registered previously AuthenticationService with AuthenticationServiceImpl implementation, if passed test environment, it will provide a AuthenticationServiceMock

Implementation

Future<void> configureAuthenticationBasicInjection(
  AppEnvironment environment,
  BasicAuthenticationConfig config, {
  Logger? logger,
  Logger? httpLogger,
  AuthenticationService Function(IHttpClient client)? customService,
}) async {
  final getIt = GetIt.instance;

  if (!getIt.isRegistered<SharedPreferences>()) {
    if (AppEnvironment.test != environment) {
      getIt.registerSingletonAsync<SharedPreferences>(
          () => SharedPreferences.getInstance());
    }
  }

  /// check that if user already defined a custom [IHttpClient]
  /// let's print a warning that some of his configuration may not be taken into account
  if (getIt.isRegistered<IHttpClient>()) {
    if (config.maxAge != null ||
        config.refreshTokenMethod != null ||
        config.refreshTokenAPIendpoint != null ||
        config.customRefreshTokenRequestBodyMapper != null ||
        config.customRefreshTokenResponseParser != null ||
        config.onRefreshToken != null ||
        config.refreshTokenTimeout != null ||
        config.onRefreshTokenFailure != null) {
      getStackBaseDefaultLogger().w(
        """
          It looks like you have already defined a custom implementation of $IHttpClient at the same time are defining some configurations that should be passed to internal $HttpClientImpl, if your are not taking care of handling this in your client you may have unexpected behavior, most specific in refresh token logic
          In order to successfully override IHttpClient without affecting this plugin functionality
          Your custom implementation of [IHttpClient] must use an instance of AuthenticationServiceImpl.defaultClient
        """,
      );
    }
  }
  if (!getIt.isRegistered<IHttpClient>()) {
    if (AppEnvironment.test != environment) {
      getIt.registerSingletonAsync<IHttpClient>(
        () async => HttpClientImpl(
          sharedPreferences: getIt(),
          maxAge: config.maxAge,
          // refreshTokenDebounceTime: config.refreshTokenDebounceTime,
          client: AuthenticationServiceImpl.defaultClient(
            logger: httpLogger,
            sharedPreferences: getIt(),
            refreshTokenMethod: config.refreshTokenMethod,
            refreshTokenUrl: config.refreshTokenAPIendpoint != null
                ? (token, decodedToken) {
                    return config.refreshTokenAPIendpoint!
                        .call(token, decodedToken)
                        .toString();
                  }
                : null,
            // customRefreshTokenRequestBodyMapper: config.customRefreshTokenRequestBodyMapper,
            customRefreshTokenResponseParser:
                config.customRefreshTokenResponseParser,
            onRefreshToken: config.onRefreshToken,
            refreshTokenTimeout: config.refreshTokenTimeout,
            onRefreshTokenFailure: config.onRefreshTokenFailure,
            customRefreshTokenRequestBodyMapper:
                config.customRefreshTokenRequestBodyMapper,
            customRefreshTokenCallback: config.customRefreshTokenCallback,
            // customRefreshTokenCallback: (token, refreshToken, deviceInfo) async {
            //   final sres = await getIt<IHttpClient>().send(
            //     http.Request(
            //       refreshTokenMethod,
            //       Uri.parse((this.refreshTokenUrl!.call(refreshToken!, decoded))),
            //     )..bodyFields = await refreshTokenRequestBodyMapper.call(refreshToken, authToken),
            //   ).timeout(
            //     refreshTokenTimeout,
            //     onTimeout: () {
            //       throw TimeoutException(
            //         "Refresh token was not retrieved after: $refreshTokenTimeout",
            //         refreshTokenTimeout,
            //       );
            //     },
            //   );

            //   // response = await http.Response.fromStream(sres);
            //   // data = await refreshTokenResponseParser.call(response.body);
            // },
          ),
        ),
        dependsOn: [
          SharedPreferences,
        ],
      );
    }
  }

  if (!getIt.isRegistered<BasicAuthenticationConfig>()) {
    getIt.registerSingleton<BasicAuthenticationConfig>(config);
  }

  getIt.registerSingletonAsync<AuthenticationService>(
    () async {
      if (AppEnvironment.test == environment) {
        return customService?.call(
              getIt<IHttpClient>(),
            ) ??
            AuthenticationServiceMock();
      }
      return customService?.call(
            getIt<IHttpClient>(),
          ) ??
          AuthenticationServiceImpl(
            config: config,
            httpClient: getIt(),
            sharedPreferences: getIt(),
            logger: logger,
          );
    },
    dependsOn: [
      if (AppEnvironment.test != environment) IHttpClient,
      if (AppEnvironment.test != environment) SharedPreferences,
    ],
  );

  await getIt.isReady<AuthenticationService>();
}