Read User and Session Data
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. We have included examples of how to use these helpers in both the client and server side to get you started.
Server Side
App Router
auth()
The auth()
helper provides lightweight access to the current auth state, this is great for managing state and loading data from external sources.
1import { auth } from '@clerk/nextjs';23export default function Home() {4const { userId } = auth();5return <div>User Id: {userId}</div>;6}7
1import { auth } from "@clerk/nextjs";2import { NextResponse } from 'next/server';34export async function GET(request: Request) {5const { userId } = auth()6return NextResponse.json({ data });7}
1'use server'2import { auth } from '@clerk/nextjs'3export async function getUserId() {4const userid = auth().userId5console.log(userid)6}7
For in depth examples of data fetching check out our documentation on router handlers and server actions.
currentUser()
The `currentUser()` helper retrieves the user object for the current user allowing you to enrich your application with user data.
1import { currentUser } from '@clerk/nextjs';23export default async function Home() {4const user = await currentUser();5if (!user) return <div>Not logged in</div>;6return <div>Hello {user?.firstName}</div>;7}8
1import { currentUser } from "@clerk/nextjs";2import { NextResponse } from 'next/server';34export async function GET(request: Request) {5const user = await currentUser()6return NextResponse.json({ user });7}
1'use server'2import { currentUser } from '@clerk/nextjs'3export async function getUser() {4const user = await currentUser()5if(!user) return null6console.log(user)7}8
Pages Router
`getAuth`
The getAuth()
helper retrieves the authentication state allowing you to protect your API routes or gather relevant data.
1import { getAuth } from "@clerk/nextjs/server";2import type { NextApiRequest, NextApiResponse } from "next";34export default async function handler(5req: NextApiRequest,6res: NextApiResponse7) {8const { userId } = getAuth(req);9// Load any data your application needs for the API route10return res.status(200).json({ userId: userId });11}
1import { getAuth, buildClerkProps } from "@clerk/nextjs/server";2import { GetServerSideProps } from "next";34export const getServerSideProps: GetServerSideProps = async (ctx) => {5const { userId } = getAuth(ctx.req);67if (!userId) {8// handle user is not logged in.9}1011// Load any data your application needs for the page using the userId12return { props: { ...buildClerkProps(ctx.req) } };13};
`clerkClient`
Our `clerkClient` helper allows you to directly interact with the Clerk API, this is useful for updating data or retrieving the full User object.
1import { getAuth, clerkClient } from "@clerk/nextjs/server";2import type { NextApiRequest, NextApiResponse } from "next";34export default async function handler(5req: NextApiRequest,6res: NextApiResponse7) {8const { userId } = getAuth(req);910if (!userId) {11return res.status(401).json({ error: "Unauthorized" });12}1314const user = userId ? await clerkClient.users.getUser(userId) : null;1516// use the user object to decide what data to return1718return res.status(200).json({});19}
1import { clerkClient, getAuth, buildClerkProps } from "@clerk/nextjs/server";2import { GetServerSideProps } from "next";34export const getServerSideProps: GetServerSideProps = async (ctx) => {5const { userId } = getAuth(ctx.req);67const user = userId ? await clerkClient.users.getUser(userId) : undefined;89return { props: { ...buildClerkProps(ctx.req, { user }) } };10};
For a full list of options check out our documentation on accessing the API
Client Side
These client side hooks within both the App Router and the Pages Router.
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.
app/page.tsx or pages/example.tsx1
useUser
The useUser
hook is a convenient way to access the current user data where you need it. This hook provides the user data and helper methods to manage the current active session.
app/page.tsx or pages/example.tsx1