Pages (React)
The most common way developers protect pages in Next.js is using our Control Components.
-
<SignedIn/>
: Renders its children only when a user is signed in -
<SignedOut/>
: Renders its children only when there's no active user
The following example shows you how to compose control components in _app at the root of your /pages directory.
You will also need to go to the dashboard and update the sign-in and sign-up paths to /sign-in
and /sign-up
.
1import { ClerkProvider, SignedIn, SignedOut, RedirectToSignIn } from '@clerk/nextjs';2import { useRouter } from 'next/router';34// List pages you want to be publicly accessible, or leave empty if5// every page requires authentication. Use this naming strategy:6// "/" for pages/index.js7// "/foo" for pages/foo/index.js8// "/foo/bar" for pages/foo/bar.js9// "/foo/[...bar]" for pages/foo/[...bar].js10const publicPages = ["/sign-in/[[...index]]", "/sign-up/[[...index]]"];1112function MyApp({ Component, pageProps }) {13// Get the pathname14const { pathname } = useRouter();1516// Check if the current route matches a public page17const isPublicPage = publicPages.includes(pathname);1819// If the current route is listed as public, render it directly20// Otherwise, use Clerk to require authentication21return (22<ClerkProvider {...pageProps}>23{isPublicPage ? (24<Component {...pageProps} />25) : (26<>27<SignedIn>28<Component {...pageProps} />29</SignedIn>30<SignedOut>31<RedirectToSignIn />32</SignedOut>33</>34)}35</ClerkProvider>36);37}3839export default MyApp;
1import {2ClerkProvider,3SignedIn,4SignedOut,5RedirectToSignIn,6} from "@clerk/nextjs";7import { AppProps } from "next/app";8import { useRouter } from "next/router";910// List pages you want to be publicly accessible, or leave empty if11// every page requires authentication. Use this naming strategy:12// "/" for pages/index.js13// "/foo" for pages/foo/index.js14// "/foo/bar" for pages/foo/bar.js15// "/foo/[...bar]" for pages/foo/[...bar].js16const publicPages = ["/sign-in/[[...index]]", "/sign-up/[[...index]]"];1718function MyApp({ Component, pageProps } : AppProps) {19// Get the pathname20const { pathname } = useRouter();2122// Check if the current route matches a public page23const isPublicPage = publicPages.includes(pathname);2425// If the current route is listed as public, render it directly26// Otherwise, use Clerk to require authentication27return (28<ClerkProvider {...pageProps}>29{isPublicPage ? (30<Component {...pageProps} />31) : (32<>33<SignedIn>34<Component {...pageProps} />35</SignedIn>36<SignedOut>37<RedirectToSignIn />38</SignedOut>39</>40)}41</ClerkProvider>42);43}4445export default MyApp;
Accessing auth state
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.
1import { useAuth } from '@clerk/nextjs';2import { supabase } from './supabaseClient';34function SupabasePage() {5const { getToken, isLoaded, isSignedIn } = useAuth();67if (!isLoaded || !isSignedIn) {8// You can handle the loading or signed state separately9return null;10}1112const fetchDataFromSupabase = async () => {13const token = await getToken({ template: 'supabase' });14supabase(token)15const { data } = await supabase.from('your-table').select("*");16return data;17}1819return <div>...</div>;20}2122export default SupabasePage;
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.