> ## Documentation Index
> Fetch the complete documentation index at: https://cometchat-22654f5b-docs-ios-ui-kit-sdk-fixes.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Login Listeners

> Guide to monitoring login and logout events using the CometChat iOS SDK CometChatLoginDelegate.

<Info>
  **Quick Reference for AI Agents & Developers**

  * **Set delegate:** `CometChat.logindelegate = self` (conform to `CometChatLoginDelegate`)
  * **Delegate methods:** `onLoginSuccess(user:)`, `onLoginFailed(error:)`, `onLogoutSuccess()`, `onLogoutFailed(error:)`
  * **Related:** [Authentication](/sdk/ios/authentication-overview) · [Connection Status](/sdk/ios/connection-status) · [Setup](/sdk/ios/setup)
</Info>

CometChat SDK provides you with a mechanism to get real-time status whenever a user logs into CometChat or logs out from CometChat.

Login Listener provides you with the below 4 methods:

| Delegate Method                            | Information                                                                                                                                 |
| ------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------- |
| onLoginSuccess(user: User)                 | This method is triggered when the user successfully logs into the CometChat SDK. It returns an Object of the User loggedIn.                 |
| onLoginFailed(error: CometChatException?)  | This method is triggered when the user could not successfully log into the CometChat SDK. It returns an Object of the CometChatException.   |
| onLogoutSuccess()                          | This method is called when the user successfully logs out from the CometChat SDK. It does not return anything.                              |
| onLogoutFailed(error: CometChatException?) | This method is triggered when the user could not successfully log out of the CometChat SDK. It returns an Object of the CometChatException. |

In order to use the Delegate methods you must add protocol conformance `CometChatLoginDelegate` as `CometChat.logindelegate = self` . Here is the example of CometChatLoginDelegate:

<Tabs>
  <Tab title="Swift">
    ```swift theme={null}
    extension AppDelegate: CometChatLoginDelegate {

    	func onLoginSuccess(user: User) {
           print("Login Success")
        }
        
        func onLoginFailed(error: CometChatException?) {
            print("Login Failed")
        }
        
        func onLogoutSuccess() {
            print("Logout Success")
        }
        
        func onLogoutFailed(error: CometChatException?) {
            print("Logout Failed")
        }
    }
    ```
  </Tab>

  <Tab title="Objective C">
    ```objc theme={null}
    @interface ViewController ()<CometChatConnectionDelegate>

    @end

    @implementation ViewController

    - (void)viewDidLoad {
        [super viewDidLoad];
        
        [CometChat logindelegate:self];
    }

    - (void)onLoginSuccess(user : User) {
        
        NSLog(@"onLoginSuccess");
    }

    - (void)onLoginFailed(error : CometChatException) {
        
        NSLog(@"onLoginFailed");
    }

    - (void)onLogoutSuccess() {
        
        NSLog(@"onLogoutSuccess");
    }

    - (void)onLogoutFailed(error : CometChatException) {
        
        NSLog(@"onLogoutFailed");
    }
    ```
  </Tab>
</Tabs>

***

## Delegate Methods

### onLoginSuccess(user: User)

This method is triggered when the user successfully logs into the CometChat SDK. It returns a `User` object containing all information about the logged-in user.

<Accordion title="Sample Payloads">
  <Tabs>
    <Tab title="Request">
      ```json theme={null}
      {
        "uid": "cometchat-uid-1",
        "authKey": "AUTH_KEY"
      }
      ```
    </Tab>

    <Tab title="Success Response">
      ```json theme={null}
      {
        "uid": "user_12345",
        "name": "John Doe",
        "avatar": "https://data-us.cometchat.io/avatars/user_12345.png",
        "link": "https://example.com/profile/john",
        "role": "default",
        "metadata": {
          "department": "Engineering",
          "level": 5
        },
        "status": "online",
        "statusMessage": "Available",
        "lastActiveAt": 1708934400.0,
        "hasBlockedMe": false,
        "blockedByMe": false,
        "tags": ["premium", "verified"],
        "deactivatedAt": null,
        "authToken": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
        "createdAt": 1700000000.0,
        "updatedAt": 1708934400.0
      }
      ```
    </Tab>

    <Tab title="Error Response">
      ```json theme={null}
      {
        "errorCode": "ERR_UID_NOT_FOUND",
        "errorDescription": "The UID 'invalid_user' does not exist.",
        "details": "Verify the UID is registered in your CometChat app."
      }
      ```
    </Tab>
  </Tabs>
</Accordion>

***

### onLoginFailed(error: CometChatException?)

This method is triggered when the user could not successfully log into the CometChat SDK. It returns a `CometChatException` object with error details.

<Accordion title="Sample Payloads">
  <Tabs>
    <Tab title="Request">
      ```json theme={null}
      {
        "uid": "invalid_user",
        "authKey": "AUTH_KEY"
      }
      ```
    </Tab>

    <Tab title="Success Response">
      ```json theme={null}
      // N/A - This callback only fires on failure
      ```
    </Tab>

    <Tab title="Error Response">
      ```json theme={null}
      {
        "errorCode": "ERR_UID_NOT_FOUND",
        "errorDescription": "The UID 'invalid_user' does not exist.",
        "details": "Verify the UID is registered in your CometChat app."
      }
      ```
    </Tab>
  </Tabs>
</Accordion>

***

### onLogoutSuccess()

This method is called when the user successfully logs out from the CometChat SDK. It does not return any data.

<Accordion title="Sample Payloads">
  <Tabs>
    <Tab title="Request">
      ```json theme={null}
      // No request parameters - logout uses current session
      {}
      ```
    </Tab>

    <Tab title="Success Response">
      ```json theme={null}
      // No data returned - method has no return value
      // CometChat.getLoggedInUser() will return nil after successful logout
      {}
      ```
    </Tab>

    <Tab title="Error Response">
      ```json theme={null}
      // N/A - This callback only fires on success
      ```
    </Tab>
  </Tabs>
</Accordion>

***

### onLogoutFailed(error: CometChatException?)

This method is triggered when the user could not successfully log out of the CometChat SDK. It returns a `CometChatException` object with error details.

<Accordion title="Sample Payloads">
  <Tabs>
    <Tab title="Request">
      ```json theme={null}
      // No request parameters - logout uses current session
      {}
      ```
    </Tab>

    <Tab title="Success Response">
      ```json theme={null}
      // N/A - This callback only fires on failure
      ```
    </Tab>

    <Tab title="Error Response">
      ```json theme={null}
      {
        "errorCode": "ERR_NOT_LOGGED_IN",
        "errorDescription": "Cannot logout - no user is currently logged in.",
        "details": null
      }
      ```
    </Tab>
  </Tabs>
</Accordion>
