Skip to content

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

🐛 Keep profile changes from reverting after a reload (#3599)

Luke Vellae08af6e3d73d
diff --git a/apps/web/src/lib/auth-plugins/host-only-cookie-cleanup.test.ts b/apps/web/src/lib/auth-plugins/host-only-cookie-cleanup.test.ts
index 84c62dc93..0a488ff9d 100644
--- a/apps/web/src/lib/auth-plugins/host-only-cookie-cleanup.test.ts
+++ b/apps/web/src/lib/auth-plugins/host-only-cookie-cleanup.test.ts
@@ -21,10 +21,12 @@ async function runHandler({
path,
cookieHeader,
responseSetCookie,
+ flag = "router",
}: {
path: string;
cookieHeader?: string;
responseSetCookie?: string;
+ flag?: string;
}) {
// biome-ignore lint/style/noNonNullAssertion: the plugin always defines this hook
const { handler } = hostOnlyCookieCleanup.hooks!.after![0];
@@ -45,6 +47,7 @@ async function runHandler({
responseHeaders,
},
returnHeaders: true,
+ _flag: flag,
} as never)) as { headers: Headers };
return result.headers.getSetCookie();
@@ -80,6 +83,16 @@ describe("hostOnlyCookieCleanup", () => {
expect(cookies).toHaveLength(2);
});
+ it("does nothing on direct auth.api calls, where nextCookies would let the deletion replace the real cookie", async () => {
+ const cookies = await runHandler({
+ path: "/update-user",
+ flag: "json",
+ responseSetCookie: `${SESSION_TOKEN}=new.token; Max-Age=5184000; Domain=.rallly.test; Path=/; HttpOnly; Secure; SameSite=Lax`,
+ });
+
+ expect(cookies).toEqual([]);
+ });
+
it("always clears host-only cookies on sign-out", async () => {
const cookies = await runHandler({ path: "/sign-out" });
diff --git a/apps/web/src/lib/auth-plugins/host-only-cookie-cleanup.ts b/apps/web/src/lib/auth-plugins/host-only-cookie-cleanup.ts
index 201fa2431..92244c845 100644
--- a/apps/web/src/lib/auth-plugins/host-only-cookie-cleanup.ts
+++ b/apps/web/src/lib/auth-plugins/host-only-cookie-cleanup.ts
@@ -20,6 +20,13 @@ export const hostOnlyCookieCleanup: BetterAuthPlugin = {
{
matcher: () => !!env.NEXT_PUBLIC_COOKIE_DOMAIN,
handler: createAuthMiddleware(async (ctx) => {
+ // Only on requests through the HTTP handler. Direct auth.api
+ // calls (server actions) reach the browser via nextCookies, which
+ // collapses Set-Cookie into a name-keyed map: our host-only
+ // deletion would replace the real domain cookie of the same name,
+ // so updateUser's fresh session_data never lands and the cookie
+ // cache serves the old user until it expires.
+ if (!("_flag" in ctx) || ctx._flag !== "router") return;
// /sign-out always cleans up. For every other path, clean up
// when a real session token landed in the response — covers
// sign-in, sign-up, OAuth callbacks, OTP verify, and periodic