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
> **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, such as possible infinite redirect loops 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).
2
+
> 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/custom-session-token.mdx
+39-23Lines changed: 39 additions & 23 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -17,15 +17,15 @@ This guide will show you how to customize a session token to include additional
17
17
1. In the Clerk Dashboard, navigate to the [**Sessions**](https://dashboard.clerk.com/last-active?path=sessions) page.
18
18
1. Under **Customize session token**, in the **Claims** editor, you can add any claim to your session token that you need and select **Save**.
19
19
20
-
The following example adds the `metadata` claim to the session token.
20
+
The following example adds the `fullName` and `primaryEmail` claims to the session token.
21
21
22
22

23
23
24
24
## Use the custom claims in your application
25
25
26
26
The [`Auth`](/docs/references/backend/types/auth-object) object includes a `sessionClaims` property that contains the custom claims you added to your session token. Accessing the `Auth` object differs depending on the framework you are using. See the [reference doc](/docs/references/backend/types/auth-object) for more information.
27
27
28
-
The following example demonstrates how to access the `metadata` claim that was added to the session token in the last step.
28
+
The following example demonstrates how to access the `fullName` and `primaryEmail` claims that were added to the session token in the last step.
Copy file name to clipboardExpand all lines: docs/backend-requests/resources/session-tokens.mdx
+4-6Lines changed: 4 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -98,9 +98,9 @@ You can also create custom tokens using a [JWT template](/docs/backend-requests/
98
98
99
99
## Size limitations
100
100
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, such as possible infinite redirect loops 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.
102
102
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.**
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.
104
104
105
105
Claims to monitor for size limits:
106
106
@@ -110,14 +110,14 @@ Claims to monitor for size limits:
110
110
-`org.public_metadata`
111
111
-`org_membership.public_metadata`
112
112
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.
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.
114
114
115
115
> [!NOTE]
116
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).
117
117
118
118
### Example
119
119
120
-
It's recommended to keep the total size of custom claims in the session token under 1.2KB. Move particularly large claims out of the session token and fetch them using a separate API call from your backend.
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
121
122
122
For example, if you were storing several fields in `user.public_metadata`, like this:
123
123
@@ -343,8 +343,6 @@ If you need to access the other fields, you can fetch them using a separate API
343
343
</Tab>
344
344
</Tabs>
345
345
346
-
However, 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. So it's 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.
347
-
348
346
## Validate session tokens
349
347
350
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