Skip to content

Fix infinite redirect loop after deleting an organization#813

Open
Cryptoteep wants to merge 1 commit into
l3montree-dev:mainfrom
Cryptoteep:fix/org-deletion-redirect-loop
Open

Fix infinite redirect loop after deleting an organization#813
Cryptoteep wants to merge 1 commit into
l3montree-dev:mainfrom
Cryptoteep:fix/org-deletion-redirect-loop

Conversation

@Cryptoteep

Copy link
Copy Markdown

Problem

Reported in l3montree-dev/devguard#2131: deleting an organization from the
settings page ("Danger Zone") redirects the user back to /, but they get
stuck bouncing back into the page of the organization that was just
deleted (which no longer exists). A manual page refresh fixes it.

Root cause

handleDeleteOrganization in
src/app/(loading-group)/[organizationSlug]/settings/page.tsx called
router.push("/") after a successful deletion. That is a client-side
transition, so the root layout's SessionShell (a Server Component that
fetches the session and organization list once) is not re-executed —
the organization list kept in SessionContext still contained the
just-deleted organization.

On /, src/app/page.tsx reads localStorage.lastActiveOrg (which still
pointed at the deleted org, since it is set whenever an org page loads
successfully — including the settings page the user just deleted the org
from) and checks whether it exists in the (stale) organization list. Since
the stale list still contained the deleted org, the app redirected right
back into /​<deleted-org-slug>, which 404s server-side and leaves the
user stuck.

Fix

  • Clear lastActiveOrg from localStorage when it points at the
    organization that was just deleted (new helper
    clearStaleLastActiveOrg in src/services/organizationService.ts, unit
    tested).
  • Navigate to / with a full page load (window.location.href = "/",
    a pattern already used elsewhere in this codebase) instead of
    router.push, so the session/organization list is refetched from the
    server and no longer contains the deleted organization.

This is a minimal, targeted fix — no unrelated files were touched.

Testing

  • Added src/services/organizationService.test.ts covering
    clearStaleLastActiveOrg (removes stale entry, leaves unrelated entry
    untouched, no-ops when unset).
  • npx jest — all pre-existing tests pass except one unrelated,
    pre-existing failure in useAutosetup.test.tsx (fails identically on
    main before this change — a sonner/toast mocking issue unrelated to
    this fix).
  • npx tsc --noEmit — no errors.

Fixes l3montree-dev/devguard#2131

Deleting an organization navigated back to "/" with router.push(), which
only performs a client-side transition. The root layout's session data
(fetched once as a Server Component) was not refetched, so the stale
organization list still contained the deleted org, and localStorage's
lastActiveOrg still pointed at it. The landing page then redirected back
into the now-deleted organization, which 404s and leaves the user stuck.

Clear lastActiveOrg when it references the deleted organization and use a
full navigation to "/" so the organization list is freshly fetched.

Fixes #2131
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Deleting an org leads to an infinite redirect loop to the just deleted org

1 participant