Files
no-copy-admin-panel-frontend/src/proxy.ts
T

59 lines
1.8 KiB
TypeScript
Raw Normal View History

2026-01-19 12:55:41 +07:00
import { NextResponse, NextRequest } from 'next/server';
import { cookies } from 'next/headers'
import createIntlMiddleware from 'next-intl/middleware'
import { routing } from '@/i18n/routing'
2026-04-06 16:08:36 +07:00
import { decrypt } from '@/app/actions/session';
2026-01-19 12:55:41 +07:00
const intlMiddleware = createIntlMiddleware(routing);
export default async function proxy(request: NextRequest) {
const path = request.nextUrl.pathname;
if (
path.startsWith('/_next') ||
2026-04-06 17:04:53 +07:00
path.includes('/api/')
2026-01-19 12:55:41 +07:00
) {
2026-04-06 17:04:53 +07:00
return NextResponse.next();
2026-01-19 12:55:41 +07:00
}
2026-04-06 17:04:53 +07:00
const pathname = request.nextUrl.pathname;
const locale = pathname.split('/')[1];
const isValidLocale = routing.locales.includes(locale as any);
if (!isValidLocale && !path.includes('/login')) {
const defaultLocale = routing.defaultLocale;
const newUrl = new URL(`/${defaultLocale}${path}`, request.url);
return NextResponse.redirect(newUrl);
}
const isLoginPage = path.includes(`/${locale}/login`) || path === '/login';
const isPublicPage = isLoginPage || path.includes('/api/');
if (!isPublicPage) {
2026-05-12 11:51:31 +07:00
const cookie = (await cookies()).get('session_nc_ap')?.value;
2026-04-06 17:04:53 +07:00
const session = await decrypt(cookie);
2026-04-08 14:25:27 +07:00
const targetLocale = isValidLocale ? locale : routing.defaultLocale;
2026-04-06 17:04:53 +07:00
if (!session?.token) {
const loginUrl = new URL(`/${targetLocale}/login`, request.url);
return NextResponse.redirect(loginUrl);
2026-04-08 14:25:27 +07:00
} else if (path === '/en' || path === '/ru') {
const dashboardUrl = new URL(`/${targetLocale}/pages`, request.url);
dashboardUrl.search = new URL(request.url).search;
return NextResponse.redirect(dashboardUrl);
2026-04-06 17:04:53 +07:00
}
}
2026-04-06 16:08:36 +07:00
2026-01-19 12:55:41 +07:00
const intlResponse = intlMiddleware(request);
if (intlResponse) {
2026-04-06 16:08:36 +07:00
const response = new NextResponse(intlResponse.body, intlResponse);
response.headers.set('x-pathname', path);
2026-04-06 17:04:53 +07:00
return response;
2026-01-19 12:55:41 +07:00
}
2026-04-06 17:04:53 +07:00
return NextResponse.next();
2026-01-19 12:55:41 +07:00
}
export const config = {
2026-01-22 17:30:46 +07:00
matcher: ['/((?!_next/static|_next/image|.*\\.png$).*)']
2026-01-19 12:55:41 +07:00
};