Skip to main content
This guide covers initializing the CometChat Calls SDK and authenticating users in your React Native application.

Initialize CometChat Calls

The init() method initializes the SDK with your app credentials. Call this method once when your application starts, typically in your app’s entry point or a dedicated initialization module.

CallAppSettingsBuilder

The CallAppSettingsBuilder class configures the SDK initialization:
ParameterTypeRequiredDescription
appIdstringYesYour CometChat App ID
regionstringYesYour app region (us, eu, or in)
import { CometChatCalls } from '@cometchat/calls-sdk-react-native';

const appId = 'APP_ID'; // Replace with your App ID
const region = 'REGION'; // Replace with your Region ("us", "eu", or "in")

const callAppSettings = new CometChatCalls.CallAppSettingsBuilder()
  .setAppId(appId)
  .setRegion(region)
  .build();

const initResult = await CometChatCalls.init(callAppSettings);

if (initResult.success) {
  console.log('CometChat Calls SDK initialized successfully');
} else {
  console.error('CometChat Calls SDK initialization failed:', initResult.error);
}

Login

After initialization, authenticate the user using one of the login methods.

Login with UID and Auth Key

Use this method during development or when you manage authentication on the client side:
const uid = 'USER_UID';
const authKey = 'AUTH_KEY';

try {
  const user = await CometChatCalls.login(uid, authKey);
  console.log('Login successful:', user);
} catch (error) {
  console.error('Login failed:', error);
}
ParameterTypeRequiredDescription
uidstringYesThe unique identifier of the user
authKeystringYesYour CometChat Auth Key
Never expose your Auth Key in production apps. Use Auth Tokens generated from your backend server instead.

Login with Auth Token

For production apps, generate an Auth Token on your backend server and use it to authenticate:
const authToken = 'USER_AUTH_TOKEN'; // Token from your backend

try {
  const user = await CometChatCalls.loginWithAuthToken(authToken);
  console.log('Login successful:', user);
} catch (error) {
  console.error('Login failed:', error);
}
ParameterTypeRequiredDescription
authTokenstringYesAuth token generated from your backend

Logout

Log out the current user when they sign out of your app:
try {
  const message = await CometChatCalls.logout();
  console.log('Logout successful:', message);
} catch (error) {
  console.error('Logout failed:', error);
}

Check Login Status

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

if (isLoggedIn) {
  console.log('User is logged in');
} else {
  console.log('No user logged in');
}

Get Logged In User

Retrieve the currently logged-in user’s details:
const user = CometChatCalls.getLoggedInUser();

if (user) {
  console.log('Current user:', user.uid, user.name);
} else {
  console.log('No user 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);
}

Login Listeners

Monitor login state changes by adding a login listener:
const listenerId = 'unique_listener_id';

CometChatCalls.addLoginListener(listenerId, {
  onLoginSuccess: (user) => {
    console.log('User logged in:', user);
  },
  onLoginFailure: (error) => {
    console.error('Login failed:', error);
  },
  onLogoutSuccess: () => {
    console.log('User logged out');
  },
  onLogoutFailure: (error) => {
    console.error('Logout failed:', error);
  },
});

// Remove listener when no longer needed
CometChatCalls.removeLoginListener(listenerId);

Error Handling

The SDK throws structured errors with the following properties:
PropertyTypeDescription
errorCodestringA unique error code
errorDescriptionstringHuman-readable error description
Common error codes:
Error CodeDescription
ERROR_SDK_NOT_INITIALIZEDSDK not initialized. Call init() first
ERROR_LOGIN_IN_PROGRESSA login operation is already in progress
ERROR_INVALID_UIDUID is empty or invalid
ERROR_UID_WITH_SPACEUID contains spaces
ERROR_API_KEY_NOT_FOUNDAuth Key is empty
ERROR_BLANK_AUTHTOKENAuth token is empty
ERROR_AUTHTOKEN_WITH_SPACEAuth token contains spaces
ERROR_NO_USER_LOGGED_INNo user is currently logged in