Update User Information
Update User Information
Update current user information, including display name, avatar, user description, metadata, etc., using the updateUser method in the 'amityClient' class. This method updateUser accepts these optional parameters:
The method accepts the following optional parameters:
displayName- user's display namedescription- user's descriptionavatarFile- file ID of the user's avataravatarCustomUrl- custom url of the user's avatarmetadata- user's metadata
The updateCurrentUser method accepts the following optional parameters:
displayName- user's display namedescription- user's descriptionavatarFile- file ID of the user's avataravatarCustomUrl- custom url of the user's avatarroles- user's rolemetadata- user's metadata
Below is a sample code on how to update the current user's display name, description, and metadata. This method returns a promise. If the update is successful, the method will return true, else it throws an error.
const myMetadata = { whatever: “i want”, toPass: “is ok” }
const success = await amityClient.updateCurrentUser({
displayName: "Batman",
description : "Hero that Gotham needs",
metadata: myMetadata,
})Update Current User's Avatar
You can upload an image and set it as an avatar for the current user.
First, you need to upload image using AmityFileRepository and then update the image info.
Step 1 — Upload an image:
AmityFileRepositoryAmityFileRepositoryStep 2 — Passing AmityImageData from step 1 into the builder, and call user update API.
If you have an avatar present in your current system/server and just want to integrate that existing avatar to AmitySDK, you can just set the URL for the avatar directly.
Note: getAvatarInfo provides the information associated with a particular Avatar
First, you need to upload image and then update the image info. If you have an avatar present in your current system/server and just want to integrate that existing avatar to AmitySDK, you can just set the URL for the avatar directly.
If you have an avatar present in your current system/server and just want to integrate that existing avatar to AmitySDK, you can just set the URL for the avatar directly.
Either you wish to let us handle your user's avatar, or if you already have a system for it we got you covered. The 2 properties avatarFileId and avatarCustomUrl answer those two use-cases separately.
You can easily retrieve the url of a file from our FileRepository object and use it to display in your app later on. Here's an example in React:
import { useState, useEffect, useMemo } from 'react'
// our image component
const FileImage = ({ fileId }) => {
const [fileUrl, setFileUrl] = useState()
useEffect(() => {
const liveObject = FileRepository.fileInformationForId(fileId)
liveObject.on('dataUpdated', model => setFileUrl(model.fileUrl))
liveObject.model && setFileUrl(liveObject.model.fileUrl)
return () => {
liveObject.dispose()
}
}, [fileId])
return fileUrl ? <img src={fileUrl} /> : null
}
// our user component
const UserHeader = ({ userId }) => {
const [user, setUser] = useState({})
const displayName = useMemo(
() => user?.displayName ?? user?.userId,
[user],
)
useEffect(() => {
const liveObject = UserRepository.getUser(userId)
liveObject.on('dataUpdated', setUser)
liveObject.model && setUser(liveObject.model)
return () => {
liveObject.dispose()
}
}, [userId])
return (
<div>
{user.avatarFileId && <FileImage fileId={user.avatarFileId} />}
{user.avatarCustomUrl && <img src={user.avatarCustomUrl} />}
</div>
)
} You can easily retrieve the url of a file using observeFile and use it to display in your app later on. Here's an example in React:
Last updated
Was this helpful?