

























Reading data about the authenticated user is a fundamental part of any application.
Whether you need to access the user's ID, username, contact information, or profile data, Clerk provides various methods to accomplish this depending on your runtime environment and application needs.
Options are great! But they can also make it challenging to decide which approach to take. I wrote this guide to explain the different methods and their appropriate use cases. This way, you can choose the most suitable and efficient option for your circumstances.
Below is a table summarizing the different approaches to read authenticated user data from Clerk.
Use it to understand your options at a glance or reference the next time you return to this page.

useUser()Clerk's Frontend API enables authenticated users to access their own data and perform actions specific to their account from a browser or native environment.
To access the authenticated user, you could call the /v1/me endpoint. However, the convenient option in Next.js is to invoke useUser(), which queries /v1/me under the hood.
Certain Frontend API requests are rate-limited but /v1/me is not. This endpoint has no defined limit, making it practically unlimited.
When to use Frontend API with useUser():
useUser() from a client component when you need to update the UI with information about the authenticated user.useUser() returns a User with a reload() function that makes it even easier to render the most up-to-date user information.When to avoid:
useUser() won't work from a server environment and is therefore inappropriate for server-side-rendering user information.currentUser()Clerk's Backend API is designed to query user data from a server environment.
The currentUser() helper is used to access information about the currently authenticated user from server components, server actions, and other server-side code like so:
When to use currentUser():
currentUser() from a Next.js server component, server action, API route handler, or other backend code where you require data about the authenticated user.user.organizatons from a backend environment. Dynamic attributes like these are usually too big for custom session claims (session claims are the subject of the next section).When to avoid:
429 Too Many Requests error and your application might not work properly.<UserProfile />, your database won't be synchronized. Instead, store only the user ID and fetch the latest user data from Clerk as needed.Claims are pieces of information about the authenticated user. They're encoded in the Clerk session token and digitally signed to ensure the information is authentic.
Claims are accessible from frontend or backend environments, and since they don't require an API roundtrip, rate limits do not apply.
The user ID is included in the token by default. However, the user's username, contact information, and other attributes are not. To access this optional data, it's necessary to customize the session token with custom claims.
Below are two code examples demonstrating how to access the authenticated user's ID on the client and on the server with the useAuth() and auth() helpers respectively:
To customize session claims for your application, open the Clerk dashboard then click Sessions.
Look for the "Customize session token section" and press Edit.
Define a JSON structure that includes dynamic placeholders called shortcodes (remember to Save after). Clerk will replace these shortcodes with the actual values at runtime:
Read custom session claims from a backend environment with the same auth() helper and the returned sessionClaims property.
Custom session claims are technically accessible from frontend environments, but this is rarely needed, so the Next.js SDK doesn't provide a dedicated helper.
When to use session claims:
When to avoid:
user.organizations, which can cause the token to exceed this limit and break your app. Only store essential, predictably-sized user data in the token, and occasionally fetch larger claims through separate Backend API calls.useUser(), as described at the top of this guide.此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。