import { createServerClient } from "@supabase/ssr"; import { NextRequest, NextResponse } from "next/server"; export async function updateSession(request: NextRequest) { let supabaseResponse = NextResponse.next({ request, }); const supabase = createServerClient( process.env.NEXT_PUBLIC_SUPABASE_URL!, process.env.NEXT_PUBLIC_SUPABASE_ANON_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), ); }, }, }, ); // IMPORTANT: Avoid writing any logic between createServerClient and // the line above as this could lead to a race condition where // cookies are set before the Supabase client is created. // This will refresh session if expired - required for Server Components // https://supabase.com/docs/guides/auth/server-side/nextjs const { data: { user }, } = await supabase.auth.getUser(); // IMPORTANT: You *must* return the supabaseResponse object as it is. // If you are creating a new response object with NextResponse.next() make // sure to: // 1. Pass the request in the first argument // 2. Pass the supabaseResponse cookies in the second argument return supabaseResponse; }