> ## Documentation Index
> Fetch the complete documentation index at: https://www.cometchat.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Authentication

> CometChat Calling SDK v5 - Authentication for React Native

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`) |

```tsx theme={null}
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:

```tsx theme={null}
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           |

<Warning>
  Never expose your Auth Key in production apps. Use Auth Tokens generated from your backend server instead.
</Warning>

### Login with Auth Token

For production apps, generate an Auth Token on your backend server and use it to authenticate:

```tsx theme={null}
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:

```tsx theme={null}
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:

```tsx theme={null}
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:

```tsx theme={null}
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:

```tsx theme={null}
const authToken = CometChatCalls.getUserAuthToken();

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

## Login Listeners

Monitor login state changes by adding a login listener:

```tsx theme={null}
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           |

## Related Documentation

* [Setup](/calls/react-native/setup) - Install and configure the SDK
* [Session Settings](/calls/react-native/session-settings) - Configure call settings
* [Join Session](/calls/react-native/join-session) - Start your first call
