kite-menu-lang
This commit is contained in:
@@ -2,37 +2,65 @@ import { NextResponse } from 'next/server'
|
||||
import type { NextRequest } from 'next/server'
|
||||
import { updateSession, decrypt } from './lib/auth'
|
||||
|
||||
const locales = ['en', 'tr'];
|
||||
const defaultLocale = 'tr';
|
||||
|
||||
export async function proxy(request: NextRequest) {
|
||||
// Always update session expiration
|
||||
let response = await updateSession(request)
|
||||
const { pathname } = request.nextUrl;
|
||||
|
||||
const isAuthPage = request.nextUrl.pathname.startsWith('/admin/login')
|
||||
const isAdminPage = request.nextUrl.pathname.startsWith('/admin')
|
||||
// --- Admin Auth Logic ---
|
||||
const isAdminPath = pathname.startsWith('/admin');
|
||||
if (isAdminPath) {
|
||||
// Always update session expiration for admin
|
||||
let response = await updateSession(request)
|
||||
|
||||
// Check if session exists and is valid
|
||||
const sessionValue = request.cookies.get('session')?.value
|
||||
let session = null
|
||||
if (sessionValue) {
|
||||
try {
|
||||
session = await decrypt(sessionValue)
|
||||
} catch(e) {
|
||||
session = null
|
||||
const isAuthPage = pathname.startsWith('/admin/login')
|
||||
|
||||
// Check if session exists and is valid
|
||||
const sessionValue = request.cookies.get('session')?.value
|
||||
let session = null
|
||||
if (sessionValue) {
|
||||
try {
|
||||
session = await decrypt(sessionValue)
|
||||
} catch(e) {
|
||||
session = null
|
||||
}
|
||||
}
|
||||
|
||||
// If trying to access admin pages (except login) without a valid session
|
||||
if (!isAuthPage && !session) {
|
||||
return NextResponse.redirect(new URL('/admin/login', request.url))
|
||||
}
|
||||
|
||||
// If trying to access login page with a valid session
|
||||
if (isAuthPage && session) {
|
||||
return NextResponse.redirect(new URL('/admin', request.url))
|
||||
}
|
||||
|
||||
return response || NextResponse.next()
|
||||
}
|
||||
|
||||
// --- i18n Logic ---
|
||||
const isPublicFile = pathname.includes('.') || pathname.startsWith('/_next') || pathname.startsWith('/api');
|
||||
|
||||
if (!isPublicFile) {
|
||||
const pathnameHasLocale = locales.some(
|
||||
(locale) => pathname.startsWith(`/${locale}/`) || pathname === `/${locale}`
|
||||
);
|
||||
|
||||
if (!pathnameHasLocale) {
|
||||
// Redirect if there is no locale
|
||||
request.nextUrl.pathname = `/${defaultLocale}${pathname}`;
|
||||
return NextResponse.redirect(request.nextUrl);
|
||||
}
|
||||
}
|
||||
|
||||
// If trying to access admin pages (except login) without a valid session
|
||||
if (isAdminPage && !isAuthPage && !session) {
|
||||
return NextResponse.redirect(new URL('/admin/login', request.url))
|
||||
}
|
||||
|
||||
// If trying to access login page with a valid session
|
||||
if (isAuthPage && session) {
|
||||
return NextResponse.redirect(new URL('/admin', request.url))
|
||||
}
|
||||
|
||||
return response || NextResponse.next()
|
||||
return NextResponse.next();
|
||||
}
|
||||
|
||||
export const config = {
|
||||
matcher: ['/admin/:path*'],
|
||||
matcher: [
|
||||
// Apply proxy to all paths except public static assets
|
||||
'/((?!_next/static|_next/image|favicon.ico|.*\\.(?:svg|png|jpg|jpeg|gif|webp|avif)$).*)',
|
||||
],
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user