Skip to content

Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.

✨ Adopt the user's saved locale on sign-in (#2667)

Luke Vella5163e49ddd41
diff --git a/apps/web/src/app/[locale]/(space)/settings/preferences/components/language-preference.tsx b/apps/web/src/app/[locale]/(space)/settings/preferences/components/language-preference.tsx
index dd5bd846d..cd29b4a37 100644
--- a/apps/web/src/app/[locale]/(space)/settings/preferences/components/language-preference.tsx
+++ b/apps/web/src/app/[locale]/(space)/settings/preferences/components/language-preference.tsx
@@ -31,9 +31,15 @@ export const LanguagePreference = ({
}) => {
const { locale } = useLocale();
const updateLocalization = useSafeAction(updateLocalizationAction);
+
+ // The saved preference can differ from the locale the app is rendered in
+ // (e.g. the cookie was reset on this device). Saving must be possible in
+ // that state even though the field matches its default.
+ const savedLanguage = defaultValue ?? locale;
+
const form = useForm({
defaultValues: {
- language: defaultValue ?? locale,
+ language: savedLanguage,
},
resolver: zodResolver(formSchema),
});
@@ -70,7 +76,7 @@ export const LanguagePreference = ({
/>
<div className="mt-6 flex flex-wrap gap-2">
<Button
- disabled={!form.formState.isDirty}
+ disabled={!form.formState.isDirty && savedLanguage === locale}
loading={form.formState.isSubmitting}
type="submit"
>
diff --git a/apps/web/src/lib/auth.ts b/apps/web/src/lib/auth.ts
index ec88923af..abc6e9627 100644
--- a/apps/web/src/lib/auth.ts
+++ b/apps/web/src/lib/auth.ts
@@ -33,6 +33,7 @@ import { getTranslation } from "@/i18n/server";
import { getLocale } from "@/i18n/server/get-locale";
import { hostOnlyCookieCleanup } from "@/lib/auth-plugins/host-only-cookie-cleanup";
import { redis } from "@/lib/kv";
+import { deleteLocaleCookie, setLocaleCookie } from "@/lib/locale/cookie";
import { posthog } from "@/lib/posthog";
import { getValueByPath } from "@/lib/utils/get-value-by-path";
@@ -333,6 +334,27 @@ export const authLib = betterAuth({
}
}),
after: createAuthMiddleware(async (ctx) => {
+ if (ctx.path === "/sign-out") {
+ const returned = ctx.context.returned as
+ | { success?: boolean }
+ | undefined;
+
+ // The locale cookie projects the signed-out user's preference;
+ // revert to accept-language for whoever uses this device next.
+ // Best-effort: a failure must not break sign-out.
+ if (returned?.success === true) {
+ try {
+ await deleteLocaleCookie();
+ } catch (error) {
+ logger.error(
+ { error },
+ "Failed to delete locale cookie on sign-out",
+ );
+ }
+ }
+ return;
+ }
+
if (
ctx.path !== "/email-otp/request-email-change" &&
ctx.path !== "/email-otp/change-email"
@@ -458,7 +480,29 @@ export const authLib = betterAuth({
},
session: {
create: {
- after: async (session) => {
+ after: async (session, ctx) => {
+ // Adopt the user's saved language on this device. Must happen
+ // before the response is sent (not in `after`), and only in a
+ // request context where cookies can be written. Best-effort:
+ // a failure here must not break a sign-in whose session is
+ // already persisted.
+ if (ctx) {
+ try {
+ const user = await prisma.user.findUnique({
+ where: { id: session.userId },
+ select: { locale: true },
+ });
+ if (user?.locale) {
+ await setLocaleCookie(user.locale);
+ }
+ } catch (error) {
+ logger.error(
+ { error, userId: session.userId },
+ "Failed to set locale cookie on sign-in",
+ );
+ }
+ }
+
after(async () => {
const user = await prisma.user
.update({
diff --git a/apps/web/src/lib/locale/cookie.ts b/apps/web/src/lib/locale/cookie.ts
index c2eb43b8c..4ae011174 100644
--- a/apps/web/src/lib/locale/cookie.ts
+++ b/apps/web/src/lib/locale/cookie.ts
@@ -16,3 +16,13 @@ export async function setLocaleCookie(locale: string) {
const cookieStore = await cookies();
cookieStore.set(LOCALE_COOKIE_NAME, locale, LOCALE_COOKIE_OPTIONS);
}
+
+export async function deleteLocaleCookie() {
+ const cookieStore = await cookies();
+ // Expire via set() with the same attributes — a deletion only takes
+ // effect when path and domain match the original cookie.
+ cookieStore.set(LOCALE_COOKIE_NAME, "", {
+ ...LOCALE_COOKIE_OPTIONS,
+ maxAge: 0,
+ });
+}