Skip to content

feat(auth): protect deeplinked screens and return to them after login - #183

Draft
fernandatoledo wants to merge 6 commits into
masterfrom
feat/protect-deeplinked-screens-and-return-to
Draft

fernandatoledo wants to merge 6 commits into
masterfrom
feat/protect-deeplinked-screens-and-return-to

Conversation

@fernandatoledo

@fernandatoledo fernandatoledo commented Nov 26, 2026 •

Copy link
Copy Markdown
Collaborator

Jira board reference:


What does this do?

Deep links into protected screens now send a logged-out user to sign-in, show "Sign in to open this link.", and take them to the linked screen after they log in instead of /, opened on top of the tabs so back returns to the app. feed/[id] and feed/add-post move under the authenticated Stack.Protected guard (they were reachable without a session), and the guarded screen lists now live in src/lib/auth/deep-link-path.ts and are rendered from there. The app scheme is also suffixed per environment (RootstrapApp.development, .qa, .staging; production stays RootstrapApp) so several builds can be installed side by side.


Why did you do this?

Opening a link to a protected screen while logged out lost the target: after login the user always landed on /. Using the same scheme for every environment meant a link could open the wrong installed build.

Who/what does this impact?

  • Incoming links on native: a new src/app/+native-intent.tsx records the link in an in-memory store (src/lib/auth/pending-deep-link.ts) and, when there is no valid session, sends the user straight to the guard's fallback (/sign-in, or /onboarding on first launch) so the protected screen is never pushed. The session check is shared with AuthProvider through hasValidSession(), and when a link finds the stored token expired, syncSession() makes a running AuthProvider drop it too, so the guards and +native-intent agree. Only links that resolve to a protected route are remembered: isReturnToPath() matches an allow-list built from AUTHENTICATED_SCREENS and APP_TAB_SCREENS, so public, auth and unknown routes open unchanged. A test walks src/app and fails when a route file is not classified. The link is dropped on onboarding, when the logged-out user leaves the auth screens, and when a newer link arrives.
  • Route guards in src/app/_layout.tsx: the guards always send an authenticated user to /, and PendingDeepLinkHandler then pushes the pending link once the tabs are showing and the session is valid, so the linked screen keeps a back stack (header back, swipe-back, Android back). Also the sign-in screen (LoginForm gets an optional notice). While auth is hydrating the root layout now renders nothing and keeps the splash, instead of an unguarded <Stack />; the splash is hidden there once auth is ready (it used to be hidden from (app)/_layout.tsx, which never mounts in that window anymore).
  • Native config: dev, qa and staging builds register a new scheme, so existing dev clients must be rebuilt, and links configured outside the app (push payloads, emails, OAuth redirect URIs) need the per-environment scheme. Docs updated in guides/navigation.mdx, agent_docs/architecture.md and REVIEW.md.

Layer 2: Risk Classification

Author applies a risk label when opening the PR. When in doubt, escalate.

Label When to use
risk:low Config, copy, minor UI tweaks. Low blast radius.
risk:medium Feature work, refactors touching multiple files.
risk:high Auth, payments, migrations, security-sensitive code.

Setup

  • Create risk:low, risk:medium, risk:high labels in the repo
  • Add risk criteria to contributing guide or PR template
  • Add reminder in PR template to apply the label before requesting review
  • Define who can override/escalate (suggested: tech lead)

How did you test this?

pnpm check-all passes (lint, type-check, translations, 45 suites / 404 tests). APP_ENV=<env> npx expo config --type public shows the expected scheme for each environment. On the iOS Simulator with a development build (old scheme RootstrapApp) and Maestro:

  • logged out, feed/1 and feed/add-post land on sign-in with the message, both on cold start and with the app running;
  • links to sign-up and forgot-password open normally with no message afterwards, and an unknown route opens the 404 without asking to sign in;
  • with onboarding pending, onboarding wins and the link is discarded.

The redirect to the link after login was checked on the simulator against a local mock of the sign-in endpoint (the development backend didn't accept the test login):

  • logged out, a link to feed/add-post → sign-in → login → Add Post with a "Feed" back button, and back returns to the tabs; same for feed/1 → Post;

  • logged in with a 60-second token left to expire with the app in the foreground, a link to feed/add-post goes to sign-in with the message instead of opening the screen with a dead token.

  • Tested on iOS

  • Tested on Android

  • Tested on a small device

  • Tested on a real device

  • Tested all flows related with this PR changes

  • Tested accessibility

  • Added tests


Notes:

  • Web is unchanged apart from the guard: +native-intent only runs on native, so on web a logged-out user is still sent to sign-in but the link is not restored.
  • expo-router/testing-library's renderRouter can't run in this repo (transitive react-test-renderer 19.1 vs React 19.3), so the guard tests mock Stack.Protected. A follow-up could align react-test-renderer and add an end-to-end router test for the post-login redirect.

Screenshots / Previews

🧑‍🎨 Figma Design:

📱 Android:

📱 iOS:

Logged out: cold start from a link to feed/1 lands on sign-in with the message, a link to sign-up opens normally, a link to sign-in shows no message, and a link to feed/add-post goes straight to sign-in without rendering the protected screen.

deeplink-protected-screens-v2.mp4

Return to the link after login: logged out, a link to feed/add-post opens sign-in with the message, and after signing in the app lands on Add Post. Recorded against a local mock of the sign-in endpoint.

deeplink-login-return-to.mp4

@fernandatoledo fernandatoledo added the risk:high Auth, payments, migrations, security-sensitive code. label Nov 26, 2026
@github-actions

github-actions Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

💯 Test Coverage

Lines Statements Branches Functions
Coverage: 74%
74.94% (625/834) 71.82% (283/394) 69.57% (231/332)

😎 Tests Results

Tests Skipped Failures Errors Time
404 0 💤 0 ❌ 0 🔥 33.198s ⏱️
👀 Tests Details • (74%)
File% Stmts% Branch% Funcs% LinesUncovered Line #s
All files74.9471.8269.5774.87 
src/app76.5797.0552.9476.57 
   _layout.tsx10095.45100100161
   sign-in.tsx100100100100 
src/app/(app)37.9337.523.0737.93 
   _layout.tsx78.571005078.5717–25
src/components97.4310087.597.43 
   login-form.tsx10010066.66100 

… routes

- Guards always send an authenticated user to `/`. PendingDeepLinkHandler then
  pushes the pending link on top of `(app)`, so the linked screen keeps a back stack.
- isReturnToPath matches an allow-list of protected routes, so unknown routes open
  the 404 instead of asking for sign-in. A test fails when a route is not classified.
- AuthProvider re-checks the stored token on foreground and when +native-intent
  finds it expired, so the guards and +native-intent agree on the session.
- runtimeVersion uses the fingerprint policy, so updates only reach builds that
  register the same scheme.
- Drop the AppState re-check: logging a user out on foreground with no notice
  is a product decision. Links still sync the session through syncSession().
- Restore the appVersion runtime policy. expo-updates is not installed, and the
  fingerprint hashes extra, which can strand OTAs built from a different env file.
- Make the unmount test fail when the session listener is not removed.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk:high Auth, payments, migrations, security-sensitive code.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant