This commit is contained in:
2026-05-31 18:00:43 +03:30
parent 98af7d639b
commit b241d12ff5
20 changed files with 939 additions and 797 deletions

47
middleware.ts Normal file
View File

@@ -0,0 +1,47 @@
// middleware.ts
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
const AUTH_COOKIE_KEY = "tid";
// مسیرهای عمومی
const publicPaths = ["/"]; // فرض کردیم "/" صفحه لاگین است
export function middleware(request: NextRequest) {
const token = request.cookies.get(AUTH_COOKIE_KEY)?.value;
const { pathname } = request.nextUrl;
const isPublicPath = publicPaths.includes(pathname);
// اگر کاربر لاگین کرده باشد
if (token) {
// اگر رفت صفحه لاگین، بفرستش داخل پنل
if (pathname === "/") {
const url = request.nextUrl.clone();
url.pathname = "/form";
return NextResponse.redirect(url);
}
// بقیه مسیرها مجاز
return NextResponse.next();
}
// اگر کاربر توکن نداشته باشد
if (!token) {
// فقط مسیرهای عمومی مجازند
if (isPublicPath) {
return NextResponse.next();
}
// هر مسیر دیگری => ریدایرکت به لاگین
const url = request.nextUrl.clone();
url.pathname = "/";
return NextResponse.redirect(url);
}
return NextResponse.next();
}
export const config = {
matcher: ["/((?!api|_next/static|_next/image|favicon.ico).*)"],
};