Quick Reference - Create and update users:
- You add the user details in your database
- You create a user in CometChat
- You log in the user to your app
- You log in the user to CometChat (programmatically)
Creating a user
Ideally, user creation should take place at your backend. You can refer to our Rest API to learn more about creating a user and use the appropriate code sample based on your backend language. However, if you wish to create users on the fly, you can use thecreateUser() method. This method takes a User object and the Auth Key as input parameters and returns the created User object if the request is successful.
- JavaScript
- TypeScript
Response
Response
On Success —
createUser() returns the created User object:When creating a user with all optional fields set:
Updating a user
Updating a user similar to creating a user should ideally be achieved at your backend using the Restful APIs. For more information, you can check the update a user section. However, this can be achieved on the fly as well as using theupdateUser() method. This method takes a User object and the Auth Key as inputs and returns the updated User object on the successful execution of the request.
- JavaScript
- TypeScript
Response
Response
On Success —
updateUser() returns the updated User object:When updating a user with all optional fields:
User object provided to the updateUser() method has the UID of the user to be updated set.
Updating logged-in user
Updating a logged-in user is similar to updating a user. The only difference being this method does not require an AuthKey. This method takes aUser object as input and returns the updated User object on the successful execution of the request.
- JavaScript
- TypeScript
Response
Response
On Success —
updateCurrentUserDetails() returns the updated User object with additional session data:updateCurrentUserDetails() method one can only update the logged-in user irrespective of the UID passed. Also, it is not possible to update the role of a logged-in user.
Deleting a user
Deleting a user can only be achieved via the Restful APIs. For more information please check the delete a user section.User Class
Best Practices
Create users from your backend
Create users from your backend
Use the REST API with your API Key to create users server-side. The
createUser() SDK method requires the Auth Key, which should not be exposed in production client apps.Keep UIDs consistent with your system
Keep UIDs consistent with your system
Use the same user identifier from your backend as the CometChat UID. This simplifies mapping between your user system and CometChat.
Use updateCurrentUserDetails for self-updates
Use updateCurrentUserDetails for self-updates
When the logged-in user updates their own profile, use
updateCurrentUserDetails() which doesn’t require an Auth Key. Reserve updateUser() for admin-level operations from your backend.Troubleshooting
createUser fails with 'UID already exists'
createUser fails with 'UID already exists'
Each UID must be unique across your CometChat app. If the user already exists, use
updateUser() instead, or skip creation and proceed to login.updateCurrentUserDetails doesn't update role
updateCurrentUserDetails doesn't update role
Role updates are not allowed via
updateCurrentUserDetails(). Use the REST API or updateUser() with Auth Key to change a user’s role.UID validation error
UID validation error
UIDs can only contain alphanumeric characters, underscores, and hyphens. Spaces, punctuation, and other special characters are not allowed.
Next Steps
Retrieve Users
Fetch user lists, search users, and get user details
Block Users
Block and unblock users, retrieve blocked user lists
Authentication
Log users into CometChat after creation
User Presence
Track online/offline status of users in real time