2023-12-19 23:09:41 +01:00
|
|
|
import type { NextRequest } from "next/server";
|
2025-11-04 21:26:44 +01:00
|
|
|
import { NextResponse } from "next/server";
|
2023-12-19 23:09:41 +01:00
|
|
|
|
2025-11-04 21:26:44 +01:00
|
|
|
import { api } from "@homarr/api/server";
|
2025-05-23 22:20:21 +02:00
|
|
|
import { localeCookieKey } from "@homarr/definitions";
|
|
|
|
|
import type { SupportedLanguage } from "@homarr/translation";
|
|
|
|
|
import { supportedLanguages } from "@homarr/translation";
|
2024-11-02 21:15:46 +01:00
|
|
|
import { createI18nMiddleware } from "@homarr/translation/middleware";
|
2023-12-19 23:09:41 +01:00
|
|
|
|
2025-05-23 22:20:21 +02:00
|
|
|
let isOnboardingFinished = false;
|
2024-11-02 21:15:46 +01:00
|
|
|
|
2025-11-04 21:26:44 +01:00
|
|
|
export async function proxy(request: NextRequest) {
|
2024-12-15 15:40:26 +01:00
|
|
|
// Redirect to onboarding if it's not finished yet
|
|
|
|
|
const pathname = request.nextUrl.pathname;
|
2025-05-23 22:20:21 +02:00
|
|
|
|
|
|
|
|
if (!isOnboardingFinished && !pathname.endsWith("/init")) {
|
2025-11-04 21:26:44 +01:00
|
|
|
const currentOnboardingStep = await api.onboard.currentStep();
|
2024-12-15 15:40:26 +01:00
|
|
|
if (currentOnboardingStep.current !== "finish") {
|
|
|
|
|
return NextResponse.redirect(new URL("/init", request.url));
|
|
|
|
|
}
|
2025-05-23 22:20:21 +02:00
|
|
|
|
|
|
|
|
isOnboardingFinished = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Only run this if the user has not already configured their language
|
|
|
|
|
const currentLocale = request.cookies.get(localeCookieKey)?.value;
|
|
|
|
|
let defaultLocale: SupportedLanguage = "en";
|
|
|
|
|
if (!currentLocale || !supportedLanguages.includes(currentLocale as SupportedLanguage)) {
|
2025-11-04 21:26:44 +01:00
|
|
|
defaultLocale = await api.serverSettings.getCulture().then((culture) => culture.defaultLocale);
|
2024-12-15 15:40:26 +01:00
|
|
|
}
|
|
|
|
|
|
2024-11-02 21:15:46 +01:00
|
|
|
// We don't want to fallback to accept-language header so we clear it
|
|
|
|
|
request.headers.set("accept-language", "");
|
2025-11-04 21:26:44 +01:00
|
|
|
|
2025-05-23 22:20:21 +02:00
|
|
|
const next = createI18nMiddleware(defaultLocale);
|
2024-11-02 21:15:46 +01:00
|
|
|
return next(request);
|
2023-12-19 23:09:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const config = {
|
|
|
|
|
matcher: ["/((?!api|static|.*\\..*|_next|favicon.ico|robots.txt).*)"],
|
|
|
|
|
};
|