langueages
This commit is contained in:
@@ -1,38 +1,56 @@
|
||||
import { NextResponse } from 'next/server'
|
||||
import type { NextRequest } from 'next/server'
|
||||
import { updateSession, decrypt } from './lib/auth'
|
||||
import createIntlMiddleware from 'next-intl/middleware';
|
||||
import {routing} from './i18n/routing';
|
||||
|
||||
export async function proxy(request: NextRequest) {
|
||||
// Always update session expiration
|
||||
let response = await updateSession(request)
|
||||
const handleI18nRouting = createIntlMiddleware(routing);
|
||||
|
||||
const isAuthPage = request.nextUrl.pathname.startsWith('/admin/login')
|
||||
const isAdminPage = request.nextUrl.pathname.startsWith('/admin')
|
||||
export default async function proxy(request: NextRequest) {
|
||||
const { pathname } = request.nextUrl;
|
||||
|
||||
// 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
|
||||
// 1) Handle Admin Routes (Authentication)
|
||||
if (pathname.startsWith('/admin')) {
|
||||
let response = await updateSession(request)
|
||||
|
||||
const isAuthPage = pathname.startsWith('/admin/login')
|
||||
const isAdminPage = pathname.startsWith('/admin')
|
||||
|
||||
const sessionValue = request.cookies.get('session')?.value
|
||||
let session = null
|
||||
if (sessionValue) {
|
||||
try {
|
||||
session = await decrypt(sessionValue)
|
||||
} catch(e) {
|
||||
session = null
|
||||
}
|
||||
}
|
||||
|
||||
if (isAdminPage && !isAuthPage && !session) {
|
||||
return NextResponse.redirect(new URL('/admin/login', request.url))
|
||||
}
|
||||
|
||||
if (isAuthPage && session) {
|
||||
return NextResponse.redirect(new URL('/admin', request.url))
|
||||
}
|
||||
|
||||
return response || NextResponse.next()
|
||||
}
|
||||
|
||||
// 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))
|
||||
// 2) Skip API and Static Files
|
||||
const isInternalOrApi = pathname.startsWith('/api') ||
|
||||
pathname.startsWith('/_next') ||
|
||||
pathname.startsWith('/_vercel') ||
|
||||
pathname.includes('.');
|
||||
if (isInternalOrApi) {
|
||||
return NextResponse.next();
|
||||
}
|
||||
|
||||
// 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()
|
||||
// 3) Handle i18n Routing for the public menu
|
||||
return handleI18nRouting(request);
|
||||
}
|
||||
|
||||
export const config = {
|
||||
matcher: ['/admin/:path*'],
|
||||
// Match everything except internal and api routes
|
||||
matcher: ['/((?!api|_next|_vercel|.*\\..*).*)'],
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user