Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating supabase middleware away from deprecated createServerClient method #368

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 11 additions & 38 deletions utils/supabase/middleware.ts
Original file line number Diff line number Diff line change
@@ -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) => {
Expand All @@ -14,46 +14,19 @@ 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 }) => request.cookies.set(name, value))
response = NextResponse.next({
request: {
headers: request.headers
}
});
response.cookies.set({
name,
value,
...options
});
request
})
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
}
);

Expand Down
25 changes: 8 additions & 17 deletions utils/supabase/server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createServerClient, type CookieOptions } from '@supabase/ssr';
import { createServerClient, type CookieMethodsServer } from '@supabase/ssr';
import { cookies } from 'next/headers';
import { Database } from '@/types_db';

Expand All @@ -15,29 +15,20 @@ export const createClient = () => {
// Define a cookies object with methods for interacting with the cookie store and pass it to the client
{
cookies: {
// The get method is used to retrieve a cookie by its name
get(name: string) {
return cookieStore.get(name)?.value;
// The getAll method is used to cookies by name
getAll() {
return cookieStore.getAll()
},
// The set method is used to set a cookie with a given name, value, and options
set(name: string, value: string, options: CookieOptions) {
// The setAll method is used to cookies with a given name, value, and options
setAll(cookiesToSet) {
try {
cookieStore.set({ name, value, ...options });
cookiesToSet.forEach(({ name, value }) => cookieStore.set(name, value))
} catch (error) {
// If the set method is called from a Server Component, an error may occur
// This can be ignored if there is middleware refreshing user sessions
}
},
// The remove method is used to delete a cookie by its name
remove(name: string, options: CookieOptions) {
try {
cookieStore.set({ name, value: '', ...options });
} catch (error) {
// If the remove method is called from a Server Component, an error may occur
// This can be ignored if there is middleware refreshing user sessions
}
}
}
} as CookieMethodsServer
}
);
};