Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.
TypeScript i18nextnext-authnextjspostgresqlprismareact-emailt3-stacktailwindcsstrpcturborepotypescriptzod
✨ Keep guest sessions alive while actively using the app (#2499)
Luke Vella967fc05a2c07
diff --git a/apps/web/src/app/[locale]/(optional-space)/layout.tsx b/apps/web/src/app/[locale]/(optional-space)/layout.tsxindex a30c01aa3..a76af4985 100644--- a/apps/web/src/app/[locale]/(optional-space)/layout.tsx+++ b/apps/web/src/app/[locale]/(optional-space)/layout.tsx@@ -1,4 +1,5 @@import { dehydrate, HydrationBoundary } from "@tanstack/react-query";+import { SessionRefresher } from "@/components/session-refresher";import { PreferencesProvider } from "@/contexts/preferences";import { PayWall } from "@/features/billing/components/pay-wall";import { isQuickCreateEnabled } from "@/features/quick-create";@@ -23,6 +24,7 @@ export default async function Layout({return (<HydrationBoundary state={dehydrate(helpers.queryClient)}>+ <SessionRefresher /><PreferencesProvider>{children}<PayWall />diff --git a/apps/web/src/app/[locale]/e/[id]/page.tsx b/apps/web/src/app/[locale]/e/[id]/page.tsxindex e32d2c11f..9d1db923c 100644--- a/apps/web/src/app/[locale]/e/[id]/page.tsx+++ b/apps/web/src/app/[locale]/e/[id]/page.tsx@@ -11,6 +11,7 @@ import { UserDropdown } from "@/app/[locale]/e/[id]/components/user-dropdown";import LogoMarkGray from "@/assets/logo-mark-gray.svg";import { OptimizedAvatarImage } from "@/components/optimized-avatar-image";import TruncatedLinkify from "@/components/poll/truncated-linkify";+import { SessionRefresher } from "@/components/session-refresher";import { BrandingStyle } from "@/features/branding/branding-style";import { formatLocationText } from "@/features/location/utils";import { isScheduledEventEnabled } from "@/features/scheduled-event/constants";@@ -101,6 +102,7 @@ export default async function EventPage({return (<div className="page-bg-gray-50 absolute inset-0 h-dvh overflow-auto md:h-dvh md:items-center md:justify-center md:p-5 dark:bg-gray-900">+ <SessionRefresher />{brandingColor ? <BrandingStyle primaryColor={brandingColor} /> : null}<header className="fixed top-0 right-0 left-0 z-10 flex justify-between p-4"><Link href="/">diff --git a/apps/web/src/app/[locale]/invite/[urlId]/page.tsx b/apps/web/src/app/[locale]/invite/[urlId]/page.tsxindex 206354fbc..89f944e13 100644--- a/apps/web/src/app/[locale]/invite/[urlId]/page.tsx+++ b/apps/web/src/app/[locale]/invite/[urlId]/page.tsx@@ -4,6 +4,7 @@ import { dehydrate, HydrationBoundary } from "@tanstack/react-query";import type { Metadata } from "next";import { notFound } from "next/navigation";import { cache } from "react";+import { SessionRefresher } from "@/components/session-refresher";import { PermissionProvider } from "@/contexts/permissions";import { PreferencesProvider } from "@/contexts/preferences";import { createPublicSSRHelper } from "@/trpc/server/create-ssr-helper";@@ -62,6 +63,7 @@ export default async function Page(props: {return (<HydrationBoundary state={dehydrate(trpc.queryClient)}>+ <SessionRefresher /><PreferencesProvider><Providers><PermissionProvider impersonatedUserId={impersonatedUserId}>diff --git a/apps/web/src/components/session-keep-alive.tsx b/apps/web/src/components/session-keep-alive.tsxnew file mode 100644index 000000000..9356aba68--- /dev/null+++ b/apps/web/src/components/session-keep-alive.tsx@@ -0,0 +1,12 @@+"use client";++import { authClient } from "@/lib/auth-client";++// Fires /get-session on mount (and on tab focus, via better-auth defaults) so+// the cookieCache Set-Cookie reaches the browser and the session's sliding+// expiry keeps advancing while the user is active. Mounted by SessionRefresher+// only once a session is known to exist.+export function SessionKeepAlive() {+ authClient.useSession();+ return null;+}diff --git a/apps/web/src/components/session-refresher.tsx b/apps/web/src/components/session-refresher.tsxindex 67f1ed1a2..57db7efe1 100644--- a/apps/web/src/components/session-refresher.tsx+++ b/apps/web/src/components/session-refresher.tsx@@ -1,15 +1,16 @@-"use client";+import { getSession } from "@/lib/auth";+import { SessionKeepAlive } from "./session-keep-alive";-import { authClient } from "@/lib/auth-client";+// Keeps an existing session (guest OR authenticated) alive while the user is+// active. Gated on getSession — which reads the cookie cache — so truly+// anonymous viewers with no session never mount the client keep-alive and+// never trigger a /get-session request. Safe to mount on public layouts.+export async function SessionRefresher() {+ const session = await getSession();-// Fires /get-session on mount (and on tab focus, via better-auth defaults)-// so the cookieCache Set-Cookie reaches the browser. Without this, the-// session_data cookie only ever propagates at sign-in and every request-// past its 5-min lifetime falls back to a DB lookup.-//-// Only mount inside authenticated layouts — using this on public layouts-// would trigger a /get-session call for every anonymous poll viewer.-export function SessionRefresher() {- authClient.useSession();- return null;+ if (!session) {+ return null;+ }++ return <SessionKeepAlive />;}