45 lines
1.3 KiB
TypeScript
45 lines
1.3 KiB
TypeScript
import { createServerClient } from '@supabase/ssr'
|
|
import { NextResponse, NextRequest } from 'next/server'
|
|
|
|
export async function proxy(req: NextRequest) {
|
|
const res = NextResponse.next()
|
|
|
|
const supabase = createServerClient(
|
|
process.env.NEXT_PUBLIC_SUPABASE_URL!,
|
|
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
|
|
{
|
|
cookies: {
|
|
getAll() {
|
|
return req.cookies.getAll()
|
|
},
|
|
setAll(cookiesToSet) {
|
|
cookiesToSet.forEach(({ name, value, options }) => {
|
|
res.cookies.set(name, value, options)
|
|
})
|
|
},
|
|
},
|
|
}
|
|
)
|
|
|
|
// Refresh session if expired
|
|
const { data: { user } } = await supabase.auth.getUser()
|
|
|
|
// Protect API routes (optional - you can make some routes public)
|
|
if (req.nextUrl.pathname.startsWith('/api/')) {
|
|
const publicRoutes = ['/api/stream/status', '/api/streams', '/api/channels']
|
|
const isPublicRoute = publicRoutes.some(route => req.nextUrl.pathname.startsWith(route))
|
|
|
|
if (!isPublicRoute && !user) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
}
|
|
|
|
return res
|
|
}
|
|
|
|
export const config = {
|
|
matcher: [
|
|
'/((?!_next|[^?]*\\.(?:html?|css|js(?!on)|jpe?g|webp|gif|svg|ttf|woff2?|ico|csv|docx?|xlsx?|zip|webmanifest)).*)',
|
|
'/(api|trpc)(.*)',
|
|
],
|
|
}
|