Unverified Commit 75958bbc authored by Jon Meyers's avatar Jon Meyers Committed by GitHub

Chore: simplify with-supabase example (#57562)

### What?

[1] Simplify example
[2] Refactor `delete` method to use `cookies.set`

### Why?

[1] Make it easier to follow
[2] Fix build errors

### How?

[1] Adding comments and abstracting code into helper functions
[2] Setting cookie to empty value when removed

---------
Co-authored-by: 's avatarLee Robinson <me@leerob.io>
parent e0cf4e2e
...@@ -10,6 +10,8 @@ export default async function Index() { ...@@ -10,6 +10,8 @@ export default async function Index() {
const cookieStore = cookies() const cookieStore = cookies()
const canInitSupabaseClient = () => { const canInitSupabaseClient = () => {
// This function is just for the interactive tutorial.
// Feel free to remove it once you have Supabase connected.
try { try {
createClient(cookieStore) createClient(cookieStore)
return true return true
......
...@@ -4,21 +4,24 @@ import Code from '@/components/Code' ...@@ -4,21 +4,24 @@ import Code from '@/components/Code'
const create = ` const create = `
create table notes ( create table notes (
id uuid default gen_random_uuid() primary key, id serial primary key,
title text title text
); );
insert into notes(title) insert into notes(title)
values('Today I connected Next.js to Supabase. It was awesome!'); values
('Today I created a Supabase project.'),
('I added some data and queried it from Next.js.'),
('It was awesome!');
`.trim() `.trim()
const server = ` const server = `
import { createClient } from '@/utils/supabase/server' import { createClient } from '@/utils/supabase/server'
import { cookies } from 'next/headers'
export const dynamic = 'force-dynamic'
export default async function Page() { export default async function Page() {
const supabase = createClient() const cookieStore = cookies()
const supabase = createClient(cookieStore)
const { data: notes } = await supabase.from('notes').select() const { data: notes } = await supabase.from('notes').select()
return <pre>{JSON.stringify(notes, null, 2)}</pre> return <pre>{JSON.stringify(notes, null, 2)}</pre>
...@@ -93,11 +96,15 @@ export default function SignUpUserSteps() { ...@@ -93,11 +96,15 @@ export default function SignUpUserSteps() {
<Step title="Query Supabase data from Next.js"> <Step title="Query Supabase data from Next.js">
<p> <p>
Create a Supabase client and query data from an Async Server To create a Supabase client and query data from an Async Server
Component. Component, create a new page.tsx file at{' '}
<span className="px-2 py-1 rounded-md bg-foreground/20 text-foreground/80">
/app/notes/page.tsx
</span>{' '}
and add the following.
</p> </p>
<Code code={server} /> <Code code={server} />
<p>Alternatively, you can use a client component.</p> <p>Alternatively, you can use a Client Component.</p>
<Code code={client} /> <Code code={client} />
</Step> </Step>
......
import { createServerClient, type CookieOptions } from '@supabase/ssr'
import { NextResponse, type NextRequest } from 'next/server' import { NextResponse, type NextRequest } from 'next/server'
import { createClient } from '@/utils/supabase/middleware'
export async function middleware(request: NextRequest) { export async function middleware(request: NextRequest) {
// Create an unmodified response
let response = NextResponse.next({
request: {
headers: request.headers,
},
})
try { try {
// Create a Supabase client configured to use cookies // This `try/catch` block is only here for the interactive tutorial.
const supabase = createServerClient( // Feel free to remove once you have Supabase connected.
process.env.NEXT_PUBLIC_SUPABASE_URL!, const { supabase, response } = createClient(request)
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
{
cookies: {
get(name: string) {
return request.cookies.get(name)?.value
},
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,
})
response = NextResponse.next({
request: {
headers: request.headers,
},
})
response.cookies.set({
name,
value,
...options,
})
},
remove(name, options) {
// If the cookie is removed, update the cookies for the request and response
request.cookies.delete({
name,
...options,
})
response = NextResponse.next({
request: {
headers: request.headers,
},
})
response.cookies.delete({
name,
...options,
})
},
},
}
)
// Refresh session if expired - required for Server Components // Refresh session if expired - required for Server Components
// https://supabase.com/docs/guides/auth/auth-helpers/nextjs#managing-session-with-middleware // https://supabase.com/docs/guides/auth/auth-helpers/nextjs#managing-session-with-middleware
await supabase.auth.getSession() await supabase.auth.getSession()
// If the session was refreshed, the request and response cookies will have been updated
// If the session was not refreshed, the request and response cookies will be unchanged
return response return response
} catch (e) { } catch (e) {
// If you are here, a Supabase client could not be created! // If you are here, a Supabase client could not be created!
// This is likely because you have not set up environment variables. // This is likely because you have not set up environment variables.
// TODO: Feel free to remove this `try catch` block once you have // Check out http://localhost:3000 for Next Steps.
// your Next.js app connected to your Supabase project. return NextResponse.next({
return response request: {
headers: request.headers,
},
})
} }
} }
import { createServerClient, type CookieOptions } from '@supabase/ssr'
import { type NextRequest, NextResponse } from 'next/server'
export const createClient = (request: NextRequest) => {
// Create an unmodified response
let response = NextResponse.next({
request: {
headers: request.headers,
},
})
const supabase = createServerClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
{
cookies: {
get(name: string) {
return request.cookies.get(name)?.value
},
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,
})
response = NextResponse.next({
request: {
headers: request.headers,
},
})
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,
})
},
},
}
)
return { supabase, response }
}
...@@ -21,7 +21,7 @@ export const createClient = (cookieStore: ReturnType<typeof cookies>) => { ...@@ -21,7 +21,7 @@ export const createClient = (cookieStore: ReturnType<typeof cookies>) => {
}, },
remove(name: string, options: CookieOptions) { remove(name: string, options: CookieOptions) {
try { try {
cookieStore.delete({ name, ...options }) cookieStore.set({ name, value: '', ...options })
} catch (error) { } catch (error) {
// The `delete` method was called from a Server Component. // The `delete` method was called from a Server Component.
// This can be ignored if you have middleware refreshing // This can be ignored if you have middleware refreshing
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment