import { createServerClient } from '@supabase/ssr'
import { NextResponse, type NextRequest } from 'next/server'
export async function middleware(request: NextRequest) {
// Validate required Supabase environment variables
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL
const supabaseAnonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY
if (!supabaseUrl || !supabaseAnonKey) {
console.error('Missing required Supabase environment variables')
return new NextResponse(
JSON.stringify({
error: 'Configuration Error',
message: 'Missing required Supabase environment variables',
}),
{
status: 500,
headers: { 'content-type': 'application/json' },
}
)
}
let supabaseResponse = NextResponse.next({ request })
const supabase = createServerClient(
supabaseUrl,
supabaseAnonKey,
{
cookies: {
getAll() {
return request.cookies.getAll()
},
setAll(cookiesToSet) {
cookiesToSet.forEach(({ name, value }) =>
request.cookies.set(name, value)
)
supabaseResponse = NextResponse.next({ request })
cookiesToSet.forEach(({ name, value, options }) =>
supabaseResponse.cookies.set(name, value, options)
)
},
},
}
)
const {
data: { user },
} = await supabase.auth.getUser()
const isAuthPage = request.nextUrl.pathname.startsWith('/login') ||
request.nextUrl.pathname.startsWith('/signup')
const isRootPage = request.nextUrl.pathname === '/'
// Redirect authenticated users from landing page to dashboard
if (user && isRootPage) {
return NextResponse.redirect(new URL('/dashboard', request.url))
}
// Redirect unauthenticated users from auth pages after login attempt
if (user && isAuthPage) {
return NextResponse.redirect(new URL('/dashboard', request.url))
}
// Redirect unauthenticated users trying to access dashboard
if (!user && request.nextUrl.pathname.startsWith('/dashboard')) {
return NextResponse.redirect(new URL('/login', request.url))
}
return supabaseResponse
}
export const config = {
matcher: [
'/',
'/dashboard/:path*',
'/login',
'/signup',
],
}