(request: NextRequest)
| 2 | import { createSupabaseServerClient } from '@/utils/supabase-server'; |
| 3 | |
| 4 | export async function middleware(request: NextRequest) { |
| 5 | const path = request.nextUrl.pathname; |
| 6 | |
| 7 | // Create Supabase client |
| 8 | const { supabase, response } = createSupabaseServerClient(request); |
| 9 | |
| 10 | // Get the session |
| 11 | const { data: { session } } = await supabase.auth.getSession(); |
| 12 | |
| 13 | // Define protected and public paths |
| 14 | const isAuthRoute = path === '/'; |
| 15 | const isProtectedRoute = path !== '/'; |
| 16 | |
| 17 | // If on login page and authenticated, redirect to dashboard |
| 18 | if (isAuthRoute && session) { |
| 19 | const redirectResponse = NextResponse.redirect(new URL('/dashboard', request.url)); |
| 20 | // Set a header to indicate fresh login for client-side handling |
| 21 | redirectResponse.headers.set('x-auth-redirect', 'true'); |
| 22 | return redirectResponse; |
| 23 | } |
| 24 | |
| 25 | // If trying to access protected route without session, redirect to login |
| 26 | if (isProtectedRoute && !session) { |
| 27 | return NextResponse.redirect(new URL('/', request.url)); |
| 28 | } |
| 29 | |
| 30 | return response; |
| 31 | } |
| 32 | |
| 33 | // Configure which routes should be processed by this middleware |
| 34 | export const config = { |
nothing calls this directly
no test coverage detected