2026-06-20 14:51:43 +03:30
|
|
|
import createMiddleware from 'next-intl/middleware';
|
2026-04-23 15:33:11 +03:30
|
|
|
import { NextResponse } from 'next/server';
|
|
|
|
|
import type { NextRequest } from 'next/server';
|
2026-06-20 14:51:43 +03:30
|
|
|
import {
|
|
|
|
|
getLocaleFromPathname,
|
|
|
|
|
routing,
|
|
|
|
|
stripLocaleFromPathname,
|
|
|
|
|
} from './i18n/routing';
|
2026-07-12 17:51:31 +03:30
|
|
|
import { hasUsableAccessToken } from './lib/auth/accessToken';
|
2026-06-20 14:51:43 +03:30
|
|
|
|
|
|
|
|
const handleI18nRouting = createMiddleware(routing);
|
2026-04-23 15:33:11 +03:30
|
|
|
|
2026-05-17 00:02:13 +03:30
|
|
|
/** Routes that must work without an existing session (first-time invitees). */
|
|
|
|
|
const publicRoutes = [
|
|
|
|
|
'/',
|
|
|
|
|
'/login',
|
|
|
|
|
'/register',
|
|
|
|
|
'/terms',
|
|
|
|
|
'/privacy',
|
|
|
|
|
'/forgot-password',
|
|
|
|
|
'/accept-invite',
|
|
|
|
|
'/accept-organization-invite',
|
|
|
|
|
];
|
2026-04-23 15:33:11 +03:30
|
|
|
|
2026-06-20 14:51:43 +03:30
|
|
|
function isRedirectResponse(response: Response): boolean {
|
|
|
|
|
return response.status >= 300 && response.status < 400;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-30 14:05:44 +03:30
|
|
|
export function proxy(request: NextRequest) {
|
2026-06-20 14:51:43 +03:30
|
|
|
const intlResponse = handleI18nRouting(request);
|
|
|
|
|
|
|
|
|
|
if (isRedirectResponse(intlResponse)) {
|
|
|
|
|
return intlResponse;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-23 15:33:11 +03:30
|
|
|
const { pathname } = request.nextUrl;
|
2026-06-20 14:51:43 +03:30
|
|
|
const pathWithoutLocale = stripLocaleFromPathname(pathname);
|
|
|
|
|
const locale = getLocaleFromPathname(pathname);
|
2026-04-23 15:33:11 +03:30
|
|
|
const token = request.cookies.get('accessToken')?.value;
|
2026-07-12 17:51:31 +03:30
|
|
|
const isAuthenticated = hasUsableAccessToken(token);
|
2026-04-23 15:33:11 +03:30
|
|
|
|
2026-06-20 14:51:43 +03:30
|
|
|
if (isAuthenticated && pathWithoutLocale === '/') {
|
|
|
|
|
return NextResponse.redirect(new URL(`/${locale}/today`, request.url));
|
2026-04-28 11:57:29 +03:30
|
|
|
}
|
|
|
|
|
|
2026-06-20 14:51:43 +03:30
|
|
|
if (publicRoutes.includes(pathWithoutLocale)) {
|
|
|
|
|
return intlResponse;
|
2026-04-23 15:33:11 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!isAuthenticated) {
|
2026-06-20 14:51:43 +03:30
|
|
|
if (pathWithoutLocale === '/login') {
|
|
|
|
|
return intlResponse;
|
2026-04-23 15:33:11 +03:30
|
|
|
}
|
|
|
|
|
|
2026-06-20 14:51:43 +03:30
|
|
|
const loginUrl = new URL(`/${locale}/login`, request.url);
|
2026-04-23 15:33:11 +03:30
|
|
|
loginUrl.searchParams.set('from', pathname);
|
|
|
|
|
return NextResponse.redirect(loginUrl);
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-20 14:51:43 +03:30
|
|
|
return intlResponse;
|
2026-04-23 15:33:11 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const config = {
|
|
|
|
|
matcher: [
|
|
|
|
|
'/((?!_next/static|_next/image|favicon.ico|.*\\.(?:svg|png|jpg|jpeg|gif|webp)$).*)',
|
|
|
|
|
],
|
2026-04-30 14:05:44 +03:30
|
|
|
};
|