-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmiddleware.ts
More file actions
31 lines (25 loc) · 913 Bytes
/
Copy pathmiddleware.ts
File metadata and controls
31 lines (25 loc) · 913 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import { NextResponse, type NextRequest } from 'next/server';
export async function middleware(request: NextRequest) {
// تجاهل صفحة تسجيل الدخول والملفات الثابتة والـ API routes
const path = request.nextUrl.pathname;
if (
path.startsWith('/admin/login') ||
path.startsWith('/api/') ||
path.startsWith('/_next/') ||
path.startsWith('/favicon')
) {
return NextResponse.next();
}
// التحقق من الجلسة لصفحات المدير
if (path.startsWith('/admin')) {
const sessionCookie = request.cookies.get('admin-session');
if (!sessionCookie || !sessionCookie.value) {
// إعادة التوجيه لصفحة تسجيل الدخول
return NextResponse.redirect(new URL('/admin/login', request.url));
}
}
return NextResponse.next();
}
export const config = {
matcher: ['/admin/:path*'],
};