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.
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.
The CometChat SDK maintains the session of the logged-in user within the SDK. Thus you do not need to call the login method for every session. You can use the CometChat.getLoggedinUser() method to check if there is any existing session in the SDK. This method should return the details of the logged-in user. If this method returns null, it implies there is no session present within the SDK and you need to log the user into CometChat.
Security Warning: This straightforward authentication method is ideal for proof-of-concept (POC) development or during the early stages of application development. For production environments, we strongly recommend using an Auth Token instead of an Auth Key to ensure enhanced security.
JavaScript
TypeScript
Report incorrect code
Copy
Ask AI
var UID = "UID";var authKey = "AUTH_KEY";// Check if user is already logged in before calling loginCometChat.getLoggedinUser().then( (user) => { if (!user) { CometChat.login(UID, authKey).then( (user) => { console.log("Login Successful:", user); }, (error) => { console.log("Login failed with exception:", error); } ); } }, (error) => { console.log("Something went wrong", error); });
Report incorrect code
Copy
Ask AI
var UID: string = "cometchat-uid-1", authKey: string = "AUTH_KEY";// Check if user is already logged in before calling loginCometChat.getLoggedinUser().then( (user: CometChat.User) => { if (!user) { CometChat.login(UID, authKey).then( (user: CometChat.User) => { console.log("Login Successful:", user); }, (error: CometChat.CometChatException) => { console.log("Login failed with exception:", error); } ); } }, (error: CometChat.CometChatException) => { console.log("Some Error Occured", error); });
Parameter
Description
UID
The UID of the user that you would like to login
authKey
CometChat Auth Key
After the user logs in, their information is returned in the User object on Promise resolved.
Sample Console Output
On Success — console.log("Login Successful:", user) returns:
On Failure — console.log("Login failed with exception:", error) returns:
Report incorrect code
Copy
Ask AI
Login failed with exception: { "code": "ERR_AUTH_TOKEN_NOT_FOUND", "name": "Auth token not found", "message": "The Auth Token provided is invalid or expired.", "details": {}}
You can use the logout() 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.