import { headers, cookies } from "next/headers"; import { auth } from "@/lib/auth/server"; import type { Locale } from "@/lib/i18n/provider"; import { MARKETING_LOCALE_COOKIE } from "@/lib/marketing-locale-cookie"; export { MARKETING_LOCALE_COOKIE }; /** * Locale for the public marketing pages — cookie-based, not tied to a user * account (visitors there have no session). Separate from the authenticated * app's locale system (`lib/i18n/provider.tsx`'s `useLocale`/`setLocale`), * which persists to `users.locale` via an authenticated PATCH that would * just 401 for an anonymous visitor and revert the UI. */ export async function getMarketingLocale(): Promise { const store = await cookies(); const value = store.get(MARKETING_LOCALE_COOKIE)?.value; return value === "fr" ? "fr" : "en"; } /** A logged-in visitor's own saved locale wins (matches what they see * everywhere else in the app); otherwise the marketing cookie. */ export async function getEffectiveMarketingLocale(): Promise { const session = await auth.api.getSession({ headers: await headers() }).catch(() => null); const sessionLocale = (session?.user as { locale?: string } | undefined)?.locale; if (sessionLocale === "en" || sessionLocale === "fr") return sessionLocale; return getMarketingLocale(); }