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
+
>
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](/docs/guides/force-token-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.
> Clerk stores the session token in a cookie, and most browsers cap cookie size at [**4KB**](https://datatracker.ietf.org/doc/html/rfc2109#section-6.3). After accounting for the size of Clerk's default claims, the cookie can support **up to 1.2KB** of custom claims. Exceeding this limit will cause the cookie to not be set, which will break your app as Clerk depends on cookies to work properly. [Learn more](/docs/backend-requests/resources/session-tokens#size-limitations).
Copy file name to clipboardExpand all lines: docs/backend-requests/resources/session-tokens.mdx
+233-3Lines changed: 233 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
+
Clerk stores the session token in a cookie, and [**most browsers limit cookie size to 4KB**](https://datatracker.ietf.org/doc/html/rfc2109#section-6.3). Exceeding this limit will cause the cookie to not be set, which will break your app as Clerk depends on cookies to work properly.
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, you must consider this limit when implementing a [custom session token](/docs/backend-requests/custom-session-token). After accounting for the size of Clerk's default claims, the cookie can support **up to 1.2KB** of custom claims.
102
104
103
105
Claims to monitor for size limits:
104
106
@@ -108,11 +110,239 @@ 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 highly recommended to [store the extra data in your own database](/docs/webhooks/sync-data#storing-extra-user-data) instead of storing it in metadata in the session token. If this isn't an option, you can [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
+
It's recommended to keep the total size of custom claims in the session token under 1.2KB. The following example shows how to move particularly large claims out of the session token and fetch them using a separate API call from your backend. The limitations of this approach are that if you make this call to Clerk's Backend API frequently, you risk hitting [rate limits](/docs/backend-requests/resources/rate-limits) and it's also slower than making a database query. We highly recommend [storing the extra data in your own database](/docs/webhooks/sync-data#storing-extra-user-data) instead of storing it in metadata in the session token.
121
+
122
+
For example, if you were storing several fields in `user.public_metadata`, like this:
123
+
124
+
```js {{ prettier: false }}
125
+
// user.public_metadata
126
+
{
127
+
onboardingComplete:true,
128
+
birthday:'2000-01-01',
129
+
country:'Canada',
130
+
bio:'This is a bio -- imagine it is 6kb of written info',
131
+
}
132
+
```
133
+
134
+
Instead of storing all of that data in the session token, and possibly exceeding the 4KB limit, like this:
135
+
136
+
```json
137
+
// Custom claims in the session token
138
+
{
139
+
"metadata": "{{user.public_metadata}}"
140
+
}
141
+
```
142
+
143
+
You could store only the necessary data in the session token - for example, just 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
336
+
const user =userId?awaitclerkClient.users.getUser(userId) :null
337
+
338
+
// Return the Backend User object
339
+
returnjson({ user })
340
+
},
341
+
})
342
+
```
343
+
</Tab>
344
+
</Tabs>
345
+
116
346
## Validate session tokens
117
347
118
348
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