Files
obc-terrassement/middleware.ts
Claude c9ff180959 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
2026-02-08 14:01:32 +00:00

71 lines
1.7 KiB
TypeScript

import { createServerClient } from "@supabase/ssr";
import { NextResponse, type NextRequest } from "next/server";
export async function middleware(request: NextRequest) {
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: [
"/dashboard/:path*",
"/formations/:path*",
"/profil/:path*",
"/login",
"/register",
],
};