> ## Documentation Index
> Fetch the complete documentation index at: https://cometchat-22654f5b-docs-rn-guide-message-privately.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Authentication

> Authentication — CometChat documentation.

## Create User

Before you log in to the user, you must add the user to CometChat.

1. **For proof of concept/MVPs**: Create the user using the [CometChat Dashboard](https://app.cometchat.com).
2. **For production apps**: Use the CometChat [Create User API](/rest-api/chat-apis) to create the user when your user signs up in your app.

<Note>
  Sample Users

  We have setup 5 users for testing having UIDs: `cometchat-uid-1`, `cometchat-uid-2`, `cometchat-uid-3`, `cometchat-uid-4` and `cometchat-uid-5`.
</Note>

Once initialisation is successful, you will need to log the user into CometChat using the `login()` method.

We recommend you call the CometChat login method once your user logs into your app. The login method needs to be called only once but the getLoggedInUser() needs to be checked every-time when the app starts and if it returns null then you need to call the login method.

***

## Login using Auth Key

This simple authentication procedure is useful when you are in development or if you do not require additional security.

The login method needs to be called in the following scenarios:

1. When the user is logging to the App for the first time.
2. If the CometChat.getLoggedInUser() function returns nil.

<Warning>
  If you are using v2.0.6 of SDK or greater please make sure you add the check of CometChat.getLoggedInUser() function in your app where you check App's user login status. In case it returns nil then you need to call the Login method inside it.
</Warning>

<Tabs>
  <Tab title="Swift">
    ```swift theme={null}
    let uid = "cometchat-uid-1"
    let authKey = "AUTH_KEY"
            
    if CometChat.getLoggedInUser() == nil {

    	CometChat.login(UID: uid, authkey: authKey, onSuccess: { (user) in

      	print("Login successful : " + user.stringValue())

    	}) { (error) in

      	print("Login failed with error: " + error.errorDescription);

    	}

    }
    ```
  </Tab>

  <Tab title="Objective C">
    ```objc theme={null}
    NSString *uid = @"cometchat-uid-1";
    NSString *authkey = @"YOUR_AUTH_KEY";

    [CometChat loginWithUID:uid authkey:authey onSuccess:^(User * user) {
        
        NSLog(@"Login successful : %@",[user stringValue]);
        
    } onError:^(CometChatException * error) {
        
        NSLog(@"Login failed with error:%@",[error errorDescription]);
        
    }];
    ```
  </Tab>
</Tabs>

| Parameter | Description                                      |
| --------- | ------------------------------------------------ |
| UID       | The UID of the user that you would like to login |
| authKey   | CometChat Auth Key                               |

The `login()` method returns the `User` object containing all the information of the logged-in user.

***

## Login using Auth Token

This advanced authentication procedure does not use the auth Key directly in your client code and thus ensuring that your auth Key is not leaked even if the client code is reverse engineered.

1. [Create a user](/rest-api/chat-apis) via the CometChat API when the user signs up in your app.
2. [Create an Auth Token](/rest-api/chat-apis) via the CometChat API for the new user and save the token in your database.
3. Load the Auth Token in your client and pass it to the `login()` method.

The login method needs to be called in the following scenarios:

1. When the user is logging to the App for the first time.
2. If the CometChat.getLoggedInUser() function returns nil.

<Warning>
  If you are using v2.0.6 of SDK or greater please make sure you add the check of CometChat.getLoggedInUser() function in your app where you check App's user login status. In case it returns nil then you need to call the Login method inside it.
</Warning>

<Tabs>
  <Tab title="Swift">
    ```swift theme={null}
    let authToken = "YOUR_AUTH_TOKEN";

    if CometChat.getLoggedInUser() == nil {
      
      CometChat.login(authToken: authToken , onSuccess: { (user) in

        print("Login successful : " + user.stringValue())

      }) { (error) in

        print("Login failed with error: " + error.errorDescription);
      }
      
    }
    ```
  </Tab>

  <Tab title="Objective C">
    ```objc theme={null}
    NSString *authToken = @"YOUR_AUTH_TOKEN";

    [CometChat loginWithAuthToken:authToken onSuccess:^(User * user) {
        
        // Login Successful
        NSLog(@"Login Successful : %@",[user stringValue]);
        
    } onError:^(CometChatException * error) {
        
        // Login error
        NSLog(@"Login failed with exception: %@",[error errorDescription]);
        
    }];
    ```
  </Tab>
</Tabs>

| Parameter | Description                                    |
| --------- | ---------------------------------------------- |
| authToken | Auth Token of the user you would like to login |

The `login()` method returns the `User` object containing all the information of the logged-in user.

***

## Logout

You can use the `logout()` method to log out the user from CometChat.

<Tabs>
  <Tab title="Swift">
    ```swift theme={null}
    CometChat.logout(onSuccess: { (response) in

      print("Logout successfully.")

    }) { (error) in

      print("logout failed with error: " + error.errorDescription);
    }
    ```
  </Tab>

  <Tab title="Objective C">
    ```objc theme={null}
    [CometChat logoutOnSuccess:^(NSString * response) {
        
        // Logout Success
        NSLog(@"%@", response);
        
    } onError:^(CometChatException * error) {
        
        // Logout error
        NSLog(@"%@",[error errorDescription]);
    }];
    ```
  </Tab>
</Tabs>
