Skip to content

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

3.9 KB
Raw Blame
import { createLogger } from "@rallly/logger";
import type { BetterAuthPlugin } from "better-auth";
import { createAuthMiddleware } from "better-auth/api";
import { parseSetCookieHeader } from "better-auth/cookies";
import { env } from "@/env";
import { hasDuplicateCookie } from "./has-duplicate-cookie";

const logger = createLogger("auth");

// Deletes the host-only variants of better-auth's session cookies left over
// from before cross-subdomain mode was enabled. Must be registered *after*
// the anonymous plugin so its after-hook runs second — the anonymous plugin
// detects new sessions by parsing Set-Cookie into a name-keyed map, and
// running our deletion last keeps that lookup pointing at the real session
// token rather than our empty placeholder.
export const hostOnlyCookieCleanup: BetterAuthPlugin = {
  id: "host-only-cookie-cleanup",
  hooks: {
    after: [
      {
        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
          // session refresh — or when the request itself proves a
          // host-only leftover is shadowing the domain cookie (the same
          // session token name appears twice in the Cookie header). The
          // latter heals stuck users on any better-auth traffic instead
          // of waiting for a sign-in they may never complete: a
          // Set-Cookie carrying the domain attribute can never clear a
          // host-only cookie, so without this the shadow wins every
          // read until it expires.
          if (ctx.path !== "/sign-out") {
            const setCookie = ctx.context.responseHeaders?.get("set-cookie");
            const sessionTokenName = ctx.context.authCookies.sessionToken.name;
            const newToken = parseSetCookieHeader(setCookie ?? "")
              .get(sessionTokenName)
              ?.value.split(".")[0];
            const hasShadowedSessionToken = hasDuplicateCookie(
              ctx.headers?.get("cookie") ?? "",
              sessionTokenName,
            );
            if (!newToken && !hasShadowedSessionToken) return;
            if (!newToken) {
              // Observability for the healing path: how often shadowed
              // cookies are still out there. No token values logged.
              logger.info(
                { path: ctx.path },
                "Clearing shadowed host-only session cookies",
              );
            }
          }
          for (const cookie of [
            ctx.context.authCookies.sessionToken,
            ctx.context.authCookies.sessionData,
          ]) {
            const { domain: _, ...attributes } = cookie.attributes;
            // maxAge: 0 alone is not a reliable deletion: Next.js
            // re-serializes Set-Cookie headers on the cookies() merge path
            // and drops the falsy zero, turning the "deletion" into an
            // empty host-only *session* cookie — a shadow that outlives
            // every later deletion attempt and wedges the login redirect
            // loop this plugin exists to prevent. Expires survives that
            // pipeline, so send both.
            ctx.setCookie(cookie.name, "", {
              ...attributes,
              maxAge: 0,
              expires: new Date(0),
            });
          }
        }),
      },
    ],
  },
};