56 lines
1.4 KiB
TypeScript
56 lines
1.4 KiB
TypeScript
import { updateSession } from '@/lib/supabase/proxy'
|
|
import { createServerClient } from '@supabase/ssr'
|
|
import { NextResponse, type NextRequest } from 'next/server'
|
|
|
|
const PUBLIC_ROUTES = ['/', '/login', '/signup', '/auth']
|
|
|
|
export async function middleware(request: NextRequest) {
|
|
const { pathname } = request.nextUrl
|
|
|
|
// Always update session to refresh cookies
|
|
const response = await updateSession(request)
|
|
|
|
// Allow public routes through without auth check
|
|
const isPublic =
|
|
pathname === '/' ||
|
|
PUBLIC_ROUTES.some(r => pathname === r || pathname.startsWith(r + '/'))
|
|
|
|
if (isPublic) {
|
|
return response
|
|
}
|
|
|
|
// For protected routes, check auth
|
|
const supabase = createServerClient(
|
|
process.env.NEXT_PUBLIC_SUPABASE_URL!,
|
|
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
|
|
{
|
|
cookies: {
|
|
getAll() {
|
|
return request.cookies.getAll()
|
|
},
|
|
setAll(cookiesToSet) {
|
|
cookiesToSet.forEach(({ name, value, options }) =>
|
|
response.cookies.set(name, value, options)
|
|
)
|
|
},
|
|
},
|
|
}
|
|
)
|
|
|
|
const {
|
|
data: { user },
|
|
} = await supabase.auth.getUser()
|
|
|
|
if (!user) {
|
|
return NextResponse.redirect(new URL('/login', request.url))
|
|
}
|
|
|
|
return response
|
|
}
|
|
|
|
export const config = {
|
|
matcher: [
|
|
'/((?!_next/static|_next/image|favicon.ico|.*\\.(?:svg|png|jpg|jpeg|gif|webp)$).*)',
|
|
],
|
|
}
|