Skip to main content
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.
const loggedInUser = CometChatCalls.getLoggedInUser();

if (loggedInUser) {
  // User is already logged in
  console.log("User already logged in:", loggedInUser.uid);
} else {
  // No user logged in, proceed with login
}
The getLoggedInUser() method returns a user 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.
const uid = "cometchat-uid-1"; // Replace with your user's UID
const apiKey = "API_KEY"; // Replace with your API Key

if (!CometChatCalls.getLoggedInUser()) {
  try {
    const user = await CometChatCalls.login(uid, apiKey);
    console.log("Login successful:", user.uid);
  } catch (error) {
    console.error("Login failed:", error.errorDescription);
  }
} else {
  // User already logged in
}
ParameterTypeDescription
uidStringThe unique identifier of the user to login
apiKeyStringYour CometChat API Key

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

  1. User authenticates with your backend
  2. Your backend calls the CometChat Create Auth Token API
  3. Your backend returns the Auth Token to the client
  4. Client uses the Auth Token to login
const authToken = "AUTH_TOKEN"; // Token received from your backend

try {
  const user = await CometChatCalls.loginWithAuthToken(authToken);
  console.log("Login successful:", user.uid);
} catch (error) {
  console.error("Login failed:", error.errorDescription);
}
ParameterTypeDescription
authTokenStringAuth Token generated via CometChat API

User Object

On successful login, the method returns a user object containing user information:
PropertyTypeDescription
uidStringUnique identifier of the user
nameStringDisplay name of the user
avatarStringURL of the user’s avatar image
statusStringUser’s online status
authTokenStringThe user’s auth token

Check User Login Status

You can verify if a user is currently logged in:
const isLoggedIn = CometChatCalls.isUserLoggedIn();

if (isLoggedIn) {
  // User is logged in
} else {
  // User is not logged in
}

Get User Auth Token

Retrieve the auth token of the currently logged-in user:
const authToken = CometChatCalls.getUserAuthToken();

if (authToken) {
  console.log("User auth token:", authToken);
}

Logout

Call logout() when the user signs out of your application. This clears the local session and disconnects from CometChat services.
try {
  const message = await CometChatCalls.logout();
  console.log("Logout successful:", message);
} catch (error) {
  console.error("Logout failed:", error.errorDescription);
}

Error Handling

Common authentication errors:
Error CodeDescription
ERROR_INVALID_UIDThe provided UID is empty or invalid
ERROR_UID_WITH_SPACEThe UID contains spaces (not allowed)
ERROR_API_KEY_NOT_FOUNDThe API Key is missing or invalid
ERROR_BLANK_AUTHTOKENThe Auth Token is empty
ERROR_AUTHTOKEN_WITH_SPACEThe Auth Token contains spaces (not allowed)
ERROR_LOGIN_IN_PROGRESSA login operation is already in progress
ERROR_SDK_NOT_INITIALIZEDSDK not initialized - call init() first
ERROR_NO_USER_LOGGED_INNo user is currently logged in (for logout)