login static method

void login({
  1. required String uid,
  2. required String authKey,
  3. required dynamic onSuccess(
    1. User?
    ),
  4. required dynamic onError(
    1. CometChatCallsException
    ),
})

Logs in to the CometChat Calls SDK using the provided user ID and auth key. All validation and state management is done here in the Dart layer using InputValidator. Requirements: 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8

Implementation

static void login(
    {required String uid,
    required String authKey,
    required Function(User?) onSuccess,
    required Function(CometChatCallsException excep) onError}) async {
  try {
    // Check if SDK is initialized
    if (!isInitialized) {
      onError(CometChatCallsException.sdkNotInitialized());
      return;
    }

    if (_isUidLoginInProgress || _isAuthTokenLoginInProgress) {
      debugPrint('Test: Login already in progress $_isUidLoginInProgress');
      onError(CometChatCallsException.loginInProgress());
      return;
    }

    // Validate uid
    final uidError = InputValidator.validateUid(uid);
    if (uidError != null) {
      onError(uidError);
      return;
    }

    // Validate authKey
    final authKeyError = InputValidator.validateAuthKey(authKey);
    if (authKeyError != null) {
      onError(authKeyError);
      return;
    }

    // State management - business logic in Dart layer
    final loggedInUser = await getLoggedInUser();
    final currentUser = await CurrentUserRepository.getCurrentUser();

    if (loggedInUser != null) {
      if (loggedInUser.uid == uid &&
          currentUser != null &&
          currentUser.authToken != null) {
        onSuccess(loggedInUser);
      } else {
        // Different user login - logout first
        logout(onSuccess: (String success) {
          _internalLogOut();
          _loginWithApiKeyInternal(uid, authKey, onSuccess, onError);
        }, onError: (CometChatCallsException e) {
          onError(CometChatCallsException.logoutFailed());
        });
      }
    } else {
      _loginWithApiKeyInternal(uid, authKey, onSuccess, onError);
    }
  } catch (exception) {
    _isUidLoginInProgress = false;
    onError(CometChatCallsException.wrapUnhandled(exception));
  }
}