feature: localization's first implmentation done. all frontend hardcoded text is now localized.
This commit is contained in:
@@ -1,5 +1,13 @@
|
||||
import createMiddleware from 'next-intl/middleware';
|
||||
import { NextResponse } from 'next/server';
|
||||
import type { NextRequest } from 'next/server';
|
||||
import {
|
||||
getLocaleFromPathname,
|
||||
routing,
|
||||
stripLocaleFromPathname,
|
||||
} from './i18n/routing';
|
||||
|
||||
const handleI18nRouting = createMiddleware(routing);
|
||||
|
||||
/** Routes that must work without an existing session (first-time invitees). */
|
||||
const publicRoutes = [
|
||||
@@ -13,30 +21,42 @@ const publicRoutes = [
|
||||
'/accept-organization-invite',
|
||||
];
|
||||
|
||||
function isRedirectResponse(response: Response): boolean {
|
||||
return response.status >= 300 && response.status < 400;
|
||||
}
|
||||
|
||||
export function proxy(request: NextRequest) {
|
||||
const intlResponse = handleI18nRouting(request);
|
||||
|
||||
if (isRedirectResponse(intlResponse)) {
|
||||
return intlResponse;
|
||||
}
|
||||
|
||||
const { pathname } = request.nextUrl;
|
||||
const pathWithoutLocale = stripLocaleFromPathname(pathname);
|
||||
const locale = getLocaleFromPathname(pathname);
|
||||
const token = request.cookies.get('accessToken')?.value;
|
||||
const isAuthenticated = !!token;
|
||||
|
||||
if (isAuthenticated && pathname === '/') {
|
||||
return NextResponse.redirect(new URL('/today', request.url));
|
||||
if (isAuthenticated && pathWithoutLocale === '/') {
|
||||
return NextResponse.redirect(new URL(`/${locale}/today`, request.url));
|
||||
}
|
||||
|
||||
if (publicRoutes.includes(pathname)) {
|
||||
return NextResponse.next();
|
||||
if (publicRoutes.includes(pathWithoutLocale)) {
|
||||
return intlResponse;
|
||||
}
|
||||
|
||||
if (!isAuthenticated) {
|
||||
if (pathname === '/login') {
|
||||
return NextResponse.next();
|
||||
if (pathWithoutLocale === '/login') {
|
||||
return intlResponse;
|
||||
}
|
||||
|
||||
const loginUrl = new URL('/login', request.url);
|
||||
const loginUrl = new URL(`/${locale}/login`, request.url);
|
||||
loginUrl.searchParams.set('from', pathname);
|
||||
return NextResponse.redirect(loginUrl);
|
||||
}
|
||||
|
||||
return NextResponse.next();
|
||||
return intlResponse;
|
||||
}
|
||||
|
||||
export const config = {
|
||||
|
||||
Reference in New Issue
Block a user