Skip to content

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

🚸 Send signed-in accounts through setup before poll admin (#3488)

Luke Vellaeb0af8f9abfd
diff --git a/apps/web/src/app/[locale]/(optional-space)/layout.tsx b/apps/web/src/app/[locale]/(optional-space)/layout.tsx
index 5052fccd1..6dae3805d 100644
--- a/apps/web/src/app/[locale]/(optional-space)/layout.tsx
+++ b/apps/web/src/app/[locale]/(optional-space)/layout.tsx
@@ -7,6 +7,7 @@ import { loadPayWallPricing } from "@/features/billing/loaders";
import { resolveSpaceTier } from "@/features/billing/utils";
import { isQuickCreateEnabled } from "@/features/quick-create/constants";
import { getActiveSpaceForUser } from "@/features/space/data";
+import { loadOnboardingGate } from "@/features/space/loaders";
import { UserProvider } from "@/features/user/client";
import { loadUser } from "@/features/user/loaders";
import { getLocale } from "@/i18n/server/get-locale";
@@ -22,6 +23,9 @@ async function OptionalSpaceGate({ children }: { children: React.ReactNode }) {
await loadUser();
}
+ // Signed-in accounts finish onboarding before managing or creating polls.
+ await loadOnboardingGate();
+
const [locale, deviceDateTimeConfig, session, payWallPricing] =
await Promise.all([
getLocale(),
diff --git a/apps/web/src/features/space/loaders.test.ts b/apps/web/src/features/space/loaders.test.ts
index bd1a88907..90e0194cd 100644
--- a/apps/web/src/features/space/loaders.test.ts
+++ b/apps/web/src/features/space/loaders.test.ts
@@ -154,3 +154,91 @@ describe("loadActiveSpace", () => {
expect(mockRedirect).not.toHaveBeenCalled();
});
});
+
+const loadOnboardingGate = async () => {
+ vi.resetModules();
+ const mod = await import("./loaders");
+ return mod.loadOnboardingGate;
+};
+
+describe("loadOnboardingGate", () => {
+ beforeEach(() => {
+ vi.clearAllMocks();
+ });
+
+ it("lets a guest through without reading spaces", async () => {
+ authenticatedAs({ ...completeUser, isGuest: true });
+
+ const gate = await loadOnboardingGate();
+
+ await expect(gate()).resolves.toBeUndefined();
+ expect(mockRedirect).not.toHaveBeenCalled();
+ expect(mockGetActiveSpaceForUser).not.toHaveBeenCalled();
+ });
+
+ it("lets a signed-out visitor through", async () => {
+ mockGetSessionState.mockResolvedValue({ status: "unauthenticated" });
+
+ const gate = await loadOnboardingGate();
+
+ await expect(gate()).resolves.toBeUndefined();
+ expect(mockRedirect).not.toHaveBeenCalled();
+ });
+
+ it("lets an onboarded user through", async () => {
+ authenticatedAs(completeUser);
+ mockGetActiveSpaceForUser.mockResolvedValue({ id: "space-1" });
+
+ const gate = await loadOnboardingGate();
+
+ await expect(gate()).resolves.toBeUndefined();
+ expect(mockRedirect).not.toHaveBeenCalled();
+ });
+
+ // A guest who signs up from their poll is returned to it and would
+ // otherwise never reach /setup.
+ it("redirects a registered user with no space to /setup and back", async () => {
+ authenticatedAs(completeUser);
+ mockGetActiveSpaceForUser.mockResolvedValue(null);
+ mockGetOwnedSpace.mockResolvedValue(null);
+
+ const gate = await loadOnboardingGate();
+
+ await expect(gate()).rejects.toThrow(REDIRECT);
+ expect(mockRedirect).toHaveBeenCalledWith(
+ expect.stringMatching(/^\/setup\?redirectTo=%2Fpolls/),
+ );
+ });
+
+ it("redirects a registered user with no name to /setup", async () => {
+ authenticatedAs({ ...completeUser, name: "" });
+
+ const gate = await loadOnboardingGate();
+
+ await expect(gate()).rejects.toThrow(REDIRECT);
+ expect(mockRedirect).toHaveBeenCalledWith(
+ expect.stringContaining("/setup"),
+ );
+ });
+
+ // /setup bounces an owner straight back, so redirecting would loop.
+ it("lets a user through who owns a space but has none active", async () => {
+ authenticatedAs(completeUser);
+ mockGetActiveSpaceForUser.mockResolvedValue(null);
+ mockGetOwnedSpace.mockResolvedValue({ id: "space-1" });
+
+ const gate = await loadOnboardingGate();
+
+ await expect(gate()).resolves.toBeUndefined();
+ expect(mockRedirect).not.toHaveBeenCalled();
+ });
+
+ it("throws for a banned user", async () => {
+ authenticatedAs({ ...completeUser, banned: true });
+
+ const gate = await loadOnboardingGate();
+
+ await expect(gate()).rejects.toThrow();
+ expect(mockRedirect).not.toHaveBeenCalled();
+ });
+});
diff --git a/apps/web/src/features/space/loaders.ts b/apps/web/src/features/space/loaders.ts
index c771e970f..2ee874c32 100644
--- a/apps/web/src/features/space/loaders.ts
+++ b/apps/web/src/features/space/loaders.ts
@@ -56,25 +56,9 @@ export const loadActiveSpace = cache(async () => {
throw new InvalidSessionError();
}
- // Accounts created through the OTP registration flow start without a
- // name; /setup collects one before they can use the app. Timezone and
- // time format are deliberately NOT gated on: both resolve without a
- // stored value (zone from the per-visit cookie, format from
- // getLocaleDefaults), and gating on them dragged long-standing accounts
- // that predate those columns back through onboarding.
- if (!user.name) {
- redirect(
- buildSafeRedirectUrl({
- destination: "/setup",
- returnUrl: await getPathname(),
- }),
- );
- }
-
- const space = await getActiveSpaceForUser(user.id);
+ const onboarding = await resolveOnboarding(user);
- if (!space) {
- // Only send them to onboarding when they genuinely have no space.
+ if (onboarding.status === "ownedSpaceInactive") {
// A user can resolve to no *active* space while still owning one
// (their only other memberships are ineffective, or their own
// membership row is gone) — /setup would bounce them straight back,
@@ -82,10 +66,10 @@ export const loadActiveSpace = cache(async () => {
// than a thrown AppError: nothing adapts AppError for server
// components, so it would reach the error boundary as an unexpected
// error, showing "Something went wrong" and reporting to Sentry.
- if (await getOwnedSpace(user.id)) {
- notFound();
- }
+ notFound();
+ }
+ if (onboarding.status === "incomplete") {
redirect(
buildSafeRedirectUrl({
destination: "/setup",
@@ -94,9 +78,70 @@ export const loadActiveSpace = cache(async () => {
);
}
- return space;
+ return onboarding.space;
});
+/**
+ * Sends a signed-in account that hasn't finished onboarding through /setup,
+ * then back. For routes that admit guests (poll admin, new poll), where
+ * loadActiveSpace's redirects would be wrong: guests and signed-out visitors
+ * pass through untouched. Without it, an account created from a guest
+ * session lands back on its claimed poll and never reaches /setup, so the
+ * space is never created and the guest polls are never adopted. Uses the
+ * same onboarding rule as loadActiveSpace so the two gates and /setup agree.
+ */
+export const loadOnboardingGate = cache(async () => {
+ const state = await getSessionState();
+
+ if (state.status !== "authenticated" || state.session.user.isGuest) {
+ return;
+ }
+
+ const { user } = state.session;
+
+ if (user.banned) {
+ throw new InvalidSessionError();
+ }
+
+ const onboarding = await resolveOnboarding(user);
+
+ if (onboarding.status === "incomplete") {
+ redirect(
+ buildSafeRedirectUrl({
+ destination: "/setup",
+ returnUrl: await getPathname(),
+ }),
+ );
+ }
+});
+
+/**
+ * Whether the account has finished onboarding. Accounts created through the
+ * OTP registration flow start without a name; /setup collects one before
+ * they can use the app. Timezone and time format are deliberately NOT gated
+ * on: both resolve without a stored value (zone from the per-visit cookie,
+ * format from getLocaleDefaults), and gating on them dragged long-standing
+ * accounts that predate those columns back through onboarding. Only a user
+ * with no space at all is sent to set one up.
+ */
+async function resolveOnboarding(user: { id: string; name: string }) {
+ if (!user.name) {
+ return { status: "incomplete" } as const;
+ }
+
+ const space = await getActiveSpaceForUser(user.id);
+
+ if (space) {
+ return { status: "complete", space } as const;
+ }
+
+ if (await getOwnedSpace(user.id)) {
+ return { status: "ownedSpaceInactive" } as const;
+ }
+
+ return { status: "incomplete" } as const;
+}
+
/**
* The visibility scope space-scoped content reads must apply for the
* signed-in user in their active space. Same gating as loadActiveSpace;
diff --git a/apps/web/tests/guest-to-user-migration.spec.ts b/apps/web/tests/guest-to-user-migration.spec.ts
index da7282df3..0e40eea16 100644
--- a/apps/web/tests/guest-to-user-migration.spec.ts
+++ b/apps/web/tests/guest-to-user-migration.spec.ts
@@ -40,6 +40,15 @@ test.describe.serial(() => {
// Step 3: Complete registration
const registerPage = new RegisterPage(page);
await registerPage.register(testUser);
+
+ // Step 4: Setup returns them to the poll, now adopted into their space
+ await expect(
+ page.getByRole("heading", { name: "Monthly Meetup" }),
+ ).toBeVisible();
+ const urlId = page.url().match(/\/poll\/([^/?]+)/)?.[1];
+ expect(urlId).toBeTruthy();
+ const poll = await prisma.poll.findUnique({ where: { id: urlId } });
+ expect(poll?.spaceId).toBeTruthy();
});
test("guest user can create a poll and link it to existing user", async ({