loginWithAuthToken static method

Future<User?> loginWithAuthToken(
  1. String authToken, {
  2. dynamic onSuccess(
    1. User user
    )?,
  3. dynamic onError(
    1. CometChatException excep
    )?,
})

Returns a User object after login in CometChat API.

The CometChat SDK maintains the session of the logged in user within the SDK. Thus you do not need to call the login method for every session. You can use the CometChat.getLoggedInUser() method to check if there is any existing session in the SDK. This method should return the details of the logged-in user.

Create an Auth Token via the CometChat API for the new user every time the user logs in to your app

method could throw PlatformException with error codes specifying the cause

Implementation

static Future<User?> loginWithAuthToken(String authToken,
    {Function(User user)? onSuccess,
    Function(CometChatException excep)? onError}) async {
  if (!checkAuthSettings(onError)) return null;
  User? loggedInUser = await getLoggedInUser();

  if (loggedInUser == null) {
    User? user =
        await CometChat.loginWithAuthToken(authToken, onSuccess: (User user) {
      //executing custom onSuccess handler when user is logged in successfully using auth token
      getConversationUpdateSettings();
      CometChatUIKit.loggedInUser = user;
      if (onSuccess != null) {
        try {
          onSuccess(user);
        } catch (e) {
          if (kDebugMode) {
            debugPrint(
                "user login is successful but failed to execute onSuccess callback");
          }
        }
      }
      _initiateAfterLogin();
    }, onError: onError);
    return user;
  } else {
    CometChatUIKit.loggedInUser = loggedInUser;
    getConversationUpdateSettings();
    _initiateAfterLogin();
    return loggedInUser;
  }
}