waitForAuthentication method

Future<bool> waitForAuthentication({
  1. required bool neverTimeout,
  2. required bool isPersevere(),
  3. RequestType? requestType,
  4. String? route,
})

Implementation

Future<bool> waitForAuthentication(
    {required bool neverTimeout, required bool Function() isPersevere, RequestType? requestType, String? route}) {
  if (_authStatus == AuthStatus.authenticated) {
    return Future.value(true);
  }
  final completer = Completer<bool>();
  Future.delayed(
      Duration(
          milliseconds: getIt
              .get<ConnectionService>()
              .connectionConfiguration
              .waitForAuthenticationTimeoutInMs), () {
    if (neverTimeout) {
      if (!completer.isCompleted) {
        logger(
            "Askless is still waiting for authentication to complete so it can perform \"(${requestType?.toString() ?? ''}) ${route ?? '(null route)'}\", please make sure to call AsklessClient.instance.authenticate(..) so the operation will continue",
            level: Level.warning);
      }
    } else {
      if (!completer.isCompleted) {
        logger(
            "Askless could not \"(${requestType?.toString() ?? ''}) ${route ?? 'null'}\" because it requires authentication, please, call AsklessClient.instance.authenticate(..) and try again",
            level: Level.warning);

        completer.complete(false);
        _waitingForAuthentication.removeWhere((item) => item.completer == completer);
      }
    }
  });
  _waitingForAuthentication.add(WaitingForAuthenticationItem(completer: completer, isPersevere: isPersevere));
  return completer.future;
}