Skip to content

feat: restore the originally-attempted route after a ProtectedRoute redirect - #1098

Closed
Junirezz wants to merge 1 commit into
mainfrom
feat/1040-protected-route-redirect-back
Closed

feat: restore the originally-attempted route after a ProtectedRoute redirect#1098
Junirezz wants to merge 1 commit into
mainfrom
feat/1040-protected-route-redirect-back

Conversation

@Junirezz

Copy link
Copy Markdown
Owner

Summary

Closes #1040.

Heads up for reviewers: #1040's title is an exact duplicate of #981, resolved by #1071 — merged less than a day before this PR was opened. That PR built the whole role-aware nav/guard system (roles.ts, ProtectedRoute, admin nav gating, docs, tests). This PR doesn't re-build any of that; it finishes one specific piece ProtectedRoute's own doc comment already promised but never delivered:

"The attempted path is passed along in location state so the redirect target can restore it later."

Nothing ever consumed that location.state.from. A guest bounced away from /admin who later connected the admin wallet just stayed on / instead of landing back on /admin.

Added useRestoreGuardedRoute(role), called from App.tsx whenever role changes. It tries the stashed from path once per role transition: if the new role is allowed, the user lands where they originally tried to go; if not, ProtectedRoute guards it again — since role hasn't changed, the hook won't re-fire, so there's no redirect loop. Updated ROLE_BASED_NAVIGATION.md to document the completed behavior.

Test plan

  • Added useRestoreGuardedRoute.test.tsx (covers: no stashed path → no-op, role already allowed → restores immediately, role still disallowed → stays put without looping, role changes to allowed → restores)
  • Re-ran ProtectedRoute.test.tsx and roles.test.ts — still passing, no behavior change to existing guard logic
  • npx tsc -b — no new type errors (one pre-existing, unrelated ToastCenter.tsx error present on main before this change too)

🤖 Generated with Claude Code

…edirect

ProtectedRoute (#1071, #981) already redirects a disallowed role away from a
guarded route and stashes the attempted path in location.state.from "so a
future redirect target can restore it later" — but nothing ever read that
state, so a user who tried /admin and later connected the admin wallet just
stayed on the page they'd been bounced to instead of landing back on /admin.

Add useRestoreGuardedRoute(role), called from App.tsx whenever role changes.
It tries the stashed `from` path once per role transition: if the new role
is allowed, the user lands where they originally tried to go; if not,
ProtectedRoute guards it again (role hasn't changed, so the hook won't
re-fire), avoiding a redirect loop.

Related to #1040, which duplicates #981, already resolved by #1071 for the
role/nav/guard scaffolding this builds on.
@esthertitilayo-dev

Copy link
Copy Markdown
Contributor

Closing — this was opened under the wrong GitHub account (repo owner's, not the assigned contributor's) due to a tooling mistake on my end. Re-opening correctly from a fork under the right account.

@Junirezz Junirezz closed this Jul 29, 2026
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.

Frontend: Add role-aware navigation and protected route guards

2 participants