login static method

  1. @Deprecated('Use loginWithAuthToken for production. This method is for testing only.')
Future<User?> login(
  1. String uid,
  2. String authKey, {
  3. required dynamic onSuccess(
    1. User user
    )?,
  4. required dynamic onError(
    1. CometChatException excep
    )?,
})

Use this function only for testing purpose. For production, use loginWithAuthToken

Migration Note: Migrated from platform channels to native Dart implementation. Behavior and signature remain identical for backward compatibility.

Android Reference: CometChat.login(String uid, String apiKey, CallbackListener)

Implementation

@Deprecated(
    'Use loginWithAuthToken for production. This method is for testing only.')
static Future<User?> login(String uid, String authKey,
    {required Function(User user)? onSuccess,
    required Function(CometChatException excep)? onError}) async {
  try {
    print('[CometChat.login] Called with uid=$uid');
    // Get SDK instance
    final sdk = SdkRegistry.getInstance();

    print('[CometChat.login] Calling sdk.auth.loginWithApiKey');
    // Call native Dart auth repository
    // The repository returns the full User object with all fields
    final user = await sdk.auth.loginWithApiKey(uid, authKey);

    print('[CometChat.login] Login successful, user=${user.uid}');

    // Re-setup stream subscriptions (cancelled during logout)
    _setupNativeStreamSubscriptions();

    // Notify login listeners
    _loginListeners.values.forEach((element) {
      element.loginSuccess(user);
    });

    // Call success callback
    if (onSuccess != null) onSuccess(user);
    return user;
  } on SdkException catch (sdkEx) {
    print('[CometChat.login] SdkException: ${sdkEx.code} - ${sdkEx.message}');
    // Convert SdkException to CometChatException
    final cometChatEx = CometChatException(
      sdkEx.code,
      sdkEx.details ?? sdkEx.message,
      sdkEx.message,
    );

    // Notify login listeners
    _loginListeners.values.forEach((element) {
      element.loginFailure(cometChatEx);
    });

    _errorCallbackHandler(cometChatEx, null, null, onError);
  } catch (e) {
    // Handle unexpected errors
    final cometChatEx = CometChatException(
      ErrorCode.errorUnhandledException,
      e.toString(),
      e.toString(),
    );

    // Notify login listeners
    _loginListeners.values.forEach((element) {
      element.loginFailure(cometChatEx);
    });

    _errorCallbackHandler(null, null, e, onError);
  }
  return null;
}