Skip to content

Commit

Permalink
Fix: Call cookies function from route to flag as dynamic (#57494)
Browse files Browse the repository at this point in the history
### What?

Move calling the `cookies` function up to the route

### Why?

This flags the route as `dynamic`, meaning we don't need to explicitly declare `export const dynamic = 'force-dynamic'`

### How?

Passing the returned `cookieStore` into Supabase helper functions, rather than the `cookies` function itself
  • Loading branch information
dijonmusters committed Oct 26, 2023
1 parent b546d8b commit b9dd6c4
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 25 deletions.
6 changes: 3 additions & 3 deletions examples/with-supabase/app/auth/callback/route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { createClient } from '@/utils/supabase/server'
import { NextResponse } from 'next/server'

export const dynamic = 'force-dynamic'
import { cookies } from 'next/headers'

export async function GET(request: Request) {
// The `/auth/callback` route is required for the server-side auth flow implemented
Expand All @@ -11,7 +10,8 @@ export async function GET(request: Request) {
const code = requestUrl.searchParams.get('code')

if (code) {
const supabase = createClient()
const cookieStore = cookies()
const supabase = createClient(cookieStore)
await supabase.auth.exchangeCodeForSession(code)
}

Expand Down
6 changes: 3 additions & 3 deletions examples/with-supabase/app/auth/sign-in/route.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { createClient } from '@/utils/supabase/server'
import { NextResponse } from 'next/server'

export const dynamic = 'force-dynamic'
import { cookies } from 'next/headers'

export async function POST(request: Request) {
const requestUrl = new URL(request.url)
const formData = await request.formData()
const email = String(formData.get('email'))
const password = String(formData.get('password'))
const supabase = createClient()
const cookieStore = cookies()
const supabase = createClient(cookieStore)

const { error } = await supabase.auth.signInWithPassword({
email,
Expand Down
6 changes: 3 additions & 3 deletions examples/with-supabase/app/auth/sign-out/route.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { createClient } from '@/utils/supabase/server'
import { NextResponse } from 'next/server'

export const dynamic = 'force-dynamic'
import { cookies } from 'next/headers'

export async function POST(request: Request) {
const requestUrl = new URL(request.url)
const supabase = createClient()
const cookieStore = cookies()
const supabase = createClient(cookieStore)

await supabase.auth.signOut()

Expand Down
6 changes: 3 additions & 3 deletions examples/with-supabase/app/auth/sign-up/route.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { createClient } from '@/utils/supabase/server'
import { NextResponse } from 'next/server'

export const dynamic = 'force-dynamic'
import { cookies } from 'next/headers'

export async function POST(request: Request) {
const requestUrl = new URL(request.url)
const formData = await request.formData()
const email = String(formData.get('email'))
const password = String(formData.get('password'))
const supabase = createClient()
const cookieStore = cookies()
const supabase = createClient(cookieStore)

const { error } = await supabase.auth.signUp({
email,
Expand Down
19 changes: 10 additions & 9 deletions examples/with-supabase/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@ import { createClient } from '@/utils/supabase/server'
import ConnectSupabaseSteps from '@/components/ConnectSupabaseSteps'
import SignUpUserSteps from '@/components/SignUpUserSteps'
import Header from '@/components/Header'
import { cookies } from 'next/headers'

export const dynamic = 'force-dynamic'
export default async function Index() {
const cookieStore = cookies()

const canInitSupabaseClient = () => {
try {
createClient()
return true
} catch (e) {
return false
const canInitSupabaseClient = () => {
try {
createClient(cookieStore)
return true
} catch (e) {
return false
}
}
}

export default async function Index() {
const isSupabaseConnected = canInitSupabaseClient()

return (
Expand Down
4 changes: 3 additions & 1 deletion examples/with-supabase/components/AuthButton.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { createClient } from '@/utils/supabase/server'
import Link from 'next/link'
import { cookies } from 'next/headers'

export default async function AuthButton() {
const supabase = createClient()
const cookieStore = cookies()
const supabase = createClient(cookieStore)

const {
data: { user },
Expand Down
4 changes: 1 addition & 3 deletions examples/with-supabase/utils/supabase/server.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { createServerClient, type CookieOptions } from '@supabase/ssr'
import { cookies } from 'next/headers'

export const createClient = () => {
const cookieStore = cookies()

export const createClient = (cookieStore: ReturnType<typeof cookies>) => {
return createServerClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
Expand Down

0 comments on commit b9dd6c4

Please sign in to comment.