Create, update, and manage users in your CometChat application
Quick Reference for AI Agents & Developers
Report incorrect code
Copy
Ask AI
// Create user (requires Auth Key - use on backend only)const user = new CometChat.User("user_uid");user.setName("John Doe");user.setAvatar("https://example.com/avatar.png");user.setRole("premium");user.setMetadata({ department: "Engineering" });await CometChat.createUser(user, authKey);// Update any user (requires Auth Key)await CometChat.updateUser(user, authKey);// Update current logged-in user (no Auth Key needed)await CometChat.updateCurrentUserDetails(user);// Get user propertiesconst user = await CometChat.getUser("user_uid");user.getUid(), user.getName(), user.getAvatar(), user.getStatus()// Delete user: REST API only (security)
User management is the foundation of your chat application. Before users can send messages, they need to exist in CometChat.
Availability: SDK (with Auth Key), REST APIFor production apps, user creation should happen on your backend using the REST API. SDK methods are useful for development and prototyping.
Creating users requires the Auth Key. In production, this should only be done from your backend server. Never expose your Auth Key in client-side code.
JavaScript
TypeScript
Async/Await
Report incorrect code
Copy
Ask AI
const authKey = "YOUR_AUTH_KEY";const uid = "user1";const name = "Kevin";const user = new CometChat.User(uid);user.setName(name);CometChat.createUser(user, authKey).then( (user) => console.log("User created:", user), (error) => console.log("Failed to create user:", error));
The logged-in user can update their own profile without the Auth Key:
JavaScript
TypeScript
Async/Await
Report incorrect code
Copy
Ask AI
const user = new CometChat.User("user1");user.setName("Kevin Fernandez");user.setAvatar("https://example.com/new-avatar.png");user.setStatusMessage("In a meeting");CometChat.updateCurrentUserDetails(user).then( (user) => console.log("Profile updated:", user), (error) => console.log("Failed to update:", error));
Report incorrect code
Copy
Ask AI
const user: CometChat.User = new CometChat.User("user1");user.setName("Kevin Fernandez");user.setAvatar("https://example.com/new-avatar.png");user.setStatusMessage("In a meeting");CometChat.updateCurrentUserDetails(user).then( (user: CometChat.User) => console.log("Profile updated:", user), (error: CometChat.CometChatException) => console.log("Failed:", error));
Report incorrect code
Copy
Ask AI
const updateMyProfile = async () => { try { const user = new CometChat.User("user1"); user.setName("Kevin Fernandez"); user.setAvatar("https://example.com/new-avatar.png"); user.setStatusMessage("In a meeting"); const updatedUser = await CometChat.updateCurrentUserDetails(user); console.log("Profile updated:", updatedUser); } catch (error) { console.log("Failed to update:", error); }};
updateCurrentUserDetails() only updates the logged-in user regardless of the UID provided. You cannot change the user’s role with this method.