Before users can make or receive calls, they must be authenticated with the CometChat Calls SDK. This guide covers the login and logout methods.
Sample UsersCometChat provides 5 test users: cometchat-uid-1, cometchat-uid-2, cometchat-uid-3, cometchat-uid-4, and cometchat-uid-5.
Check Login Status
Before calling login(), check if a user is already logged in using getLoggedInUser(). The SDK maintains the session internally, so you only need to login once per user session.
val loggedInUser = CometChatCalls.getLoggedInUser()
if (loggedInUser != null) {
// User is already logged in
Log.d(TAG, "User already logged in: ${loggedInUser.uid}")
} else {
// No user logged in, proceed with login
}
CallUser loggedInUser = CometChatCalls.getLoggedInUser();
if (loggedInUser != null) {
// User is already logged in
Log.d(TAG, "User already logged in: " + loggedInUser.getUid());
} else {
// No user logged in, proceed with login
}
The getLoggedInUser() method returns a CallUser object if a user is logged in, or null if no session exists.
Login with UID and API Key
This method is suitable for development and testing. For production apps, use Auth Token login instead.
Security NoticeUsing the API Key directly in client code is not recommended for production. Use Auth Token authentication for enhanced security.
val uid = "cometchat-uid-1" // Replace with your user's UID
val apiKey = "API_KEY" // Replace with your API Key
if (CometChatCalls.getLoggedInUser() == null) {
CometChatCalls.login(uid, apiKey, object : CometChatCalls.CallbackListener<CallUser>() {
override fun onSuccess(user: CallUser) {
Log.d(TAG, "Login successful: ${user.uid}")
}
override fun onError(e: CometChatException) {
Log.e(TAG, "Login failed: ${e.message}")
}
})
} else {
// User already logged in
}
String uid = "cometchat-uid-1"; // Replace with your user's UID
String apiKey = "API_KEY"; // Replace with your API Key
if (CometChatCalls.getLoggedInUser() == null) {
CometChatCalls.login(uid, apiKey, new CometChatCalls.CallbackListener<CallUser>() {
@Override
public void onSuccess(CallUser user) {
Log.d(TAG, "Login successful: " + user.getUid());
}
@Override
public void onError(CometChatException e) {
Log.e(TAG, "Login failed: " + e.getMessage());
}
});
} else {
// User already logged in
}
| Parameter | Type | Description |
|---|
uid | String | The unique identifier of the user to login |
apiKey | String | Your CometChat API Key |
listener | CallbackListener | Callback for success/error handling |
Login with Auth Token
This is the recommended authentication method for production applications. The Auth Token is generated server-side, keeping your API Key secure.
Auth Token Flow
- User authenticates with your backend
- Your backend calls the CometChat Create Auth Token API
- Your backend returns the Auth Token to the client
- Client uses the Auth Token to login
val authToken = "AUTH_TOKEN" // Token received from your backend
CometChatCalls.login(authToken, object : CometChatCalls.CallbackListener<CallUser>() {
override fun onSuccess(user: CallUser) {
Log.d(TAG, "Login successful: ${user.uid}")
}
override fun onError(e: CometChatException) {
Log.e(TAG, "Login failed: ${e.message}")
}
})
String authToken = "AUTH_TOKEN"; // Token received from your backend
CometChatCalls.login(authToken, new CometChatCalls.CallbackListener<CallUser>() {
@Override
public void onSuccess(CallUser user) {
Log.d(TAG, "Login successful: " + user.getUid());
}
@Override
public void onError(CometChatException e) {
Log.e(TAG, "Login failed: " + e.getMessage());
}
});
| Parameter | Type | Description |
|---|
authToken | String | Auth Token generated via CometChat API |
listener | CallbackListener | Callback for success/error handling |
CallUser Object
On successful login, the callback returns a CallUser object containing user information:
| Property | Type | Description |
|---|
uid | String | Unique identifier of the user |
name | String | Display name of the user |
avatar | String | URL of the user’s avatar image |
status | String | User’s online status |
Logout
Call logout() when the user signs out of your application. This clears the local session and disconnects from CometChat services.
CometChatCalls.logout(object : CometChatCalls.CallbackListener<String>() {
override fun onSuccess(message: String) {
Log.d(TAG, "Logout successful")
}
override fun onError(e: CometChatException) {
Log.e(TAG, "Logout failed: ${e.message}")
}
})
CometChatCalls.logout(new CometChatCalls.CallbackListener<String>() {
@Override
public void onSuccess(String message) {
Log.d(TAG, "Logout successful");
}
@Override
public void onError(CometChatException e) {
Log.e(TAG, "Logout failed: " + e.getMessage());
}
});
Error Handling
Common authentication errors:
| Error Code | Description |
|---|
ERROR_INVALID_UID | The provided UID is empty or invalid |
ERROR_UID_WITH_SPACE | The UID contains spaces (not allowed) |
ERROR_API_KEY_NOT_FOUND | The API Key is missing or invalid |
ERROR_BLANK_AUTHTOKEN | The Auth Token is empty |
ERROR_LOGIN_IN_PROGRESS | A login operation is already in progress |