Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

kaizen improvements to nextjs server auth guide (#21305)

Charis9c322b7f5827
diff --git a/apps/docs/content/guides/auth/server-side/nextjs.mdx b/apps/docs/content/guides/auth/server-side/nextjs.mdx
index f26d4afbebb..080dadd151e 100644
--- a/apps/docs/content/guides/auth/server-side/nextjs.mdx
+++ b/apps/docs/content/guides/auth/server-side/nextjs.mdx
@@ -123,9 +123,11 @@ export function createClient() {
```ts utils/supabase/server.ts
import { createServerClient, type CookieOptions } from '@supabase/ssr'
-import { type cookies } from 'next/headers'
+import { cookies } from 'next/headers'
+
+export function createClient() {
+ const cookieStore = cookies()
-export function createClient(cookieStore: ReturnType<typeof cookies>) {
return createServerClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
@@ -198,11 +200,34 @@ It's safe to trust `getUser()` because it sends a request to the Supabase Auth s
<StepHikeCompact.Code>
+<CH.Code className="min-h-[800px]">
+
```ts middleware.ts
+import { updateSession } from '@/utils/supabase/middleware'
+
+export async function middleware(request: NextRequest) {
+ return await updateSession(request)
+}
+
+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|.*\\.(?:svg|png|jpg|jpeg|gif|webp)$).*)',
+ ],
+}
+```
+
+```ts utils/supabase/middleware.ts
import { createServerClient, type CookieOptions } from '@supabase/ssr'
import { NextResponse, type NextRequest } from 'next/server'
-export async function middleware(request: NextRequest) {
+export async function updateSession(request: NextRequest) {
let response = NextResponse.next({
request: {
headers: request.headers,
@@ -259,21 +284,10 @@ export async function middleware(request: NextRequest) {
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|.*\\.(?:svg|png|jpg|jpeg|gif|webp)$).*)',
- ],
-}
```
+</CH.Code>
+
</StepHikeCompact.Code>
</StepHikeCompact.Step>
@@ -321,14 +335,12 @@ export default function LoginPage() {
'use server'
import { revalidatePath } from 'next/cache'
-import { cookies } from 'next/headers'
import { redirect } from 'next/navigation'
import { createClient } from '@/utils/supabase/server'
export async function login(formData: FormData) {
- const cookieStore = cookies()
- const supabase = createClient(cookieStore)
+ const supabase = createClient()
// type-casting here for convenience
// in practice, you should validate your inputs
@@ -348,8 +360,7 @@ export async function login(formData: FormData) {
}
export async function signup(formData: FormData) {
- const cookieStore = cookies()
- const supabase = createClient(cookieStore)
+ const supabase = createClient()
// type-casting here for convenience
// in practice, you should validate your inputs
@@ -409,14 +420,11 @@ Since this is a Router Handler, use the Supabase client from `@/utils/supabase/s
```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/server'
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
@@ -428,7 +436,7 @@ export async function GET(request: NextRequest) {
redirectTo.searchParams.delete('type')
if (token_hash && type) {
- const supabase = createClient(cookieStore)
+ const supabase = createClient()
const { error } = await supabase.auth.verifyOtp({
type,
@@ -477,14 +485,12 @@ It's safe to trust `getUser()` because it sends a request to the Supabase Auth s
<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 supabase = createClient()
const { data, error } = await supabase.auth.getUser()
if (error || !data?.user) {
@@ -512,29 +518,6 @@ You're done! To recap, you've successfully:
You can now use any Supabase features from your client or server code!
-## Troubleshooting
-
-<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">Getting the error <code>DynamicServerError: Dynamic server usage: cookies</code></span>}
- id="troubleshooting-error-dynamic-cookies"
- >
- This means Next.js has failed to detect that the route is dynamic until the `cookies` function is called. To fix this, make sure you call the `cookies` function directly in your Server Component, instead of abstracting it out.
-
- Alternatively, you can mark the route as dynamic using `export const dynamic = 'force-dynamic'`.
- </Accordion.Item>
-
- </div>
-</Accordion>
-
</TabPanel>
<TabPanel id="pages" label="Pages Router">