Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

fix(auth): break ERR_TOO_MANY_REDIRECTS deadlock for un-unlockable sessions (#1473)

Brad Barbin55ab767cfed1
diff --git a/apps/erp/app/routes/_public+/unlock.tsx b/apps/erp/app/routes/_public+/unlock.tsx
index 1df322587d..c7f2b5e5b2 100644
--- a/apps/erp/app/routes/_public+/unlock.tsx
+++ b/apps/erp/app/routes/_public+/unlock.tsx
@@ -10,6 +10,7 @@ import { userHasVerifiedTotpFactor } from "@carbon/auth/mfa.server";
import { verifyPasskeyAuthentication } from "@carbon/auth/passkey.server";
import {
completeMfaChallenge,
+ destroyAuthSession,
flash,
getAuthSession,
isSessionExpiredAbsolute,
@@ -95,8 +96,12 @@ export async function loader({ request }: LoaderFunctionArgs) {
new URL(request.url).searchParams.get("redirectTo") ?? undefined;
// Absolute cap already passed → termination wins over lock; force full re-login.
+ // DESTROY the session (not a bare redirect): the tokens are still valid, so
+ // /login would just bounce an authenticated user straight back to the app and
+ // /unlock would relock them — an ERR_TOO_MANY_REDIRECTS loop. Clearing the
+ // cookie makes /login render its form.
if (isSessionExpiredAbsolute(authSession)) {
- throw redirect(path.to.login);
+ throw await destroyAuthSession(request);
}
// Not actually locked → nothing to unlock, send them where they were going.
if (!isSessionIdleLocked(authSession)) {
@@ -105,8 +110,10 @@ export async function loader({ request }: LoaderFunctionArgs) {
// Unlock credentials: a verified TOTP factor and/or a registered passkey.
// Either can re-establish access here; a session with neither cannot unlock
- // (should not happen under CONTROLLED_ENVIRONMENT, where MFA is forced) —
- // fall through to a full re-login.
+ // (happens on FIRST-RUN enterprise: a user who idle-locks before enrolling
+ // MFA has nothing to present) — DESTROY the session and force a full re-login.
+ // A bare redirect(login) would loop: the tokens are still valid, so /login
+ // sends them back to the app, /x relocks, /unlock lands here again.
const [hasTotp, hasPasskey] = await Promise.all([
userHasVerifiedTotpFactor(authSession.userId),
isAuthProviderEnabled("passkey")
@@ -115,7 +122,7 @@ export async function loader({ request }: LoaderFunctionArgs) {
]);
if (!hasTotp && !hasPasskey) {
- throw redirect(path.to.login);
+ throw await destroyAuthSession(request);
}
return { hasTotp, hasPasskey };
diff --git a/apps/mes/app/routes/_public+/unlock.tsx b/apps/mes/app/routes/_public+/unlock.tsx
index 9bd2ba09af..7f759fd9ce 100644
--- a/apps/mes/app/routes/_public+/unlock.tsx
+++ b/apps/mes/app/routes/_public+/unlock.tsx
@@ -11,6 +11,7 @@ import { userHasVerifiedTotpFactor } from "@carbon/auth/mfa.server";
import { verifyPasskeyAuthentication } from "@carbon/auth/passkey.server";
import {
completeMfaChallenge,
+ destroyAuthSession,
flash,
getAuthSession,
isSessionExpiredAbsolute,
@@ -96,8 +97,11 @@ export async function loader({ request }: LoaderFunctionArgs) {
const redirectTo =
new URL(request.url).searchParams.get("redirectTo") ?? undefined;
+ // Absolute cap passed → terminate. DESTROY (not a bare redirect): valid tokens
+ // would make /login bounce the user back into the app and /unlock relock them,
+ // an ERR_TOO_MANY_REDIRECTS loop. Clearing the cookie makes /login render.
if (isSessionExpiredAbsolute(authSession)) {
- throw redirect(path.to.login);
+ throw await destroyAuthSession(request);
}
if (!isSessionIdleLocked(authSession)) {
throw redirect(safeRedirect(redirectTo, path.to.authenticatedRoot));
@@ -105,8 +109,9 @@ export async function loader({ request }: LoaderFunctionArgs) {
// Unlock credentials: a verified TOTP factor and/or a registered passkey.
// Either can re-establish access here; a session with neither cannot unlock
- // (should not happen under CONTROLLED_ENVIRONMENT, where MFA is forced) —
- // fall through to a full re-login.
+ // (happens on FIRST-RUN enterprise: a user who idle-locks before enrolling
+ // MFA has nothing to present) — DESTROY the session and force a full re-login.
+ // A bare redirect(login) would loop against the still-valid tokens.
const [hasTotp, hasPasskey] = await Promise.all([
userHasVerifiedTotpFactor(authSession.userId),
isAuthProviderEnabled("passkey")
@@ -115,7 +120,7 @@ export async function loader({ request }: LoaderFunctionArgs) {
]);
if (!hasTotp && !hasPasskey) {
- throw redirect(path.to.login);
+ throw await destroyAuthSession(request);
}
return { hasTotp, hasPasskey };