# Magic Link Sign-In

## Objective

Sign in from an emailed link instead of remembering a password.

Passwordless authentication by email: a short-lived single-use link, purpose-bound tokens, and defined behaviour when the link opens somewhere unexpected.

## 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. Make links short-lived — minutes, not hours — single-use, and bound to the purpose they were issued for. A sign-in token must not also work as a reset or invite token.
2. Bind the request to the browser that started it: issue a paired value on the request page and require it, so a link opened on another device follows the handoff path instead of silently signing in.
3. Give the same response to every address entered, and never reveal whether an account exists at any step.
4. Validate the post-login destination against an allowlist and drop anything external.
5. Do not make magic links the only way in for accounts that matter. Email is a single point of failure; keep at least one other method available and say so at sign-up.

## 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

- Opening the link on a different device or browser must offer an explicit confirmation step rather than failing or silently authenticating.
- Link scanners in corporate mail systems will hit the URL before the user does — consuming the token on GET breaks sign-in entirely for those users.
- Requesting a second link must invalidate the first, and the emails must be distinguishable so people do not click the stale one.
- A consumed or expired link must show a neutral 'request a new one' screen with no account details.
- Requests must be rate-limited per address and per IP; the endpoint sends email on demand.
- The token must not survive in browser history, referrer headers, or server logs after use.

## 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

- [ ] Links expire in minutes, are single-use, and are valid only for sign-in.
- [ ] A link opened in a different browser requires explicit confirmation.
- [ ] Pre-fetching by mail scanners does not consume the token.
- [ ] Post-login redirects are allowlisted and never external.
- [ ] Responses never disclose whether an address is registered.
- [ ] An alternative sign-in method remains available on every account.
- [ ] 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.
