From 55c654f1e0601ec092b6eaeeafeda7f715ca7910 Mon Sep 17 00:00:00 2001 From: Kyle Thornton Date: Fri, 23 Aug 2024 13:11:48 -0700 Subject: [PATCH] Updating supabase middleware away from deprecated createServerClient method --- utils/supabase/middleware.ts | 49 +++++++++++------------------------- 1 file changed, 14 insertions(+), 35 deletions(-) diff --git a/utils/supabase/middleware.ts b/utils/supabase/middleware.ts index 32f1b3c5..9c2105cb 100644 --- a/utils/supabase/middleware.ts +++ b/utils/supabase/middleware.ts @@ -1,4 +1,4 @@ -import { createServerClient, type CookieOptions } from '@supabase/ssr'; +import { createServerClient, type CookieMethodsServer } from '@supabase/ssr'; import { type NextRequest, NextResponse } from 'next/server'; export const createClient = (request: NextRequest) => { @@ -14,46 +14,25 @@ export const createClient = (request: NextRequest) => { process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!, { cookies: { - get(name: string) { - return request.cookies.get(name)?.value; + getAll() { + return request.cookies.getAll() }, - set(name: string, value: string, options: CookieOptions) { - // If the cookie is updated, update the cookies for the request and response - request.cookies.set({ - name, - value, - ...options - }); + setAll(cookiesToSet) { + cookiesToSet.forEach(({ name, value, options }) => request.cookies.set(name, value)) response = NextResponse.next({ request: { headers: request.headers } - }); - response.cookies.set({ - name, - value, - ...options - }); + }) + cookiesToSet.forEach(({ name, value, options }) => + response.cookies.set({ + name, + value, + ...options + }) + ) }, - remove(name: string, options: CookieOptions) { - // If the cookie is removed, update the cookies for the request and response - request.cookies.set({ - name, - value: '', - ...options - }); - response = NextResponse.next({ - request: { - headers: request.headers - } - }); - response.cookies.set({ - name, - value: '', - ...options - }); - } - } + } as CookieMethodsServer } );