rsnext/examples/with-supabase/middleware.ts
Jon Meyers 75958bbc46
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: Lee Robinson <me@leerob.io>
2023-10-31 18:33:55 -05:00

25 lines
913 B
TypeScript

import { NextResponse, type NextRequest } from 'next/server'
import { createClient } from '@/utils/supabase/middleware'
export async function middleware(request: NextRequest) {
try {
// This `try/catch` block is only here for the interactive tutorial.
// Feel free to remove once you have Supabase connected.
const { supabase, response } = createClient(request)
// Refresh session if expired - required for Server Components
// https://supabase.com/docs/guides/auth/auth-helpers/nextjs#managing-session-with-middleware
await supabase.auth.getSession()
return response
} catch (e) {
// If you are here, a Supabase client could not be created!
// This is likely because you have not set up environment variables.
// Check out http://localhost:3000 for Next Steps.
return NextResponse.next({
request: {
headers: request.headers,
},
})
}
}