Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.
TypeScript i18nextnext-authnextjspostgresqlprismareact-emailt3-stacktailwindcsstrpcturborepotypescriptzod
π Never sign out automatically on invalid sessions (#2601)
Luke Vellaa4d71c3ba873
diff --git a/apps/web/public/locales/en/app.json b/apps/web/public/locales/en/app.jsonindex 175184253..031cecc76 100644--- a/apps/web/public/locales/en/app.json+++ b/apps/web/public/locales/en/app.json@@ -11,6 +11,7 @@"accountNotLinkedDescription": "A user with this email already exists. Please log in using the original method.","actionErrorForbidden": "You are not allowed to perform this action","actionErrorInternalServerError": "An internal server error occurred",+ "actionErrorInvalidSession": "Your session is no longer valid","actionErrorNotFound": "The resource was not found","actionErrorPayloadTooLarge": "The file you uploaded is too large. Please try a smaller file.","actionErrorPaymentRequired": "You need to upgrade to perform this action",@@ -316,6 +317,9 @@"invalidFileType": "Invalid file type","invalidFileTypeDescription": "Please upload a JPG or PNG file.","invalidName": "Please enter a valid name, not a URL, email, or phone number",+ "invalidSessionDescription": "Sign out and log in again to continue.",+ "invalidSessionLabel": "Session",+ "invalidSessionTitle": "Your session is no longer valid","inviteCanceledError": "Failed to cancel invite","inviteCanceledSuccess": "Invite canceled successfully","inviteFailed": "Failed to send invitation",diff --git a/apps/web/src/app/[locale]/error.tsx b/apps/web/src/app/[locale]/error.tsxindex 94b0465c1..74fb61b24 100644--- a/apps/web/src/app/[locale]/error.tsx+++ b/apps/web/src/app/[locale]/error.tsx@@ -8,7 +8,6 @@ import Link from "next/link";import { useRouter } from "next/navigation";import React from "react";import { ErrorPage, ErrorPageLinkItem } from "@/components/error-page";-import { RouterLoadingIndicator } from "@/components/router-loading-indicator";import { Trans } from "@/i18n/client";import { signOut } from "@/lib/auth-client";import { INVALID_SESSION } from "@/lib/errors/invalid-session-error";@@ -21,21 +20,51 @@ export default function LocaleErrorBoundary({reset: () => void;}) {const router = useRouter();+ const [isSigningOut, setIsSigningOut] = React.useState(false);const isInvalidSession = error.digest === INVALID_SESSION;React.useEffect(() => {- if (isInvalidSession) {- signOut().finally(() => {- router.push("/login");- });- return;+ if (!isInvalidSession) {+ Sentry.captureException(error);}-- Sentry.captureException(error);- }, [error, router, isInvalidSession]);+ }, [error, isInvalidSession]);if (isInvalidSession) {- return <RouterLoadingIndicator />;+ // Never sign out automatically β a failed sign out turns this into an+ // infinite loop with the login page (RAL-1313).+ return (+ <ErrorPage+ label={<Trans i18nKey="invalidSessionLabel" defaults="Session" />}+ title={+ <Trans+ i18nKey="invalidSessionTitle"+ defaults="Your session is no longer valid"+ />+ }+ description={+ <Trans+ i18nKey="invalidSessionDescription"+ defaults="Sign out and log in again to continue."+ />+ }+ actions={+ <Button+ variant="primary"+ loading={isSigningOut}+ onClick={() => {+ setIsSigningOut(true);+ signOut().finally(() => {+ router.push("/login");+ });+ }}+ >+ <Trans i18nKey="signOut" defaults="Sign Out" />+ </Button>+ }+ >+ {null}+ </ErrorPage>+ );}return (diff --git a/apps/web/src/lib/errors/app-error.ts b/apps/web/src/lib/errors/app-error.tsindex 38cce3d00..c6193e088 100644--- a/apps/web/src/lib/errors/app-error.ts+++ b/apps/web/src/lib/errors/app-error.ts@@ -1,5 +1,6 @@export type AppErrorCode =| "UNAUTHORIZED"+ | "INVALID_SESSION"| "NOT_FOUND"| "FORBIDDEN"| "INTERNAL_SERVER_ERROR"diff --git a/apps/web/src/trpc/client/provider.tsx b/apps/web/src/trpc/client/provider.tsxindex e92cc5e73..21d3c94bf 100644--- a/apps/web/src/trpc/client/provider.tsx+++ b/apps/web/src/trpc/client/provider.tsx@@ -29,9 +29,34 @@ export function TRPCProvider(props: { children: React.ReactNode }) {switch (error.data?.code) {case "UNAUTHORIZED":- signOut().finally(() => {- window.location.href = "/login";- });+ // Never sign out automatically β a failed sign out turns this+ // into an infinite loop with the login page (RAL-1313).+ if (error.data.appError === "INVALID_SESSION") {+ toast.error(+ t("actionErrorInvalidSession", {+ defaultValue: "Your session is no longer valid",+ }),+ {+ id: "invalid-session",+ duration: Number.POSITIVE_INFINITY,+ action: {+ label: t("signOut", { defaultValue: "Sign Out" }),+ onClick: () => {+ signOut().finally(() => {+ window.location.href = "/login";+ });+ },+ },+ },+ );+ } else {+ toast.error(+ t("actionErrorUnauthorized", {+ defaultValue: "You are not authorized to perform this action",+ }),+ { id: "unauthorized" },+ );+ }break;case "FORBIDDEN":toast.error(diff --git a/apps/web/src/trpc/trpc.ts b/apps/web/src/trpc/trpc.tsindex 03c34dabf..ec32743aa 100644--- a/apps/web/src/trpc/trpc.ts+++ b/apps/web/src/trpc/trpc.ts@@ -62,6 +62,10 @@ const mutationSessionGuard = t.middleware(async ({ ctx, type, next }) => {throw new TRPCError({code: "UNAUTHORIZED",message: "Your session is no longer valid",+ cause: new AppError({+ code: "INVALID_SESSION",+ message: "Session references a user that no longer exists",+ }),});}