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.
Authentication Flow
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.- JavaScript
- TypeScript
- Async/Await
| 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
- TypeScript
- Async/Await
| Parameter | Description |
|---|---|
| authToken | Auth Token of the user you would like to login |
User object on the Promise resolved.
Logout
You can use thelogout() method to log out the user from CometChat. We suggest you call this method once your user has been successfully logged out from your app.
- JavaScript
- TypeScript
- Async/Await
Best Practices
Always check for existing sessions
Always check for existing sessions
Before calling
login(), use CometChat.getLoggedinUser() to check if a session already exists. This avoids unnecessary login calls and prevents session conflicts.Use Auth Tokens in production
Use Auth Tokens in production
Auth Keys are convenient for development but expose your app to security risks in production. Always generate Auth Tokens server-side using the REST API and pass them to the client.
Handle token expiry gracefully
Handle token expiry gracefully
Auth Tokens can expire. Implement a mechanism to detect login failures due to expired tokens and re-generate them from your server. Use the Login Listener to detect session changes.
Logout on user sign-out
Logout on user sign-out
Always call
CometChat.logout() when your user signs out of your app. This clears the SDK session and stops real-time event delivery, preventing stale data and memory leaks.Troubleshooting
Login fails with 'UID not found'
Login fails with 'UID not found'
Login fails with 'Auth Key is not valid'
Login fails with 'Auth Key is not valid'
Verify your Auth Key matches the one in your CometChat Dashboard → API & Auth Keys. Ensure you haven’t accidentally used the REST API Key instead.
Login fails with 'App not found'
Login fails with 'App not found'
Ensure
CometChat.init() has been called and completed successfully before calling login(). Verify your App ID and Region are correct.getLoggedinUser() returns null after page refresh
getLoggedinUser() returns null after page refresh
This can happen if the SDK session was not persisted. Ensure
init() is called on every app load before checking getLoggedinUser(). The SDK stores session data in the browser — clearing browser storage will clear the session.