Skip to content

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

🐛 Stop guest writes when the guest session could not be created

Luke Vella0f2c4b9e4733
diff --git a/apps/web/public/locales/en/app.json b/apps/web/public/locales/en/app.json
index f180aa66f..3cc0e2479 100644
--- a/apps/web/public/locales/en/app.json
+++ b/apps/web/public/locales/en/app.json
@@ -435,6 +435,7 @@
"guest": "Guest",
"guestPollAlertDescription": "<0>Create an account</0> or <1>login</1> to claim this poll.",
"guestPollAlertTitle": "Your administrator rights can be lost if you clear your cookies",
+ "guestSessionUnavailableError": "We couldn't start a session for you. Check that cookies are enabled and try again.",
"hexColor": "Hex color",
"hiddenParticipantName": "Participant #{number}",
"hideAttribution": "Hide attribution",
diff --git a/apps/web/src/features/poll/components/comments-sheet.tsx b/apps/web/src/features/poll/components/comments-sheet.tsx
index db54fc9d3..7d7d844af 100644
--- a/apps/web/src/features/poll/components/comments-sheet.tsx
+++ b/apps/web/src/features/poll/components/comments-sheet.tsx
@@ -114,6 +114,11 @@ function NewCommentForm({ onSubmitted }: { onSubmitted: () => void }) {
return t("actionErrorTooManyRequests", {
defaultValue: "You are making too many requests",
});
+ case "sessionUnavailable":
+ return t("guestSessionUnavailableError", {
+ defaultValue:
+ "We couldn't start a session for you. Check that cookies are enabled and try again.",
+ });
default:
return t("actionErrorInternalServerError", {
defaultValue: "An internal server error occurred",
@@ -125,7 +130,11 @@ function NewCommentForm({ onSubmitted }: { onSubmitted: () => void }) {
<form
className="w-full"
onSubmit={handleSubmit(async ({ authorName, content }) => {
- await createGuestIfNeeded();
+ const session = await createGuestIfNeeded();
+ if (!session.ok) {
+ toast.error(getCommentErrorMessage(session.reason));
+ return;
+ }
const result = await addComment.execute({
authorName,
content,
diff --git a/apps/web/src/features/poll/components/create-poll.tsx b/apps/web/src/features/poll/components/create-poll.tsx
index dc7612d00..548a45843 100644
--- a/apps/web/src/features/poll/components/create-poll.tsx
+++ b/apps/web/src/features/poll/components/create-poll.tsx
@@ -230,7 +230,20 @@ export const CreatePoll = ({
id="create-poll"
onSubmit={form.handleSubmit(async (formData) => {
const title = required(formData?.title.trim());
- await createGuestIfNeeded();
+ const session = await createGuestIfNeeded();
+ if (!session.ok) {
+ toast.error(
+ session.reason === "tooManyRequests"
+ ? t("actionErrorTooManyRequests", {
+ defaultValue: "You are making too many requests",
+ })
+ : t("guestSessionUnavailableError", {
+ defaultValue:
+ "We couldn't start a session for you. Check that cookies are enabled and try again.",
+ }),
+ );
+ return;
+ }
const res = await makePoll.mutateAsync({
title: title,
location: formData?.location?.trim(),
diff --git a/apps/web/src/features/poll/components/new-participant-modal.tsx b/apps/web/src/features/poll/components/new-participant-modal.tsx
index 6b83e98b5..0eff1e0f4 100644
--- a/apps/web/src/features/poll/components/new-participant-modal.tsx
+++ b/apps/web/src/features/poll/components/new-participant-modal.tsx
@@ -172,6 +172,11 @@ export const NewParticipantForm = (props: NewParticipantModalProps) => {
defaultValue:
"We couldn't verify your session. Please refresh the page and try again.",
});
+ case "sessionUnavailable":
+ return t("guestSessionUnavailableError", {
+ defaultValue:
+ "We couldn't start a session for you. Check that cookies are enabled and try again.",
+ });
default:
return t("newParticipantFormSubmitError", {
defaultValue:
@@ -271,7 +276,13 @@ export const NewParticipantForm = (props: NewParticipantModalProps) => {
if (!pollId) {
throw new Error("NewParticipantForm needs a pollId");
}
- await createGuestIfNeeded();
+ const session = await createGuestIfNeeded();
+ if (!session.ok) {
+ setError("root", {
+ message: getSubmitErrorMessage(session.reason),
+ });
+ return;
+ }
const result = await addParticipant.execute({
name: data.name,
votes: props.votes,
diff --git a/apps/web/src/features/user/client.tsx b/apps/web/src/features/user/client.tsx
index eb9880da2..99b93cb9d 100644
--- a/apps/web/src/features/user/client.tsx
+++ b/apps/web/src/features/user/client.tsx
@@ -5,6 +5,8 @@ import React from "react";
import type { UserAbility } from "@/features/user/ability";
import { defineAbilityFor } from "@/features/user/ability";
import type { UserDTO } from "@/features/user/schema";
+import type { GuestSessionResult } from "@/features/user/utils";
+import { toGuestSessionResult } from "@/features/user/utils";
import { authClient } from "@/lib/auth-client";
import { useLocale } from "@/lib/locale/client";
import { isOwner } from "@/lib/utils/permissions";
@@ -72,11 +74,19 @@ export function useUser() {
return React.useMemo(() => {
return {
user: user ?? undefined,
- createGuestIfNeeded: async () => {
- if (!user) {
- await authClient.signIn.anonymous();
- router.refresh();
+ createGuestIfNeeded: async (): Promise<GuestSessionResult> => {
+ if (user) return { ok: true };
+
+ let result: GuestSessionResult;
+ try {
+ const { error } = await authClient.signIn.anonymous();
+ result = toGuestSessionResult(error);
+ } catch {
+ result = { ok: false, reason: "sessionUnavailable" };
}
+
+ if (result.ok) router.refresh();
+ return result;
},
getAbility: (): UserAbility => defineAbilityFor(user ?? undefined),
ownsObject: (resource: { userId?: string | null }) => {
diff --git a/apps/web/src/features/user/utils.test.ts b/apps/web/src/features/user/utils.test.ts
new file mode 100644
index 000000000..4d2cfc0b7
--- /dev/null
+++ b/apps/web/src/features/user/utils.test.ts
@@ -0,0 +1,31 @@
+import { describe, expect, it } from "vitest";
+import { toGuestSessionResult } from "./utils";
+
+describe("toGuestSessionResult", () => {
+ it("succeeds when sign-in returns no error", () => {
+ expect(toGuestSessionResult(null)).toEqual({ ok: true });
+ });
+
+ it("succeeds when a guest session already exists", () => {
+ expect(
+ toGuestSessionResult({
+ status: 400,
+ code: "ANONYMOUS_USERS_CANNOT_SIGN_IN_AGAIN_ANONYMOUSLY",
+ }),
+ ).toEqual({ ok: true });
+ });
+
+ it("reports rate limiting", () => {
+ expect(toGuestSessionResult({ status: 429 })).toEqual({
+ ok: false,
+ reason: "tooManyRequests",
+ });
+ });
+
+ it("fails on any other error", () => {
+ expect(toGuestSessionResult({ status: 500 })).toEqual({
+ ok: false,
+ reason: "sessionUnavailable",
+ });
+ });
+});
diff --git a/apps/web/src/features/user/utils.ts b/apps/web/src/features/user/utils.ts
new file mode 100644
index 000000000..cc1291fb7
--- /dev/null
+++ b/apps/web/src/features/user/utils.ts
@@ -0,0 +1,25 @@
+export type GuestSessionResult =
+ | { ok: true }
+ | { ok: false; reason: "tooManyRequests" | "sessionUnavailable" };
+
+/**
+ * Maps the anonymous sign-in response to whether a session now exists. The
+ * "already anonymous" refusal means a guest cookie is present (the page's
+ * user context was stale), so the write can go ahead.
+ */
+export const toGuestSessionResult = (
+ error: { status: number; code?: string } | null,
+): GuestSessionResult => {
+ if (
+ !error ||
+ error.code === "ANONYMOUS_USERS_CANNOT_SIGN_IN_AGAIN_ANONYMOUSLY"
+ ) {
+ return { ok: true };
+ }
+
+ if (error.status === 429) {
+ return { ok: false, reason: "tooManyRequests" };
+ }
+
+ return { ok: false, reason: "sessionUnavailable" };
+};