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.
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.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.
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.- TypeScript
- JavaScript
Check for an Existing Session
The SDK persists the logged-in user’s session locally. Before callinglogin(), always check whether a session already exists — this avoids unnecessary login calls and keeps your app responsive.
getLoggedinUser() returns null, no active session exists and you need to call login().
Login with Auth Key
Auth Key login is the simplest way to get started. Pass a UID and your Auth Key directly from the client.- TypeScript
| Parameters | Description |
|---|---|
| UID | The UID of the user that you would like to login |
| authKey | CometChat Auth Key |
User object on Promise resolved.
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.
- JavaScript
async/await syntax:| Parameter | Description |
|---|---|
| UID | The UID of the user to log in |
| authKey | Your CometChat Auth Key |
Promise resolves with a User object containing the logged-in user’s details.
Login with Auth Token
Auth Token login keeps your Auth Key off the client entirely. Your server generates a token via the REST API and passes it to the client.- Create the user via the REST API when they sign up (first time only).
- Generate an Auth Token on your server and return it to the client.
- Pass the token to
login().
- TypeScript
- JavaScript
| Parameter | Description |
|---|---|
| authToken | Auth Token generated on your server for the user |
Promise resolves with a User object containing the logged-in user’s details.
Logout
Calllogout() when your user logs out of your app. This clears the local session.
- TypeScript
- JavaScript
Login Listener
You can listen for login and logout events in real time usingLoginListener. This is useful for updating UI state or triggering side effects when the auth state changes.
| Callback | Description |
|---|---|
loginSuccess(event) | User logged in successfully. Provides the User object. |
loginFailure(event) | Login failed. Provides a CometChatException. |
logoutSuccess() | User logged out successfully. |
logoutFailure(event) | Logout failed. Provides a CometChatException. |
Add a Listener
- TypeScript
- JavaScript
Remove a Listener
Next Steps
Send Messages
Send your first text, media, or custom message
User Management
Create, update, and delete users programmatically
Connection Status
Monitor the SDK connection state in real time
All Real-Time Listeners
Complete reference for all SDK event listeners