Quick Reference for AI Agents & Developers
- Login with Auth Key:
CometChat.login(UID:authKey:onSuccess:onError:)— for development/testing - Login with Auth Token:
CometChat.login(authToken:onSuccess:onError:)— for production (generate token server-side) - Logout:
CometChat.logout(onSuccess:onError:) - Get logged-in user:
CometChat.getLoggedInUser() - Related: Setup · User Management · Key Concepts
Create User
Before you log in a user, you must add the user to CometChat.- For proof of concept/MVPs: Create the user using the CometChat Dashboard.
- For production apps: Use the CometChat Create User API to create the user when your user signs up in your app.
Sample UsersWe have setup 5 users for testing having UIDs:
cometchat-uid-1, cometchat-uid-2, cometchat-uid-3, cometchat-uid-4 and cometchat-uid-5.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 straightforward authentication method is ideal for proof-of-concept (POC) development or during the early stages of application development. For production environments, however, we strongly recommend using an AuthToken instead of an Auth Key to ensure enhanced security. The login method needs to be called in the following scenarios:- When the user is logging into the App for the first time.
- If the CometChat.getLoggedInUser() function returns nil.
- Swift
- Objective C
| Parameter | Description |
|---|---|
| UID | The UID of the user that you would like to login |
| authKey | CometChat Auth Key |
login() method returns the User object containing all the information of the logged-in user.
Sample Payloads
Sample Payloads
- Request
- Success Response
- Error Response (Invalid UID)
- Error Response (Invalid AuthKey)
Method:
CometChat.login(UID:authKey:)| Parameter | Type | Value |
|---|---|---|
| UID | String | "cometchat-uid-2" |
| authKey | String | "537ab75a0d76f93a811d0cb390a3a0de936b3ed0" |
Login using Auth Token
This advanced authentication procedure does not use the Auth Key directly in your client code thus ensuring safety.- Create a user via the CometChat API when the user signs up in your app.
- Create an Auth Token via the CometChat API for the new user and save the token in your database.
- Load the Auth Token in your client and pass it to the
login()method.
- When the user is logging into the App for the first time.
- If the CometChat.getLoggedInUser() function returns nil.
- Swift
- Objective C
| Parameter | Description |
|---|---|
| authToken | Auth Token of the user you would like to login |
login() method returns the User object containing all the information of the logged-in user.
Sample Payloads
Sample Payloads
- Request
- Success Response
- Error Response (Invalid Token)
Method:
CometChat.login(authToken:)| Parameter | Type | Value |
|---|---|---|
| authToken | String | "cometchat-uid-2_1770120517a02ff84f63e515ece9895ebe2481df" |
Logout
You can use thelogout() method to log out the user from CometChat.
- Swift
- Objective C
Sample Payloads
Sample Payloads
- Request
- Success Response
- Error Response
Method:
CometChat.logout()| Parameter | Type | Value |
|---|---|---|
| — | — | No request parameters |
Helper Methods
getLoggedInUser()
UseCometChat.getLoggedInUser() to check if a user is currently logged in. This method returns the logged-in User object or nil if no user is logged in.
- Swift
Sample Payloads
Sample Payloads
- Response (No User Logged In)
- Response (User Logged In)
Method:
CometChat.getLoggedInUser()Return Type: User?| Parameter | Type | Value |
|---|---|---|
| return | User? | nil (no user logged in) |
Object Structures
User Object
TheUser object represents a CometChat user and contains all user-related information.
| Parameter | Type | Description |
|---|---|---|
| uid | String? | Unique user identifier assigned during user creation |
| name | String? | Display name of the user |
| avatar | String? | Avatar image URL |
| link | String? | Profile link URL |
| role | String? | User role (e.g., "default", "admin", "moderator") |
| metadata | [String: Any]? | Custom metadata dictionary for storing additional user data |
| status | CometChat.UserStatus | User status: .online (0), .offline (1), or .available |
| statusMessage | String? | Custom status message set by the user |
| lastActiveAt | Double | Unix timestamp of last activity |
| hasBlockedMe | Bool | true if this user has blocked the logged-in user |
| blockedByMe | Bool | true if the logged-in user has blocked this user |
| deactivatedAt | Double | Unix timestamp when user was deactivated (0.0 if active) |
| tags | [String] | Array of tags associated with the user |
| authToken | String? | Authentication token for the session |
| createdAt | Double? | Unix timestamp of account creation |
| updatedAt | Double? | Unix timestamp of last profile update |
CometChatException Object
TheCometChatException object represents an error returned by the SDK.
| Parameter | Type | Description |
|---|---|---|
| errorCode | String | Unique error code identifier for programmatic handling |
| errorDescription | String | Human-readable error message |
| details | String? | Additional context or troubleshooting information |
Common Error Codes
| Error Code | Description | Resolution |
|---|---|---|
ERR_UID_NOT_FOUND | User with specified UID does not exist | Create user first via API or Dashboard |
ERR_INVALID_API_KEY | Invalid Auth Key provided | Verify Auth Key from Dashboard |
AUTH_ERR_APIKEY_NOT_FOUND | Auth Key does not exist | Verify Auth Key from Dashboard |
AUTH_ERR_AUTH_TOKEN_NOT_FOUND | Auth Token does not exist or is invalid | Generate new Auth Token |
ERR_USER_DEACTIVATED | User account is deactivated | Reactivate user via Dashboard |
ERR_NOT_LOGGED_IN | No user is currently logged in | Call login() first |
ERR_SDK_NOT_INITIALIZED | SDK not initialized | Call CometChat.init() first |
ERR_NETWORK_ERROR | Network connectivity issue | Check internet connection and retry |
CometChatLoginDelegate
TheCometChatLoginDelegate protocol allows you to listen for authentication events (login/logout) across your application. This is useful for updating UI state, managing session data, or triggering side effects when authentication status changes.
Setup
To receive login/logout events, conform toCometChatLoginDelegate and register your class:
- Swift
onLoginSuccess(user: User)
Called when a user successfully logs in viaCometChat.login().
Sample Payloads
Sample Payloads
- Request
- Success Response
- Error Response
Trigger:
CometChat.login(UID:authKey:) or CometChat.login(authToken:)| Parameter | Type | Value |
|---|---|---|
| UID | String | "cometchat-uid-2" |
| authKey | String | "AUTH_KEY" |
| Scenario | Behavior |
|---|---|
| Delegate not set | Method will not be called even on successful login |
| SDK not initialized | Login will fail before delegate is called |
| Already logged in | Calling login again returns existing user; delegate is still called |
| Network disconnection after login | Login succeeds; connection listener handles reconnection |
| User deactivated after login | User object contains deactivatedAt timestamp |
onLoginFailed(error: CometChatException?)
Called when a login attempt fails.Sample Payloads
Sample Payloads
- Request
- Success Response
- Error Response
Trigger:
CometChat.login(UID:authKey:) with invalid credentials| Parameter | Type | Value |
|---|---|---|
| UID | String | "non_existent_user_xyz_99999" |
| authKey | String | "AUTH_KEY" |
| Scenario | Behavior |
|---|---|
error parameter is nil | Rare; indicates internal SDK error - log and retry |
| Multiple rapid login failures | Implement exponential backoff to avoid rate limiting |
| Network timeout | ERR_NETWORK_ERROR returned; safe to retry |
| Invalid credentials cached | Clear stored credentials and prompt user to re-enter |
onLogoutSuccess()
Called when a user successfully logs out viaCometChat.logout().
Sample Payloads
Sample Payloads
- Request
- Success Response
- Error Response
Trigger:
CometChat.logout()| Parameter | Type | Value |
|---|---|---|
| — | — | No request parameters |
| Scenario | Behavior |
|---|---|
| Logout while offline | May fail with network error; local session persists |
| Logout during active call | Call is terminated before logout completes |
| Multiple logout calls | Subsequent calls may return ERR_NOT_LOGGED_IN |
| App killed during logout | Session may persist; check on next app launch |
onLogoutFailed(error: CometChatException?)
Called when a logout attempt fails.Sample Payloads
Sample Payloads
- Request
- Success Response
- Error Response
Trigger:
CometChat.logout() when no user is logged in| Parameter | Type | Value |
|---|---|---|
| — | — | No request parameters |
| Scenario | Behavior |
|---|---|
error parameter is nil | Rare; treat as unknown error |
| Logout fails due to network | Consider force local logout option |
| SDK crashes during logout | Local session may persist; handle on next launch |
Complete Implementation Example
Full CometChatLoginDelegate Implementation
Full CometChatLoginDelegate Implementation
Section Validation
Documentation Self-Sufficiency Check
Conclusion: This section is now self-sufficient. A developer can implement
| Criteria | Status |
|---|---|
| All delegate methods documented | ✅ Complete |
| Sample Payloads (Request/Success/Error) in tabular format | ✅ Complete for all methods |
| Property tables with types | ✅ Complete |
| Common error codes | ✅ Documented with resolutions |
| Edge cases documented | ✅ For all methods |
| Setup instructions | ✅ Complete |
| Complete implementation example | ✅ Included |
| Cross-references with links | ✅ Added |
CometChatLoginDelegate without guessing or requiring additional SDK exploration.