Skip to main content
Quick Reference for AI Agents & Developers
// Check if user is already logged in
val loggedInUser = CometChat.getLoggedInUser()

// Login with Auth Key (development only)
CometChat.login("UID", "AUTH_KEY", object : CometChat.CallbackListener<User?>() {
    override fun onSuccess(user: User?) { }
    override fun onError(e: CometChatException?) { }
})

// Login with Auth Token (production)
CometChat.login("AUTH_TOKEN", object : CometChat.CallbackListener<User?>() {
    override fun onSuccess(user: User?) { }
    override fun onError(e: CometChatException?) { }
})

// Logout
CometChat.logout(object : CometChat.CallbackListener<String>() {
    override fun onSuccess(p0: String?) { }
    override fun onError(p0: CometChatException?) { }
})
Required Credentials: App ID (from init), Auth Key (dev) or Auth Token (prod)
Get from: CometChat Dashboard → Your App → API & Auth Keys

Create User

Before you log in a user, you must add the user to CometChat.
  1. For proof of concept/MVPs: Create the user using the CometChat Dashboard.
  2. For production apps: Use the CometChat Create User API to create the user when your user signs up in your app.
Sample UsersWe have set up 5 users for testing with UIDs: cometchat-uid-1, cometchat-uid-2, cometchat-uid-3, cometchat-uid-4, and cometchat-uid-5.
Once initialization is successful, you need to log the user into CometChat using the login() method. We recommend calling the CometChat login() method once your user logs into your app. The login() method only needs to be called once.
CometChat.init() must be called before any other SDK method. Calling login(), sendMessage(), or registering listeners before init() will fail.
The CometChat SDK maintains the session of the logged-in user within the SDK. You do not need to call the login method for every session. 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. If this method returns null, it means there is no session present within the SDK and you need to log the user into CometChat.

Login using Auth Key

This straightforward authentication method is ideal for proof-of-concept (POC) development or during the early stages of application development. For production environments, however, we strongly recommend using an Auth Token instead of an Auth Key to ensure enhanced security.
Auth Key is for development/testing only. In production, generate Auth Tokens on your server using the REST API and pass them to the client. Never expose Auth Keys in production client code.
private String UID = "cometchat-uid-1";
private String authKey = "AUTH_KEY";

if (CometChat.getLoggedInUser() == null) {
    CometChat.login(UID, authKey, new CometChat.CallbackListener<User>() {
        @Override
        public void onSuccess(User user) {

        }

        @Override
        public void onError(CometChatException e) {

        }
    });
} else {
    // user already logged-in perform your action
}
ParameterDescription
UIDThe UID of the user that you would like to login
authKeyCometChat App Auth Key
After the user logs in, their information is returned in the User object.

Login using Auth Token

This advanced authentication procedure does not use the Auth Key directly in your client code, ensuring better security.
  1. Create a User via the CometChat API when the user signs up in your app.
  2. Create an Auth Token via the CometChat API for the new user every time the user logs in to your app.
  3. Pass the Auth Token to your client and use it in the login() method.
private String authToken = "AUTH_TOKEN";

CometChat.login(authToken, new CometChat.CallbackListener<User>()
  @Override
  public void onSuccess(User user) {
    Log.d(TAG, "Login Successful : " + user.toString());
  }

  @Override
  public void onError(CometChatException e) {
    Log.d(TAG, "Login failed with exception: " + e.getMessage());
  }
});
ParameterDescription
authTokenAuth Token of the user you would like to login
After the user logs in, their information is returned in the User object.

Logout

You can use the logout() method to log out the user from CometChat. We suggest you call this method once your user has been successfully logged out from your app.
CometChat.logout(new CometChat.CallbackListener<String>() {
  @Override
  public void onSuccess(String successMessage) {
    Log.d(TAG, "Logout completed successfully");
  }

  @Override
  public void onError(CometChatException e) {
    Log.d(TAG, "Logout failed with exception: " + e.getMessage());
  }
});

Next Steps