The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
docs: add server-side auth guide for nextjs (#20559)
Charisfee05a03b9b1
diff --git a/apps/docs/components/Navigation/NavigationMenu/NavigationMenu.constants.ts b/apps/docs/components/Navigation/NavigationMenu/NavigationMenu.constants.tsindex ec26db6379d..f9d7a90dfba 100644--- a/apps/docs/components/Navigation/NavigationMenu/NavigationMenu.constants.ts+++ b/apps/docs/components/Navigation/NavigationMenu/NavigationMenu.constants.ts@@ -614,6 +614,10 @@ export const auth = {url: undefined,items: [{ name: 'Overview', url: '/guides/auth/server-side/overview' },+ {+ name: 'Next.js guide',+ url: '/guides/auth/server-side/nextjs',+ },{name: 'Creating a client',url: '/guides/auth/server-side/creating-a-client',diff --git a/apps/docs/pages/guides/auth/server-side/nextjs.mdx b/apps/docs/pages/guides/auth/server-side/nextjs.mdxnew file mode 100644index 00000000000..99247eaca9c--- /dev/null+++ b/apps/docs/pages/guides/auth/server-side/nextjs.mdx@@ -0,0 +1,987 @@+import Layout from '~/layouts/DefaultGuideLayout'+import StepHikeCompact from '~/components/StepHikeCompact'+import { Accordion } from 'ui'++export const meta = {+ title: 'Setting up Server-Side Auth for Next.js',+ sidebar_label: 'Next.js guide',+}++Next.js comes in two flavors: the [App Router](https://nextjs.org/docs/app) and the [Pages Router](https://nextjs.org/docs/pages). You can set up Server-Side Auth with either strategy. You can even use both in the same application.++<Tabs scrollable size="small" type="underlined" defaultActiveId="app" queryGroup="router">++<TabPanel id="app" label="App Router">++<StepHikeCompact>++<StepHikeCompact.Step step={1}>++<StepHikeCompact.Details title="Install Supabase packages">++Install the `@supabase/supabase-js` package and the helper `@supabase/ssr` package.++</StepHikeCompact.Details>++<StepHikeCompact.Code>++```sh+npm install @supabase/supabase-js @supabase/ssr+```++</StepHikeCompact.Code>++</StepHikeCompact.Step>++<StepHikeCompact.Step step={2}>++<StepHikeCompact.Details title="Set up environment variables">++Create a `.env.local` file in your project root directory.++Fill in your `NEXT_PUBLIC_SUPABASE_URL` and `NEXT_PUBLIC_SUPABASE_ANON_KEY`:++<ProjectConfigVariables variable="url" />+<ProjectConfigVariables variable="anonKey" />++</StepHikeCompact.Details>++<StepHikeCompact.Code>++```txt .env.local+NEXT_PUBLIC_SUPABASE_URL=<your_supabase_project_url>+NEXT_PUBLIC_SUPABASE_ANON_KEY=<your_supabase_anon_key>+```++</StepHikeCompact.Code>++</StepHikeCompact.Step>++<StepHikeCompact.Step step={3}>++<StepHikeCompact.Details title="Write utility functions to create Supabase clients">++To access Supabase from your Next.js app, you need 3 types of Supabase clients:++1. **Client Component client** - To access Supabase from Client Components, which run in the browser.+1. **Server Component client** - To access Supabase from Server Components, which run only on the server.+1. **Server Action client** - To access Supabase from Server Actions and Route Handlers.++Create a `utils/supabase` folder with a file for each type of client. Then copy the utility functions for each client type.++<Accordion+ type="default"+ openBehaviour="multiple"+ chevronAlign="right"+ justified+ size="medium"+ className="text-foreground-light mt-8 mb-6"+>+ <div className="border-b pb-3">+ <Accordion.Item+ header={<span className="text-foreground">Why do I need so many types of clients?</span>}+ id="nextjs-clients"+ >++ A Supabase client reads and sets cookies in order to access and update the user session. Depending on where the client is used, it needs to interact with cookies in a different way:++ - **Client Component** - Only reads cookies and doesn't set them. Accesses the cookies from browser storage. Behind the scenes, `createBrowserClient` reuses the same client instance if called multiple times, so don't worry about deduplicating the client yourself.+ - **Server Component** - Only reads cookies and doesn't set them. (This limitation is set by Next.js, which doesn't allow cookies to be updated from a Server Component.) Accesses the cookies via the Next.js `cookies()` API, which reads the cookies from the request. Server clients should always be created afresh each time, because they need the cookies from the request.+ - **Server Action or Route Handler** - Reads cookies and sets them. Accesses the cookies via the Next.js `cookies()` API, which reads the cookies from the request. Server Action clients should always be created afresh each time, because they need the cookies from the request.++ </Accordion.Item>++ </div>+ <div className="border-b mt-3 pb-3">+ <Accordion.Item+ header={<span className="text-foreground">What does the `cookies` object do?</span>}+ id="client-storage-cookies"+ >++ The cookies object lets the Supabase client know how to access the cookies, so it can read and write the user session data. To make `@supabase/ssr` framework-agnostic, the cookies methods aren't hard-coded. But you only need to set them up once. You can then reuse your utility functions whenever you need a Supabase client.++ </Accordion.Item>++ </div>+</Accordion>++</StepHikeCompact.Details>++<StepHikeCompact.Code>++<CH.Code className="min-h-[34rem]">++```ts utils/supabase/client.ts+import { createBrowserClient } from '@supabase/ssr'++export function createClient() {+ return createBrowserClient(+ process.env.NEXT_PUBLIC_SUPABASE_URL!,+ process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!+ )+}+```++```ts utils/supabase/server.ts+import { createServerClient } from '@supabase/ssr'+import { cookies } from 'next/headers'++export function createClient(cookieStore: ReturnType<typeof cookies>) {+ return createServerClient(+ process.env.NEXT_PUBLIC_SUPABASE_URL!,+ process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,+ {+ cookies: {+ get(name: string) {+ return cookieStore.get(name)?.value+ },+ },+ }+ )+}+```++```ts utils/supabase/actions.ts+import { type CookieOptions, createServerClient } from '@supabase/ssr'+import { cookies } from 'next/headers'++export function createClient(cookieStore: ReturnType<typeof cookies>) {+ return createServerClient(+ process.env.NEXT_PUBLIC_SUPABASE_URL!,+ process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,+ {+ cookies: {+ get(name: string) {+ return cookieStore.get(name)?.value+ },+ set(name: string, value: string, options: CookieOptions) {+ return cookieStore.set({ name, value, ...options })+ },+ remove(name: string, options: CookieOptions) {+ return cookieStore.set({ name, value: '', ...options })+ },+ },+ }+ )+}+```++</CH.Code>++</StepHikeCompact.Code>++</StepHikeCompact.Step>++<StepHikeCompact.Step step={4}>++<StepHikeCompact.Details title="Hook up middleware">++Create a `middleware.ts` file at the root of your project.++Since Server Components can't write cookies, you need middleware to refresh expired Auth tokens and store them. Copy the code to refresh the Auth token whenever it's expired.++Add a [matcher](https://nextjs.org/docs/app/building-your-application/routing/middleware#matching-paths) so the middleware doesn't run on routes that don't access Supabase.++<Admonition type="danger">++Be careful when protecting pages. The server gets the user session from the cookies, which can be spoofed by anyone.++Always use `supabase.auth.getUser()` to protect pages and user data.++_Never_ trust `supabase.auth.getSession()` inside server code such as middleware. It isn't guaranteed to revalidate the Auth token.++It's safe to trust `getUser()` because it sends a request to the Supabase Auth server every time to revalidate the Auth token.++</Admonition>++</StepHikeCompact.Details>++<StepHikeCompact.Code>++```ts middleware.ts+import { createServerClient, type CookieOptions } from '@supabase/ssr'+import { NextResponse, type NextRequest } from 'next/server'++export async function middleware(request: NextRequest) {+ let response = NextResponse.next({+ request: {+ headers: request.headers,+ },+ })++ const supabase = createServerClient(+ process.env.NEXT_PUBLIC_SUPABASE_URL!,+ process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,+ {+ cookies: {+ get(name: string) {+ return request.cookies.get(name)?.value+ },+ set(name: string, value: string, options: CookieOptions) {+ request.cookies.set({+ name,+ value,+ ...options,+ })+ response = NextResponse.next({+ request: {+ headers: request.headers,+ },+ })+ response.cookies.set({+ name,+ value,+ ...options,+ })+ },+ remove(name: string, options: CookieOptions) {+ request.cookies.set({+ name,+ value: '',+ ...options,+ })+ response = NextResponse.next({+ request: {+ headers: request.headers,+ },+ })+ response.cookies.set({+ name,+ value: '',+ ...options,+ })+ },+ },+ }+ )++ await supabase.auth.getUser()++ return response+}++export const config = {+ matcher: [+ /*+ * Match all request paths except for the ones starting with:+ * - _next/static (static files)+ * - _next/image (image optimization files)+ * - favicon.ico (favicon file)+ * Feel free to modify this pattern to include more paths.+ */+ '/((?!_next/static|_next/image|favicon.ico).*)',+ ],+}+```++</StepHikeCompact.Code>++</StepHikeCompact.Step>++<StepHikeCompact.Step step={5}>++<StepHikeCompact.Details title="Create a login page">++Create a login page for your app. Use a Server Action to call the Supabase signup function.++If you have email confirmations turned on (the default), a new user will receive a confirmation email after signing up. When they click the link, they are directed to the Auth callback route at `auth/confirm` with a secure code. A Route Handler at this route finishes the signup process by exchanging the code for an Auth token.++Since Supabase is being called from an Action and a Route Handler, use the client defined in `@/utils/supabase/actions.ts`.++</StepHikeCompact.Details>++<StepHikeCompact.Code>++<CH.Code className="min-h-[34rem]">++```ts app/login/page.tsx+import { login, signup } from './actions'++export default function LoginPage() {+ return (+ <form>+ <label htmlFor="email">Email:</label>+ <input id="email" name="email" type="email" required />+ <label htmlFor="password">Password:</label>+ <input id="password" name="password" type="password" required />+ <button formAction={login}>Log in</button>+ <button formAction={signup}>Sign up</button>+ </form>+ )+}+```++```ts app/login/actions.ts+'use server'++import { revalidatePath } from 'next/cache'+import { cookies } from 'next/headers'+import { redirect } from 'next/navigation'++import { createClient } from '@/utils/supabase/actions'++export async function login(formData: FormData) {+ const cookieStore = cookies()+ const supabase = createClient(cookieStore)++ // type-casting here for convenience+ // in practice, you should validate your inputs+ const data = {+ email: formData.get('email') as string,+ password: formData.get('password') as string,+ }++ const { error } = await supabase.auth.signInWithPassword(data)++ if (error) {+ redirect('/error')+ }++ revalidatePath('/', 'layout')+ redirect('/')+}++export async function signup(formData: FormData) {+ const cookieStore = cookies()+ const supabase = createClient(cookieStore)++ // type-casting here for convenience+ // in practice, you should validate your inputs+ const data = {+ email: formData.get('email') as string,+ password: formData.get('password') as string,+ }++ const { error } = await supabase.auth.signUp(data)++ if (error) {+ redirect('/error')+ }++ revalidatePath('/', 'layout')+ redirect('/')+}+```++```ts app/error/page.tsx+export default function ErrorPage() {+ return <p>Sorry, something went wrong</p>+}+```++```ts app/auth/confirm/route.ts+import { type EmailOtpType } from '@supabase/supabase-js'+import { cookies } from 'next/headers'+import { type NextRequest, NextResponse } from 'next/server'++import { createClient } from '@/utils/supabase/actions'++export async function GET(request: NextRequest) {+ const cookieStore = cookies()++ const { searchParams } = new URL(request.url)+ const token_hash = searchParams.get('token_hash')+ const type = searchParams.get('type') as EmailOtpType | null+ const next = searchParams.get('next') ?? '/'++ const redirectTo = request.nextUrl.clone()+ redirectTo.pathname = next+ redirectTo.searchParams.delete('token_hash')+ redirectTo.searchParams.delete('type')++ if (token_hash && type) {+ const supabase = createClient(cookieStore)++ const { error } = await supabase.auth.verifyOtp({+ type,+ token_hash,+ })+ if (!error) {+ redirectTo.searchParams.delete('next')+ return NextResponse.redirect(redirectTo)+ }+ }++ // return the user to an error page with some instructions+ redirectTo.pathname = '/error'+ return NextResponse.redirect(redirectTo)+}+```++</CH.Code>++</StepHikeCompact.Code>++</StepHikeCompact.Step>++<StepHikeCompact.Step step={6}>++<StepHikeCompact.Details title="Access user info from Server Component">++Server Components can read cookies, so you can get the Auth status and user info.++Since you're calling Supabase from a Server Component, use the client created in `@/utils/supabase/server.ts`.++Create a `private` page that users can only access if they're logged in. The page displays their email.++<Admonition type="danger">++Be careful when protecting pages. The server gets the user session from the cookies, which can be spoofed by anyone.++Always use `supabase.auth.getUser()` to protect pages and user data.++_Never_ trust `supabase.auth.getSession()` inside Server Components. It isn't guaranteed to revalidate the Auth token.++It's safe to trust `getUser()` because it sends a request to the Supabase Auth server every time to revalidate the Auth token.++</Admonition>++</StepHikeCompact.Details>++<StepHikeCompact.Code>++```ts app/private/page.tsx+import { cookies } from 'next/headers'+import { redirect } from 'next/navigation'++import { createClient } from '@/utils/supabase/server'++export default async function PrivatePage() {+ const cookieStore = cookies()+ const supabase = createClient(cookieStore)++ const { data, error } = await supabase.auth.getUser()+ if (error || !data?.user) {+ redirect('/')+ }++ return <p>Hello {data.user.email}</p>+}+```++</StepHikeCompact.Code>++</StepHikeCompact.Step>++</StepHikeCompact>++## Congratulations++You're done! To recap, you've successfully:++- Called Supabase from a Server Action+- Called Supabase from a Server Component+- Set up a Supabase client utility to call Supabase from a Client Component+- Set up middleware to automatically refresh the Supabase Auth session++You can now use any Supabase features from your client or server code!++## FAQ++<Accordion+ type="default"+ openBehaviour="multiple"+ chevronAlign="right"+ justified+ size="medium"+ className="text-foreground-light mt-8 mb-6"+>+ <div className="border-b pb-3">+ <Accordion.Item+ header={<span className="text-foreground">Which type of Supabase client should I use?</span>}+ id="choosing-client"+ >+ In this tutorial, you wrote functions to create 3 different types of Supabase client: client, server, and action. Choose which one to use based on where your code is located:++ | Code location | Type of Supabase client |+ | - | - |+ | Client Component | Client |+ | Server Component | Server |+ | Server Action | Action |+ | Route Handler | Action |++ </Accordion.Item>++ </div>+</Accordion>++</TabPanel>++<TabPanel id="pages" label="Pages Router">++<StepHikeCompact>++<StepHikeCompact.Step step={1}>++<StepHikeCompact.Details title="Install Supabase packages">++Install the `@supabase/supabase-js` package and the helper `@supabase/ssr` package.++</StepHikeCompact.Details>++<StepHikeCompact.Code>++```sh+npm install @supabase/supabase-js @supabase/ssr+```++</StepHikeCompact.Code>++</StepHikeCompact.Step>++<StepHikeCompact.Step step={2}>++<StepHikeCompact.Details title="Set up environment variables">++Create a `.env.local` file in your project root directory.++Fill in your `NEXT_PUBLIC_SUPABASE_URL` and `NEXT_PUBLIC_SUPABASE_ANON_KEY`:++<ProjectConfigVariables variable="url" />+<ProjectConfigVariables variable="anonKey" />++</StepHikeCompact.Details>++<StepHikeCompact.Code>++```txt .env.local+NEXT_PUBLIC_SUPABASE_URL=<your_supabase_project_url>+NEXT_PUBLIC_SUPABASE_ANON_KEY=<your_supabase_anon_key>+```++</StepHikeCompact.Code>++</StepHikeCompact.Step>++<StepHikeCompact.Step step={3}>++<StepHikeCompact.Details title="Write utility functions to create Supabase clients">++To access Supabase from your Next.js app, you need 4 types of Supabase clients:++1. **`getServerSideProps` client** - To access Supabase from `getServerSideProps`.+1. **`getStaticProps` client** - To access Supabase from `getStaticProps`.+1. **Component client** - To access Supabase from within components.+1. **API route client** - To access Supabase from API route handlers.++Create a `utils/supabase` folder with a file for each type of client. Then copy the utility functions for each client type.++<Accordion+ type="default"+ openBehaviour="multiple"+ chevronAlign="right"+ justified+ size="medium"+ className="text-foreground-light mt-8 mb-6"+>+ <div className="border-b pb-3">+ <Accordion.Item+ header={<span className="text-foreground">Why do I need so many types of clients?</span>}+ id="nextjs-clients"+ >++ A Supabase client reads and sets cookies in order to access and update the user session. Depending on where the client is used, it needs to interact with cookies in a different way:++ - **`getServerSideProps`** - Runs on the server. Reads cookies from the request, which is passed through from `GetServerSidePropsContext`.+ - **`getStaticProps`** - Runs at build time, where there is no user, session, or cookies.+ - **Component** - Runs on the client. Reads cookies from browser storage. Behind the scenes, `createBrowserClient` reuses the same client instance if called multiple times, so don't worry about deduplicating the client yourself.+ - **API route** - Runs on the server. Reads cookies from the request, which is passed through from `NextApiRequest`.++ </Accordion.Item>++ </div>+ <div className="border-b mt-3 pb-3">+ <Accordion.Item+ header={<span className="text-foreground">What does the `cookies` object do?</span>}+ id="client-storage-cookies"+ >++ The cookies object lets the Supabase client know how to access the cookies, so it can read and write the user session. To make `@supabase/ssr` framework-agnostic, the cookies methods aren't hard-coded. But you only need to set them up once. You can then reuse your utility functions whenever you need a Supabase client.++ </Accordion.Item>++ </div>+</Accordion>++</StepHikeCompact.Details>++<StepHikeCompact.Code>++<CH.Code>++```ts utils/supabase/server-props.ts+import { createServerClient, type CookieOptions, serialize } from '@supabase/ssr'+import { type GetServerSidePropsContext } from 'next'++export function createClient(context: GetServerSidePropsContext) {+ const supabase = createServerClient(+ process.env.NEXT_PUBLIC_SUPABASE_URL!,+ process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,+ {+ cookies: {+ get(name: string) {+ return context.req.cookies[name]+ },+ set(name: string, value: string, options: CookieOptions) {+ context.res.appendHeader('Set-Cookie', serialize(name, value, options))+ },+ remove(name: string, options: CookieOptions) {+ context.res.appendHeader('Set-Cookie', serialize(name, '', options))+ },+ },+ }+ )++ return supabase+}+```++```ts utils/supabase/static-props.ts+import { createClient as createClientPrimitive } from '@supabase/supabase-js'++export function createClient() {+ const supabase = createClientPrimitive(+ process.env.NEXT_PUBLIC_SUPABASE_URL!,+ process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!+ )++ return supabase+}+```++```ts utils/supabase/component.ts+import { createBrowserClient } from '@supabase/ssr'++export function createClient() {+ const supabase = createBrowserClient(+ process.env.NEXT_PUBLIC_SUPABASE_URL!,+ process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!+ )++ return supabase+}+```++```ts utils/supabase/api.ts+import { createServerClient, type CookieOptions, serialize } from '@supabase/ssr'+import { type NextApiRequest, type NextApiResponse } from 'next'++export default function createClient(req: NextApiRequest, res: NextApiResponse) {+ const supabase = createServerClient(+ process.env.NEXT_PUBLIC_SUPABASE_URL!,+ process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,+ {+ cookies: {+ get(name: string) {+ return req.cookies[name]+ },+ set(name: string, value: string, options: CookieOptions) {+ res.setHeader('Set-Cookie', serialize(name, value, options))+ },+ remove(name: string, options: CookieOptions) {+ res.setHeader('Set-Cookie', serialize(name, '', options))+ },+ },+ }+ )++ return supabase+}+```++</CH.Code>++</StepHikeCompact.Code>++</StepHikeCompact.Step>++<StepHikeCompact.Step step={4}>++<StepHikeCompact.Details title="Create a login page">++Create a login page for your app.++Since Supabase is being called from a component, use the client defined in `@/utils/supabase/component.ts`.++</StepHikeCompact.Details>++<StepHikeCompact.Code>++<CH.Code>++```ts pages/login.tsx+import { useRouter } from 'next/router'+import { useState } from 'react'++import { createClient } from '@/utils/supabase/component'++export default function LoginPage() {+ const router = useRouter()+ const supabase = createClient()++ const [email, setEmail] = useState('')+ const [password, setPassword] = useState('')++ async function logIn() {+ const { error } = await supabase.auth.signInWithPassword({ email, password })+ if (error) {+ console.error(error)+ }+ router.push('/')+ }++ async function signUp() {+ const { error } = await supabase.auth.signUp({ email, password })+ if (error) {+ console.error(error)+ }+ router.push('/')+ }++ return (+ <main>+ <form>+ <label htmlFor="email">Email:</label>+ <input id="email" type="email" value={email} onChange={(e) => setEmail(e.target.value)} />+ <label htmlFor="password">Password:</label>+ <input+ id="password"+ type="password"+ value={password}+ onChange={(e) => setPassword(e.target.value)}+ />+ <button type="button" onClick={logIn}>+ Log in+ </button>+ <button type="button" onClick={signUp}>+ Sign up+ </button>+ </form>+ </main>+ )+}+```++</CH.Code>++</StepHikeCompact.Code>++</StepHikeCompact.Step>++<StepHikeCompact.Step step={5}>++<StepHikeCompact.Details title="Change the Auth confirmation path">++If you have email confirmation turned on (the default), a new user will receive an email confirmation after signing up. By default, the link in this email goes to the path `auth/confirm`.++Change this to `api/auth/confirm`.++Go to the [Auth templates](https://supabase.com/dashboard/project/_/auth/templates) page in your dashboard. In each email template, change `{{ .SiteURL }}/auth/confirm` to `{{ .SiteURL }}/api/auth/confirm`.++</StepHikeCompact.Details>++</StepHikeCompact.Step>++<StepHikeCompact.Step step={6}>++<StepHikeCompact.Details title="Create a route handler for Auth confirmation">++Create an API route for `api/auth/confirm`. When a user clicks their confirmation email link, exchange their secure code for an Auth token.++Since this is an API route, use the Supabase client from `@/utils/supabase/api.ts`.++</StepHikeCompact.Details>++<StepHikeCompact.Code>++<CH.Code>++```ts pages/api/auth/confirm.tsx+import { type EmailOtpType } from '@supabase/supabase-js'+import type { NextApiRequest, NextApiResponse } from 'next'++import { createClient } from '@/utils/supabase/api'++function stringOrFirstString(item: string | string[] | undefined) {+ return Array.isArray(item) ? item[0] : item+}++export default async function handler(req: NextApiRequest, res: NextApiResponse) {+ if (req.method !== 'GET') {+ res.status(405).appendHeader('Allow', 'GET').end()+ return+ }++ const queryParams = req.query+ const token_hash = stringOrFirstString(queryParams.token_hash)+ const type = stringOrFirstString(queryParams.type)++ let next = '/error'++ if (token_hash && type) {+ const supabase = createClient(req, res)+ const { error } = await supabase.auth.verifyOtp({+ type: type as EmailOtpType,+ token_hash,+ })+ if (error) {+ console.error(error)+ } else {+ next = stringOrFirstString(queryParams.next) || '/'+ }+ }++ res.redirect(next)+}+```++```tsx pages/error.tsx+export default function ErrorPage() {+ return <p>Sorry, something went wrong</p>+}+```++</CH.Code>++</StepHikeCompact.Code>++</StepHikeCompact.Step>++<StepHikeCompact.Step step={7}>++<StepHikeCompact.Details title="Make an authenticated-only page using `getServerSideProps`">++If you use dynamic server-side rendering, you can serve a page to authenticated users only by checking for the user data in `getServerSideProps`. Unauthenticated users will be redirected to the home page.++Since you're calling Supabse from `getServerSideProps`, use the client from `@/utils/supabase/server-props.ts`.++<Admonition type="danger">++Be careful when protecting pages. The server gets the user session from the cookies, which can be spoofed by anyone.++Always use `supabase.auth.getUser()` to protect pages and user data.++_Never_ trust `supabase.auth.getSession()` inside server code. It isn't guaranteed to revalidate the Auth token.++It's safe to trust `getUser()` because it sends a request to the Supabase Auth server every time to revalidate the Auth token.++</Admonition>++</StepHikeCompact.Details>++<StepHikeCompact.Code>++```ts pages/private.tsx+import type { User } from '@supabase/supabase-js'+import type { GetServerSidePropsContext } from 'next'++import { createClient } from '@/utils/supabase/server-props'++export default function PrivatePage({ user }: { user: User }) {+ return <h1>Hello, {user.email || 'user'}!</h1>+}++export async function getServerSideProps(context: GetServerSidePropsContext) {+ const supabase = createClient(context)++ const { data, error } = await supabase.auth.getUser()++ if (error || !data) {+ return {+ redirect: {+ destination: '/',+ permanent: false,+ },+ }+ }++ return {+ props: {+ user: data.user,+ },+ }+}+```++</StepHikeCompact.Code>++</StepHikeCompact.Step>++<StepHikeCompact.Step step={8}>++<StepHikeCompact.Details title="Fetch static data using `getStaticProps`">++You can also fetch static data at build time using Supabase. Note that there's no session or user at build time, so the data will be the same for everyone who sees the page.++Add some countries data to your database by running the [Countries Quickstart](https://supabase.com/dashboard/project/_/sql/quickstarts) in the dashboard.++Then fetch the countries data using `getStaticProps` with the client from `@/utils/supabase/static-props.ts`.++</StepHikeCompact.Details>++<StepHikeCompact.Code>++```ts pages/public.tsx+import { createClient } from '@/utils/supabase/static-props'++export default function PublicPage({ data }: { data?: any[] }) {+ return <pre>{data && JSON.stringify(data, null, 2)}</pre>+}++export async function getStaticProps() {+ const supabase = createClient()++ const { data, error } = await supabase.from('countries').select()++ if (error || !data) {+ return { props: {} }+ }++ return { props: { data } }+}+```++</StepHikeCompact.Code>++</StepHikeCompact.Step>++</StepHikeCompact>++## Congratulations++You're done! To recap, you've successfully:++- Called Supabase from a component+- Called Supabase from an API route+- Called Supabase from `getServerSideProps`+- Called Supabase from `getStaticProps`++You can now use any Supabase features from your client or server code!++</TabPanel>++<TabPanel id="hybrid" label="Hybrid router strategies">++You can use both the App and Pages Routers together.++Follow the instructions for both the App and Pages Routers. Whenever you need to connect to Supabase, import the `createClient` utility that you need:++| Router | Code location | Which `createClient` to use |+| ------------ | ------------------------------ | --------------------------- |+| App Router | Server Component | `server.ts` |+| | Client Component | `client.ts` |+| | Server Action or Route Handler | `actions.ts` |+| Pages Router | `getServerSideProps` | `server-props.ts` |+| | `getStaticProps` | `static-props.ts` |+| | Component | `component.ts` |+| | API route | `api.ts` |++Remember to create the `middleware.ts` file for the App Router so the session refreshes for App Router pages.++</TabPanel>++</Tabs>++export const Page = ({ children }) => <Layout meta={meta} children={children} hideToc={true} />++export default Pagediff --git a/apps/docs/pages/guides/auth/server-side/overview.mdx b/apps/docs/pages/guides/auth/server-side/overview.mdxindex 18e4c186f9a..9324f8ebdab 100644--- a/apps/docs/pages/guides/auth/server-side/overview.mdx+++ b/apps/docs/pages/guides/auth/server-side/overview.mdx@@ -11,11 +11,11 @@ When using Supabase with server-side languages and frameworks - such as Next.js,## Framework quickstarts-<div className="grid lg:grid-cols-12 gap-6 not-prose">+<div className="grid cols-[repeat(auto-fit,min-max(250px,1fr)] gap-6 mb-6 not-prose">{quickstarts.map((item) => {return (- <Link href={`${item.href}`} key={item.title} passHref className={'col-span-4'}>- <GlassPanel title={item.title} span="col-span-6" background={false} icon={item.icon}>+ <Link href={`${item.href}`} key={item.title} passHref>+ <GlassPanel title={item.title} background={false} icon={item.icon}>{item.description}</GlassPanel></Link>@@ -39,7 +39,7 @@ If you're currently using the [Auth Helpers package](https://github.com/supabaseexport const quickstarts = [{title: 'Next.js',- href: '/guides/auth/quickstarts/nextjs',+ href: '/guides/auth/server-side/nextjs',description:'Automatically configure Supabase to use cookies, making your user and their session available on the client and server.',icon: '/docs/img/icons/nextjs-icon',