loginWithAuthToken static method

void loginWithAuthToken({
  1. required String authToken,
  2. required dynamic onSuccess(
    1. User?
    ),
  3. required dynamic onError(
    1. CometChatCallsException
    ),
})

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

Implementation

static void loginWithAuthToken(
    {required String authToken,
    required Function(User?) onSuccess,
    required Function(CometChatCallsException excep) onError}) async {
  try {
    // Validation - business logic in Dart layer using centralized validator
    // Check login in progress first
    // if (_isUidLoginInProgress || _isAuthTokenLoginInProgress) {
    //   debugPrint('Test: Login already in progress $_isUidLoginInProgress');
    //   onError(CometChatCallsException.loginInProgress());
    //   return;
    // }

    // Validate authToken
    final authTokenError = InputValidator.validateAuthToken(authToken);
    if (authTokenError != null) {
      onError(authTokenError);
      return;
    }

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

    if (currentUser != null && currentUser.authToken != null) {
      if (authToken == currentUser.authToken) {
        onSuccess(getLoggedInUser);
      } else {
        logout(onSuccess: (success) {
          _logInWithAuthTokenInternal(authToken, onSuccess, onError);
        }, onError: (error) {
          onError(CometChatCallsException.logoutFailed());
        });
      }
    } else {
      _logInWithAuthTokenInternal(authToken, onSuccess, onError);
    }
  } catch (exception) {
    _isUidLoginInProgress = false;
    _isAuthTokenLoginInProgress = false;
    onError(CometChatCallsException.wrapUnhandled(exception));
  }
}