fix: remove Prisma from edge middleware — use page-level auth guards instead
This commit is contained in:
parent
e1433d3bb7
commit
0c3f375a4b
|
|
@ -1,6 +1,4 @@
|
||||||
import createMiddleware from "next-intl/middleware";
|
import createMiddleware from "next-intl/middleware";
|
||||||
import { auth } from "@/auth";
|
|
||||||
import { NextRequest, NextResponse } from "next/server";
|
|
||||||
|
|
||||||
const intlMiddleware = createMiddleware({
|
const intlMiddleware = createMiddleware({
|
||||||
locales: ["fr", "en", "es"],
|
locales: ["fr", "en", "es"],
|
||||||
|
|
@ -8,35 +6,7 @@ const intlMiddleware = createMiddleware({
|
||||||
localePrefix: "always",
|
localePrefix: "always",
|
||||||
});
|
});
|
||||||
|
|
||||||
export default async function middleware(req: NextRequest) {
|
export default intlMiddleware;
|
||||||
const { pathname } = req.nextUrl;
|
|
||||||
|
|
||||||
// Auth check for protected routes
|
|
||||||
const protectedPatterns = ["/dashboard", "/admin", "/courses/[^/]+/learn"];
|
|
||||||
const isProtected = protectedPatterns.some((p) =>
|
|
||||||
new RegExp(`^/(fr|en|es)${p}`).test(pathname)
|
|
||||||
);
|
|
||||||
|
|
||||||
if (isProtected) {
|
|
||||||
const session = await auth();
|
|
||||||
if (!session) {
|
|
||||||
const locale = pathname.split("/")[1] || "fr";
|
|
||||||
return NextResponse.redirect(
|
|
||||||
new URL(`/${locale}/auth/login`, req.url)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
if (pathname.includes("/admin")) {
|
|
||||||
if ((session.user as any)?.role !== "ADMIN") {
|
|
||||||
const locale = pathname.split("/")[1] || "fr";
|
|
||||||
return NextResponse.redirect(
|
|
||||||
new URL(`/${locale}/dashboard`, req.url)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return intlMiddleware(req);
|
|
||||||
}
|
|
||||||
|
|
||||||
export const config = {
|
export const config = {
|
||||||
matcher: ["/((?!api|_next|_vercel|.*\\..*).*)"],
|
matcher: ["/((?!api|_next|_vercel|.*\\..*).*)"],
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue