61 lines
1.7 KiB
TypeScript
61 lines
1.7 KiB
TypeScript
import { createServerClient } from '@supabase/ssr'
|
|
import type { CookieOptions } from '@supabase/ssr'
|
|
import type { NextRequest } from 'next/server'
|
|
import { NextResponse } from 'next/server'
|
|
|
|
export async function middleware(request: NextRequest) {
|
|
const response = NextResponse.next()
|
|
|
|
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) {
|
|
response.cookies.set({
|
|
name,
|
|
value,
|
|
path: options?.path ?? '/',
|
|
maxAge: options?.maxAge ?? 0,
|
|
domain: options?.domain,
|
|
secure: options?.secure,
|
|
httpOnly: options?.httpOnly,
|
|
sameSite: options?.sameSite,
|
|
})
|
|
},
|
|
remove(name: string, options: CookieOptions) {
|
|
response.cookies.set({
|
|
name,
|
|
value: '',
|
|
path: options?.path ?? '/',
|
|
maxAge: 0,
|
|
domain: options?.domain,
|
|
secure: options?.secure,
|
|
httpOnly: options?.httpOnly,
|
|
sameSite: options?.sameSite,
|
|
})
|
|
},
|
|
},
|
|
}
|
|
)
|
|
|
|
await supabase.auth.getUser()
|
|
|
|
return response
|
|
}
|
|
|
|
export const config = {
|
|
matcher: [
|
|
/*
|
|
* Match all request paths except for the ones starting with:
|
|
* - _next/static (static files)
|
|
* - _next/image (image optimization files)
|
|
* - favicon.ico (favicon file)
|
|
* Feel free to modify this pattern to include more paths.
|
|
*/
|
|
'/((?!_next/static|_next/image|favicon.ico).*)',
|
|
],
|
|
}
|