Pages (getServerSideProps)
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>
).
Optionally, use the Preload User example to make user profile data available during server-side rendering.
1import { getAuth, buildClerkProps } from "@clerk/nextjs/server";2import { GetServerSideProps } from 'next'34export const getServerSideProps: GetServerSideProps = async ctx => {5const { userId} = getAuth(ctx.req)6if (!userId) {7// handle user is not logged in.8}9return { props: { ...buildClerkProps(ctx.req) } }10}
1import { getAuth, buildClerkProps } from "@clerk/nextjs/server";23export const getServerSideProps = ({ req }) => {4const { userId } = getAuth(req);5// ...6return { props: { ...buildClerkProps(req) } };7};
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}
1import { clerkClient, getAuth, buildClerkProps } from "@clerk/nextjs/server";23export const getServerSideProps = async ({ req }) => {4const { userId } = getAuth(req);56const user = userId ? await clerkClient.users.getUser(userId) : undefined;7// ...8return { props: { ...buildClerkProps(req, { user }) } };9};
Before Next.js 12.2
1import { withServerSideAuth } from "@clerk/nextjs/ssr";2import type { GetServerSideProps } from "next";34export const getServerSideProps: GetServerSideProps = withServerSideAuth(({ req, resolvedUrl }) => {5const { sessionId } = req.auth;67if (!sessionId) {8return {9redirect: {10destination: "/sign-in?redirect_url=" + resolvedUrl,11permanent: false12}13};14}1516return { props: {} };17});
1import { withServerSideAuth } from "@clerk/nextjs/ssr";23export const getServerSideProps = withServerSideAuth(({ req, resolvedUrl }) => {4const { sessionId } = req.auth;56if (!sessionId) {7return {8redirect: {9destination: "/sign-in?redirect_url=" + resolvedUrl,10permanent: false11}12};13}1415return { props: {} };16});
1import { withServerSideAuth } from "@clerk/nextjs/ssr";2import type { GetServerSideProps } from "next";34export const getServerSideProps: GetServerSideProps = withServerSideAuth({ loadUser: true });56// or, if you also need a custom getServerSideProps handler:7export const getServerSideProps: GetServerSideProps = withServerSideAuth(8({ req }) => {9const { userId } = req.auth;10// fetch data11return { props: { yourData: "here" } };12},13{ loadUser: true }14);
1import { withServerSideAuth } from "@clerk/nextjs/ssr";23export const getServerSideProps = withServerSideAuth({ loadUser: true });45// or, if you also need a custom getServerSideProps handler:6export const getServerSideProps = withServerSideAuth(7({ req }) => {8const { userId } = req.auth;9// fetch data10return { props: { yourData: "here" } };11},12{ loadUser: true }13);
Example Response
{sessionId: 'sess_2GaMqUCB3Sc1WNAkWuNzsnYVVEy',userId: 'user_2F2u1wtUyUlxKgFkKqtJNtpJJWj',orgId: null,getToken: [AsyncFunction (anonymous)],claims: {azp: 'http://localhost:3000',exp: 1666622607,iat: 1666622547,iss: 'https://clerk.quiet.muskox-85.lcl.dev',nbf: 1666622537,sid: 'sess_2GaMqUCB3Sc1WNAkWuNzsnYVVEy',sub: 'user_2F2u1wtUyUlxKgFkKqtJNtpJJWj'}}
More detailed information about the fields in this object can be found in the Authentication Object documentation.