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:
| Parameter | Type | Required | Description |
|---|
appId | string | Yes | Your CometChat App ID |
region | string | Yes | Your 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);
}
| Parameter | Type | Required | Description |
|---|
uid | string | Yes | The unique identifier of the user |
authKey | string | Yes | Your 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);
}
| Parameter | Type | Required | Description |
|---|
authToken | string | Yes | Auth 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:
| Property | Type | Description |
|---|
errorCode | string | A unique error code |
errorDescription | string | Human-readable error description |
Common error codes:
| Error Code | Description |
|---|
ERROR_SDK_NOT_INITIALIZED | SDK not initialized. Call init() first |
ERROR_LOGIN_IN_PROGRESS | A login operation is already in progress |
ERROR_INVALID_UID | UID is empty or invalid |
ERROR_UID_WITH_SPACE | UID contains spaces |
ERROR_API_KEY_NOT_FOUND | Auth Key is empty |
ERROR_BLANK_AUTHTOKEN | Auth token is empty |
ERROR_AUTHTOKEN_WITH_SPACE | Auth token contains spaces |
ERROR_NO_USER_LOGGED_IN | No user is currently logged in |