When a user logs into your app, you need to programmatically login the user into CometChat. But before you log in the user to CometChat, you need to create the user.Summing up-When a user registers in your app
Ideally, user creation should take place at your backend. You can refer 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 the createUser() 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.
Swift
Report incorrect code
Copy
Ask AI
let newUser : User = User(uid: "user1", name: "Kevin") // Replace with your uid and the name for the user to be created.let authKey = "AUTH_KEY" // Replace with your Auth Key.CometChat.createUser(user: newUser, apiKey: authKey, onSuccess: { (User) in print("User created successfully. \(User.stringValue())") }) { (error) in print("The error is \(String(describing: error?.description))")}
Sample Payload
Request Parameters:
Parameter
Type
Description
uid
String
Unique identifier for the new user. Must be alphanumeric with underscore/hyphen only. Example: "user1"
name
String
Display name for the new user. Example: "Kevin"
apiKey
String
Your CometChat API key for authentication. Example: "AUTH_KEY"
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 using the updateUser() method. This method takes a User object and the Auth Key as inputs and returns the updated User object on successful execution of the request.
Swift
Report incorrect code
Copy
Ask AI
let updateUser : User = User(uid: "user1", name: "Kevin Fernandez") // Replace with your uid and the name for the user to be created.let authKey = "AUTH_KEY" // Replace with your Auth Key.CometChat.updateUser(user: newUser1, apiKey: authKey, onSuccess: { (User) in print("User updated successfully. \(User.stringValue())") }) { (error) in print("The error is \(String(describing: error?.description))") }
Please make sure the User object provided to the updateUser() method has the UID of the user to be updated set.
Sample Payload
Request Parameters:
Parameter
Type
Description
uid
String
Unique identifier of the user to update. Example: "cometchat-uid-3"
name
String
New display name for the user. Example: "Nancy Grace"
avatar
String?
New avatar URL (optional). Example: nil
link
String?
New profile link URL (optional). Example: nil
role
String?
New role for the user (optional). Example: nil
statusMessage
String?
New status message (optional). Example: nil
metadata
[String: Any]?
New metadata dictionary (optional). Example: nil
tags
[String]?
New tags array (optional). Example: nil
apiKey
String
Your CometChat API key for authentication. Example: "AUTH_KEY"
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 a User object as input and returns the updated User object on the successful execution of the request.
Swift
Report incorrect code
Copy
Ask AI
let currentUser = User(uid: "cometchat-uid-1", name: "Andrew Joseph")CometChat.updateCurrentUserDetails(user: currentUser, onSuccess: { user in print("Updated user object",user)}, onError: { error in print("Update user failed with error: \(error?.errorDescription)")})
By using the 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.
Sample Payload
Request Parameters:
Parameter
Type
Description
uid
String
UID of the logged-in user (ignored, uses actual logged-in user). Example: "cometchat-uid-2"
name
String
New display name for the user. Example: "Updated Name"
avatar
String?
New avatar URL (optional). Example: nil
link
String?
New profile link URL (optional). Example: nil
statusMessage
String?
New status message (optional). Example: "Available"