Read Session & User 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.
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.
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
Server Side
API Routes
You can protect your API routes by using the auth
helper and retrieve data from your own systems.
1// app/api/route.ts23import type { NextApiRequest, NextApiResponse } from "next";4import { auth } from "@clerk/nextjs";56export default function handler(req: NextApiRequest, res: NextApiResponse) {7const { userId } = auth(req);8if (!userId) {9res.status(401).json({ error: "Unauthorized" });10return;11}12// retrieve data from your database13res.status(200).json({});14}
1// pages/api/auth.ts23import type { NextApiRequest, NextApiResponse } from "next";4import { getAuth } from "@clerk/nextjs";56export default function handler(req: NextApiRequest, res: NextApiResponse) {7const { userId } = getAuth(req);8if (!userId) {9res.status(401).json({ error: "Unauthorized" });10return;11}12// retrieve data from your database13res.status(200).json({});14}
In some cases, you may need the full user object, for example, if you want to access the user's email address or name. You can use the clerkClient
helper to get the full user object.
1// app/api/route.ts23import { auth, clerkClient } from "@clerk/nextjs";4import type { NextApiRequest, NextApiResponse } from "next";56export default async function handler(7req: NextApiRequest,8res: NextApiResponse9) {10const { userId } = auth(req);1112if (!userId) {13return res.status(401).json({ error: "Unauthorized" });14}1516const user = userId ? await clerkClient.users.getUser(userId) : null;1718// use the user object to decide what data to return1920return res.status(200).json({});21}
1// pages/api/auth.ts23import { getAuth, clerkClient } from "@clerk/nextjs";4import type { NextApiRequest, NextApiResponse } from "next";56export default async function handler(7req: NextApiRequest,8res: NextApiResponse9) {10const { userId } = getAuth(req);1112if (!userId) {13return res.status(401).json({ error: "Unauthorized" });14}1516const user = userId ? await clerkClient.users.getUser(userId) : null;1718// use the user object to decide what data to return1920return res.status(200).json({});21}
Server Side Rendering (Pages Router only)
You can access the active session and user data in your getServerSideProps
using the getAuth
helper.
Please note the addition of buildClerkProps in the return statement, which informs our React helpers of the authentication state during server-side rendering (like useAuth()
, <SignedIn>
, and <SignedOut>
).
pages/example.tsx1
You can also access the full user object before passing it to the page using the clerkClient
helper.
pages/example.tsx1