dasdasd
This commit is contained in:
47
middleware.ts
Normal file
47
middleware.ts
Normal 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).*)"],
|
||||
};
|
||||
Reference in New Issue
Block a user