From c9ff180959c39cc1577f43cff5f26f5527163a80 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 8 Feb 2026 14:01:32 +0000 Subject: [PATCH] fix: inline Supabase middleware to fix Vercel Edge resolution The @/ path alias was not resolved in Vercel's Edge Function bundler, causing "unsupported modules" error. Inlined the middleware logic directly to avoid the import. https://claude.ai/code/session_01H2aRGDaKgarPvhay2HxN6Y --- middleware.ts | 62 ++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 57 insertions(+), 5 deletions(-) diff --git a/middleware.ts b/middleware.ts index 4f741fc..800c964 100644 --- a/middleware.ts +++ b/middleware.ts @@ -1,17 +1,69 @@ -import { updateSession } from "@/lib/supabase/middleware"; -import type { NextRequest } from "next/server"; +import { createServerClient } from "@supabase/ssr"; +import { NextResponse, type NextRequest } from "next/server"; export async function middleware(request: NextRequest) { - return await updateSession(request); + let supabaseResponse = NextResponse.next({ + request, + }); + + 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 }) => + 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(); + + // Rediriger vers login si pas connecte et route protegee + if ( + !user && + (request.nextUrl.pathname.startsWith("/dashboard") || + request.nextUrl.pathname.startsWith("/formations") || + request.nextUrl.pathname.startsWith("/profil")) + ) { + const url = request.nextUrl.clone(); + url.pathname = "/login"; + return NextResponse.redirect(url); + } + + // Rediriger vers dashboard si deja connecte et sur login/register + if ( + user && + (request.nextUrl.pathname === "/login" || + request.nextUrl.pathname === "/register") + ) { + const url = request.nextUrl.clone(); + url.pathname = "/dashboard"; + return NextResponse.redirect(url); + } + + return supabaseResponse; } export const config = { matcher: [ - // Routes protégées "/dashboard/:path*", "/formations/:path*", "/profil/:path*", - // Routes auth (redirection si déjà connecté) "/login", "/register", ],