Skip to main content
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.
  1. For proof of concept/MVPs: Create the user using the CometChat Dashboard.
  2. 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.
Once initialization 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 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:
  1. When the user is logging into the App for the first time.
  2. If the CometChat.getLoggedInUser() function returns nil.
The login() method returns the User object containing all the information of the logged-in user.
Method: CometChat.login(UID:authKey:)

Login using Auth Token

This advanced authentication procedure does not use the Auth Key directly in your client code thus ensuring safety.
  1. Create a user via the CometChat API when the user signs up in your app.
  2. Create an Auth Token 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 into the App for the first time.
  2. If the CometChat.getLoggedInUser() function returns nil.
The login() method returns the User object containing all the information of the logged-in user.
Method: CometChat.login(authToken:)

Logout

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

Helper Methods

getLoggedInUser()

Use CometChat.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.
Method: CometChat.getLoggedInUser()Return Type: User?

Object Structures

User Object

The User object represents a CometChat user and contains all user-related information.

CometChatException Object

The CometChatException object represents an error returned by the SDK.

Common Error Codes


CometChatLoginDelegate

The CometChatLoginDelegate 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 to CometChatLoginDelegate and register your class:
Important Considerations
  • The delegate must be set before calling login() or logout() to receive events
  • Only one delegate can be registered at a time; setting a new delegate replaces the previous one
  • The delegate methods are called on the main thread
  • SDK must be initialized before setting the delegate
  • Delegate is not persisted across app restarts; re-register on app launch

onLoginSuccess(user: User)

Called when a user successfully logs in via CometChat.login().
Trigger: CometChat.login(UID:authKey:) or CometChat.login(authToken:)
Edge Cases & Considerations

onLoginFailed(error: CometChatException?)

Called when a login attempt fails.
Trigger: CometChat.login(UID:authKey:) with invalid credentials
Edge Cases & Considerations

onLogoutSuccess()

Called when a user successfully logs out via CometChat.logout().
Trigger: CometChat.logout()
Edge Cases & Considerations

onLogoutFailed(error: CometChatException?)

Called when a logout attempt fails.
Trigger: CometChat.logout() when no user is logged in
Edge Cases & Considerations

Complete Implementation Example


Section Validation

Documentation Self-Sufficiency CheckConclusion: This section is now self-sufficient. A developer can implement CometChatLoginDelegate without guessing or requiring additional SDK exploration.