main
This commit is contained in:
@@ -1,15 +1,14 @@
|
|||||||
export type NavChildItem = {
|
|
||||||
label: string;
|
|
||||||
href: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
export type NavItem = {
|
export type NavItem = {
|
||||||
label: string;
|
label: string;
|
||||||
href: string;
|
href: string;
|
||||||
children?: NavChildItem[];
|
protected?: boolean;
|
||||||
|
public?: boolean;
|
||||||
|
onlyPublic?: boolean;
|
||||||
|
visible?: boolean;
|
||||||
|
children?: NavItem[];
|
||||||
};
|
};
|
||||||
|
|
||||||
export const NAV_ITEMS: NavItem[] = [
|
export const NAV_ITEMS: NavItem[] = [
|
||||||
{ label: "home", href: "/home" },
|
{ label: "home", href: "/home", public: true },
|
||||||
{ label: "about", href: "/about" },
|
{ label: "predictions", href: "/predictions", public: true },
|
||||||
];
|
];
|
||||||
|
|||||||
81
src/proxy.ts
81
src/proxy.ts
@@ -1,50 +1,55 @@
|
|||||||
import { NextResponse } from 'next/server';
|
import { NAV_ITEMS } from "@/config/navigation";
|
||||||
import { withAuth } from 'next-auth/middleware';
|
import { withAuth } from "next-auth/middleware";
|
||||||
import createIntlMiddleware from 'next-intl/middleware';
|
import createMiddleware from "next-intl/middleware";
|
||||||
import { routing } from './i18n/routing';
|
import { NextRequest } from "next/server";
|
||||||
|
import { routing } from "./i18n/routing";
|
||||||
|
|
||||||
const intlMiddleware = createIntlMiddleware(routing);
|
const publicPages = NAV_ITEMS.flatMap((item) => [
|
||||||
|
...(!item.protected ? [item.href] : []),
|
||||||
|
...(item.children
|
||||||
|
?.filter((child) => !child.protected)
|
||||||
|
.map((child) => child.href) ?? []),
|
||||||
|
]);
|
||||||
|
|
||||||
const publicPaths = ['/signin', '/signup', '/forgot-password', '/reset-password'];
|
const handleI18nRouting = createMiddleware(routing);
|
||||||
const localizedPublicPaths = routing.locales.flatMap((locale) => publicPaths.map((path) => `/${locale}${path}`));
|
|
||||||
const allPublicPaths = [...publicPaths, ...localizedPublicPaths];
|
|
||||||
|
|
||||||
export default withAuth(
|
const authMiddleware = withAuth(
|
||||||
async function middleware(req) {
|
// Note that this callback is only invoked if
|
||||||
const intlResponse = intlMiddleware(req);
|
// the `authorized` callback has returned `true`
|
||||||
if (intlResponse) return intlResponse;
|
// and not for pages listed in `pages`.
|
||||||
|
function onSuccess(req) {
|
||||||
const token = req.nextauth?.token;
|
return handleI18nRouting(req);
|
||||||
const { pathname } = req.nextUrl;
|
|
||||||
|
|
||||||
// Kullanıcı giriş yaptıysa ve public bir sayfadaysa → home'a yönlendir
|
|
||||||
if (token && allPublicPaths.some((p) => pathname.startsWith(p))) {
|
|
||||||
const redirectUrl = new URL(`/${routing.defaultLocale}/home`, req.url);
|
|
||||||
return NextResponse.redirect(redirectUrl);
|
|
||||||
}
|
|
||||||
|
|
||||||
return NextResponse.next();
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
pages: {
|
|
||||||
signIn: `/${routing.defaultLocale}/signin`,
|
|
||||||
},
|
|
||||||
callbacks: {
|
callbacks: {
|
||||||
/**
|
authorized: ({ token }) => token != null,
|
||||||
* Eğer public route'taysa -> yetkilendirme kontrolü atla
|
},
|
||||||
* Aksi halde -> token gerekli
|
pages: {
|
||||||
*/
|
signIn: "/home",
|
||||||
authorized: ({ token, req }) => {
|
|
||||||
const { pathname } = req.nextUrl;
|
|
||||||
if (allPublicPaths.some((p) => pathname.startsWith(p))) {
|
|
||||||
return true; // public sayfa, izin ver
|
|
||||||
}
|
|
||||||
return !!token; // diğerleri için token gerekli
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
export default function proxy(req: NextRequest) {
|
||||||
|
// CRITICAL: Skip API routes entirely - they should not go through i18n or auth middleware
|
||||||
|
if (req.nextUrl.pathname.startsWith("/api/")) {
|
||||||
|
return; // Return undefined to pass through without modification
|
||||||
|
}
|
||||||
|
|
||||||
|
const publicPathnameRegex = RegExp(
|
||||||
|
`^(/(${routing.locales.join("|")}))?(${publicPages.flatMap((p) => (p === "/" ? ["", "/"] : p)).join("|")})/?$`,
|
||||||
|
"i",
|
||||||
|
);
|
||||||
|
const isPublicPage = publicPathnameRegex.test(req.nextUrl.pathname);
|
||||||
|
|
||||||
|
if (isPublicPage) {
|
||||||
|
return handleI18nRouting(req);
|
||||||
|
} else {
|
||||||
|
return (authMiddleware as any)(req);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export const config = {
|
export const config = {
|
||||||
matcher: ['/((?!api|trpc|_next|_vercel|.*\\..*).*)'],
|
matcher: "/((?!api|trpc|_next|_vercel|.*\\..*).*)",
|
||||||
|
// matcher: ['/', '/(de|en|tr)/:path*'],
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user