SupabaseS
Supabase2w ago
j

Supabase Auth: why set supabaseResponse in setAll() again?

Hi there! I'm following https://supabase.com/docs/guides/auth/server-side/creating-a-client which has this code:
export async function updateSession(request: NextRequest) {
  let supabaseResponse = NextResponse.next({request})
  // With Fluid compute, don't put this client in a global environment
  // variable. Always create a new one on each request.
  const supabase = createServerClient(
    process.env.NEXT_PUBLIC_SUPABASE_URL!,
    process.env.NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY!,
    {
      cookies: {
        getAll() {
          return request.cookies.getAll()
        },
        setAll(cookiesToSet) {
          cookiesToSet.forEach(({ name, value }) => request.cookies.set(name, value))
          supabaseResponse = NextResponse.next({request})
          cookiesToSet.forEach(({ name, value, options }) => supabaseResponse.cookies.set(name, value, options))
        },
      },
    }
  )


A few questions:
  1. Why do we need to do supabaseResponse = NextResponse.next({request}) within setAll()? I removed it and it seems like everything is working okay. Did I miss something in my testing?
  2. Does any of this code change if I write it directly in my apps/web/src/middleware.ts file? What I found is that the first line, let supabaseResponse = NextResponse.next({request}), also seems to work as just let supabaseResponse = NextResponse.next() if I write this code in apps/web/src/middleware.ts. Is that unexpected to you?
Thanks so much!
Was this page helpful?