# Permission-Aware Route Fallbacks

## Objective

Send people somewhere useful when a page exists but they cannot open it.

Access denials that distinguish why the user was blocked and route them to something they can actually do.

## Before You Begin

This feature is being added to an application that already exists and already
works. Do not scaffold a new project, and do not assume a blank slate.

Inspect the codebase first and establish:

- The existing application structure and where code of this kind already lives.
- The framework and version in use.
- The existing design system — colours, spacing, typography, and component conventions.
- Existing UI components you can reuse instead of writing new ones.
- The existing database structure, if this feature needs to persist anything.
- The existing authentication and authorization system, if this feature is user-scoped.
- Dependencies already installed, so you don't add a library that duplicates one.
- The existing test setup and conventions.

Only start writing code once you understand the above. If the application
already implements part of this feature, extend it rather than replacing it.

## Implementation Instructions

1. Enumerate the reasons a route can be refused: signed out, expired session, wrong workspace, insufficient role, suspended account, unpaid plan.
2. Give each its own response and its own next action. Sign in, switch workspace, request access, contact an admin, and upgrade are all different fixes.
3. For sensitive resources, return the same not-found response whether the record is missing or merely forbidden, so existence is not disclosed.
4. Capture the intended destination before redirecting to sign-in and return the user there afterwards.
5. Do NOT render a blank page or bounce to the dashboard with no explanation. Silent redirects read as bugs and generate support tickets.

## UI and UX Requirements

Match the application's existing design system exactly. Reuse its components,
spacing, and typography. This feature should look like it was always there.

## Responsive Requirements

Works on mobile, tablet, and desktop. Touch targets are large enough to hit on a
phone, and nothing overflows horizontally at 320px.

## Accessibility Requirements

- Fully keyboard navigable.
- Correct semantic elements and ARIA roles.
- Visible focus states.
- Meets WCAG AA contrast.
- Dynamic changes are announced to screen readers.
- Respects prefers-reduced-motion.

## Edge Cases

- A validated return URL only. An open redirect parameter is a phishing vector, so reject anything not internal to the app.
- Redirect loops happen when the fallback destination is itself forbidden. Cap the chain and fall through to a guaranteed-safe page.
- An expired session on a POST must not lose the user's input. Preserve it or tell them plainly that it was not saved.
- Do not leak record names, counts, or workspace details in the denial message for resources the user cannot see.
- API and page routes need different responses. A fetch must get a status code, not an HTML sign-in page.
- Where a request-access flow exists, route the user into it rather than dead-ending at 'forbidden'.
- Deep links shared between colleagues in different workspaces should offer to switch workspace rather than simply refusing.

## Testing

Exercise the feature end to end in the running application. Cover every edge case
above, then run the existing test suite and confirm nothing regressed.

## Acceptance Criteria

- [ ] Each denial reason produces a distinct response with a specific next action.
- [ ] Sensitive forbidden resources are indistinguishable from missing ones.
- [ ] The intended destination survives sign-in and is validated as internal before use.
- [ ] No route chain can loop; a guaranteed-safe fallback always terminates it.
- [ ] API requests receive status codes rather than redirects to HTML.
- [ ] Denial messages disclose nothing about the resource itself.
- [ ] The feature matches the existing design system.
- [ ] No existing functionality is broken.

## Adaptation Rules

- Match the existing design system. Do not introduce a new colour palette,
  spacing scale, or component library.
- Reuse existing components and utilities wherever they fit.
- Follow the naming, file layout, and code style already present.
- Do not upgrade, replace, or remove existing dependencies to make this
  feature fit. Adapt the feature to the app, not the app to the feature.
- Do not break existing functionality. If a change is genuinely required in
  existing code, make the smallest one that works and say so.
- If something in these instructions conflicts with how the application is
  built, follow the application and explain the deviation.

## Final Verification

Before you report the work as done:

1. Re-read the acceptance criteria above and check each one against what you
   actually built.
2. Run the application and exercise the feature end to end.
3. Run the existing test suite and confirm you have broken nothing.
4. Check the feature on mobile, tablet, and desktop widths.
5. Check keyboard navigation and focus handling.
6. Summarize what changed: files added, files modified, and anything you
   deliberately did differently because of how this application is built.

If any acceptance criterion is unmet, fix it before reporting completion.
