You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
> Metadata is limited to **8KB** maximum. If you're storing metadata as a custom claim in the session token, it's highly recommended to keep it under **1.2KB**. [Learn more about the session token size limitations](/docs/backend-requests/resources/session-tokens#size-limitations).
3
3
>
4
4
> If you use Clerk metadata and modify it server-side, the changes won't appear in the session token until the next refresh. To avoid race conditions, either force a JWT refresh after metadata changes or handle the delay in your application logic.
> The entire session token has a max size of 4KB. Exceeding this size can have adverse effects, including a possible infinite redirect loop for users who exceed this size in Next.js applications.
3
-
> It's recommended to move particularly large claims out of the JWT and fetch these using a separate API call from your backend.
> **It's recommended to keep the total size of custom claims in the session token under 1.2KB.** Exceeding this size can have adverse effects, including a possible infinite redirect loop for users who exceed this size in Next.js applications. It's recommended to move particularly large claims out of the JWT and fetch these using a separate API call from your backend. [Learn more](/docs/backend-requests/resources/session-tokens#size-limitations).
Copy file name to clipboardExpand all lines: docs/backend-requests/custom-session-token.mdx
+2Lines changed: 2 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -31,6 +31,8 @@ This guide will show you how to customize a session token to include additional
31
31
<Tab>
32
32
For Next.js, the `Auth` object is accessed using the `auth()` helper in App Router apps and the `getAuth()` function in Pages Router apps. [Learn more about using these helpers](/docs/references/nextjs/read-session-data#server-side).
33
33
34
+
Use the following tabs to see examples of how to use these helpers to access the user's ID in your App Router or Pages Router app.
Copy file name to clipboardExpand all lines: docs/backend-requests/resources/session-tokens.mdx
+231-3Lines changed: 231 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -90,15 +90,17 @@ Read more about Clerk session tokens and how they work in [the guide on how Cler
90
90
</Tab>
91
91
</Tabs>
92
92
93
+
## Custom claims
94
+
93
95
If you would like to add custom claims to your session token, you can [customize it](/docs/backend-requests/custom-session-token).
94
96
95
97
You can also create custom tokens using a [JWT template](/docs/backend-requests/jwt-templates).
96
98
97
99
## Size limitations
98
100
99
-
The Clerk session token is stored in a cookie. All modern browsers [limit the maximum size of a cookie to 4kb](https://datatracker.ietf.org/doc/html/rfc2109#section-6.3). Exceeding this limit can have adverse effects, including a possible infinite redirect loop for users who exceed this size in Next.js applications.
101
+
The Clerk session token is stored in a cookie. All modern browsers [limit the maximum size of a cookie to **4KB**](https://datatracker.ietf.org/doc/html/rfc2109#section-6.3). Exceeding this limit can have adverse effects, including a possible infinite redirect loop for users who exceed this size in Next.js applications.
100
102
101
-
A session token with the [default session claims](#default-claims) won't run into this issue, as this configuration produces a cookie significantly smaller than 4kb. However, this limitation becomes relevant when implementing a [custom session token](/docs/backend-requests/custom-session-token). In this case, it's recommended to move particularly large claims out of the token and fetch these using a separate API call from your backend.
103
+
A session token with the [default session claims](#default-claims) won't run into this issue, as this configuration produces a cookie significantly smaller than 4KB. However, this limitation becomes relevant when implementing a [custom session token](/docs/backend-requests/custom-session-token). **It's recommended to keep the total size of custom claims in the session token under 1.2KB.**
102
104
103
105
Claims to monitor for size limits:
104
106
@@ -108,11 +110,237 @@ Claims to monitor for size limits:
108
110
-`org.public_metadata`
109
111
-`org_membership.public_metadata`
110
112
111
-
If you include any of these claims in your token, use caution to ensure the stored data doesn't exceed the size limit.
113
+
If you add any of these custom claims in your token, use caution to ensure the stored data doesn't exceed the size limit. It's recommended to [move particularly large claims like these out of the token](#example) and fetch them using a separate API call from your backend.
112
114
113
115
> [!NOTE]
114
116
> If your application encounters this issue, the Clerk Dashboard will display a warning: **"Some users are exceeding cookie size limits"**. To resolve this, update your [custom session token](/docs/backend-requests/custom-session-token).
115
117
118
+
### Example
119
+
120
+
For example, if you were storing several fields in `user.public_metadata`, like this:
121
+
122
+
```js {{ prettier: false }}
123
+
// user.public_metadata
124
+
{
125
+
onboardingComplete:true,
126
+
birthday:'2000-01-01',
127
+
country:'Canada',
128
+
bio:'This is a bio -- imagine it is 6kb of written info',
129
+
}
130
+
```
131
+
132
+
Instead of storing all of that data in the session token, and possibly exceeding the 4KB limit, like this:
133
+
134
+
```json
135
+
// Custom claims in the session token
136
+
{
137
+
"metadata": "{{user.public_metadata}}"
138
+
}
139
+
```
140
+
141
+
You could store only the necessary data in the session token, such as only the `onboardingComplete` field, like this:
If you need to access the other fields, you can fetch them using a separate API call from your backend. The following example uses the [`getUser()`](/docs/references/backend/user/get-user) method to access the current user's [Backend `User` object](/docs/references/backend/types/backend-user), which includes the `publicMetadata` field.
// Use the Backend SDK's `getUser()` method to get the Backend User object
334
+
const user =userId?awaitclerkClient.users.getUser(userId) :null
335
+
336
+
// Return the Backend User object
337
+
returnjson({ user })
338
+
},
339
+
})
340
+
```
341
+
</Tab>
342
+
</Tabs>
343
+
116
344
## Validate session tokens
117
345
118
346
If you're using the middleware provided by our Clerk SDKs, validating session tokens is handled automatically in every request. If you're not using the middleware, you can still use the respective helpers provided by the SDKs to validate the tokens.
0 commit comments