Quick Reference - Login with Auth Key (dev) or Auth Token (production):
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 Users: We have set up 5 users for testing with 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
- JavaScript
- TypeScript
| Parameter | Description |
|---|---|
| UID | The UID of the user that you would like to login |
| authKey | CometChat Auth Key |
User object on Promise resolved.
Response
Response
On Success — Returns a User object:User Object:
| Parameter | Type | Description | Sample Value |
|---|---|---|---|
uid | string | Unique identifier of the user | "cometchat-uid-2" |
name | string | Display name of the user | "George Alan" |
authToken | string | Authentication token for the session | "cometchat-uid-2_17713124898af10df254d51ef6ffc14e79955ac0" |
avatar | string | URL to user’s avatar image | "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-2.webp" |
status | string | Current online status | "online" |
role | string | User’s role | "default" |
lastActiveAt | number | Unix timestamp of last activity | 1771311515 |
hasBlockedMe | boolean | Whether this user has blocked the logged-in user | false |
blockedByMe | boolean | Whether logged-in user has blocked this user | false |
deactivatedAt | number | Unix timestamp if user is deactivated (0 if active) | 0 |
tags | array | Tags associated with the user | [] |
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
Create a User via the CometChat API when the user signs up in your app.
Create an Auth Token
Create an Auth Token via the CometChat API for the new user and save the token in your database.
- JavaScript
- TypeScript
| Parameter | Description |
|---|---|
| authToken | Auth Token of the user you would like to login |
User object on the Promise resolved.
Response
Response
On Success — Returns a User object:User Object:
| Parameter | Type | Description | Sample Value |
|---|---|---|---|
uid | string | Unique identifier of the user | "cometchat-uid-2" |
name | string | Display name of the user | "George Alan" |
authToken | string | Authentication token for the session | "cometchat-uid-2_17713124898af10df254d51ef6ffc14e79955ac0" |
avatar | string | URL to user’s avatar image | "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-2.webp" |
status | string | Current online status | "online" |
role | string | User’s role | "default" |
lastActiveAt | number | Unix timestamp of last activity | 1771311515 |
hasBlockedMe | boolean | Whether this user has blocked the logged-in user | false |
blockedByMe | boolean | Whether logged-in user has blocked this user | false |
deactivatedAt | number | Unix timestamp if user is deactivated (0 if active) | 0 |
tags | array | Tags associated with the user | [] |
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
Response
Response
On Success — Returns a success message:
| Parameter | Type | Description | Sample Value |
|---|---|---|---|
message | string | Logout confirmation | "Logout completed successfully" |
Best Practices
Best Practices
- Always check for an existing session with
getLoggedinUser()before callinglogin() - Use Auth Token (not Auth Key) in production environments
- Generate Auth Tokens server-side and never expose your REST API Key in client code
- Call
logout()when the user logs out of your app to clean up the CometChat session - Handle login errors gracefully and provide user-friendly error messages
Troubleshooting
Troubleshooting
- Login fails with “UID not found”: Ensure the user has been created in CometChat before attempting login
- Auth Token expired: Generate a new Auth Token from your server and retry login
- Session persists after logout: Ensure
logout()completes successfully before redirecting - Multiple login calls: Use
getLoggedinUser()to prevent redundant login attempts