Read session and user data in your Next.js app with Clerk
Clerk provides a set of hooks and helpers that you can use to access the active session and user data in your Next.js application. Here are examples of how to use these helpers in both the client and server-side to get you started.
Server-side
App Router
auth()
and currentUser()
are App Router-specific helpers that you can use inside of your Route Handlers, Middleware, Server Components, and Server Actions.
The auth()
helper will return the Auth
object of the currently active user. Now that request data is available in the global scope through Next.js's headers()
and cookies()
methods, passing the request object to Clerk is no longer required.
The currentUser()
helper will return the Backend User
object of the currently active user. This is helpful if you want to render information, like their first and last name, directly from the server.
Under the hood, currentUser()
uses the clerkClient
wrapper to make a call to Clerk's Backend API. This does count towards the Backend API Request Rate Limit. This also uses fetch()
so it is automatically deduped per request.
This example uses the new auth()
helper to validate an authenticated user and the new currentUser()
helper to access the Backend API User
object for the authenticated user.
A Route Handler can use the auth()
helper to return information about the user or their authentication state, or to control access to some or all of the Route Handler. The auth()
helper does require Middleware.
A Route Handler can use the auth()
helper to return information about the user or their authentication state, or to control access to some or all of the Route Handler. The auth()
helper does require Middleware.
In this example, the auth()
helper is used to validate an authenticated user and the currentUser()
helper is used to access the Backend User
object for the authenticated user.
For Next.js applications using the Pages Router, you can retrieve information about the user and their authentication state, or control access to some or all of your API routes by using the getAuth()
helper. The getAuth()
helper does require Middleware.
For Next.js applications using the Pages Router, you can retrieve information about the user and their authentication state, or control access to some or all of your API routes by using the getAuth()
helper. The getAuth()
helper does require Middleware.
In some cases, you may need the full User
object. For example, if you want to access the user's email address address or name, you can use the clerkClient
helper to get the full User
object.
You can access the active session and user data in your getServerSideProps
using the getAuth()
helper.
You can also access the full User
object before passing it to the page by using the clerkClient
helper.
Client-side
useAuth
The useAuth
hook is a convenient way to access the current auth state. This hook provides the minimal information needed for data-loading and helper methods to manage the current active session.
Feedback
Last updated on