Skip to content

Commit 9d9985f

Browse files
brunol95cursoragentalexisintech
authored
add user.updateMetadata method (#3366)
Co-authored-by: Cursor <cursoragent@cursor.com> Co-authored-by: Alexis Aguilar <98043211+alexisintech@users.noreply.github.com>
1 parent 6134744 commit 9d9985f

2 files changed

Lines changed: 27 additions & 5 deletions

File tree

docs/guides/users/extending.mdx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,14 @@ See the following examples to see how to set unsafe metadata on the frontend (cl
6161

6262
<Tabs items={["Client-side", "Server-side"]}>
6363
<Tab>
64+
Use the [`User.updateMetadata()`](/docs/reference/objects/user#update-metadata) method to update unsafe metadata from the frontend. The provided value is **deep-merged** with the existing `unsafeMetadata`, and any key set to `null` is removed.
65+
66+
> [!TIP]
67+
> You can also use [`User.update()`](/docs/reference/objects/user#update) to update unsafe metadata, but it **fully replaces** the value instead of merging. Use `User.updateMetadata()` when you need to change specific keys.
68+
6469
<Tabs items={["React-based SDKs", "JavaScript"]}>
6570
<Tab>
66-
For React-based SDKs, such as Next.js, use the [`useUser()`](/docs/reference/hooks/use-user) hook to update unsafe metadata.
71+
For React-based SDKs, such as Next.js, use the [`useUser()`](/docs/reference/hooks/use-user) hook to access the `User` object and call the `updateMetadata()` method.
6772

6873
```tsx {{ filename: 'page.tsx' }}
6974
export default function Page() {
@@ -76,7 +81,7 @@ See the following examples to see how to set unsafe metadata on the frontend (cl
7681

7782
<button
7883
onClick={() => {
79-
user?.update({
84+
user?.updateMetadata({
8085
unsafeMetadata: { birthday },
8186
})
8287
}}
@@ -90,7 +95,7 @@ See the following examples to see how to set unsafe metadata on the frontend (cl
9095
</Tab>
9196

9297
<Tab>
93-
When using the JavaScript SDK, use the [`User.update()`](/docs/reference/objects/user#update) method to update unsafe metadata.
98+
When using the JavaScript SDK, use the [`Clerk`](/docs/reference/objects/clerk) object to access the `User` object and call the `updateMetadata()` method.
9499

95100
```js {{ filename: 'main.js' }}
96101
import { Clerk } from '@clerk/clerk-js'
@@ -103,7 +108,7 @@ See the following examples to see how to set unsafe metadata on the frontend (cl
103108

104109
if (clerk.isSignedIn) {
105110
await clerk.user
106-
.update({
111+
.updateMetadata({
107112
unsafeMetadata: {
108113
birthday: '01-01-2000',
109114
},

docs/reference/objects/user.mdx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -775,7 +775,24 @@ function update(params: UpdateUserParams): Promise<User>
775775
- `unsafeMetadata?`
776776
- [`UserUnsafeMetadata`](/docs/reference/types/metadata#user-unsafe-metadata)
777777

778-
Metadata that can be read and set from the Frontend API. One common use case for this attribute is to implement custom fields that will be attached to the `User` object. Updating this value overrides the previous value; it does not merge. To perform a merge, you can pass something like `{ …user.unsafeMetadata, …newData }` to this parameter.
778+
Metadata that can be read and set from the Frontend API. One common use case for this attribute is to implement custom fields that will be attached to the `User` object. Using `update()` to update this value fully replaces the existing value. To perform a merge, use [`updateMetadata()`](#update-metadata) instead.
779+
</Properties>
780+
781+
### `updateMetadata()`
782+
783+
Updates the user's `unsafeMetadata` using deep-merge semantics. Unlike [`update()`](#update), which fully replaces `unsafeMetadata`, this method merges the provided value with the existing `unsafeMetadata`. Top-level and nested keys are merged, and any key set to `null` is removed. Only `unsafeMetadata` is writable from the frontend; `publicMetadata` and `privateMetadata` can only be set from the [Backend API](/docs/reference/backend-api){{ target: '_blank' }}.
784+
785+
```ts
786+
function updateMetadata(params: UpdateUserMetadataParams): Promise<User>
787+
```
788+
789+
#### `UpdateUserMetadataParams`
790+
791+
<Properties>
792+
- `unsafeMetadata`
793+
- [`UserUnsafeMetadata`](/docs/reference/types/metadata#user-unsafe-metadata)
794+
795+
The metadata to deep-merge with the user's existing `unsafeMetadata`. Keys at any nesting level whose value is `null` are removed.
779796
</Properties>
780797

781798
### `updatePassword()`

0 commit comments

Comments
 (0)