You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
feat(auth): protect deeplinked screens and return to them after login - #183
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.
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
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.
… 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.
… fix sonar gate
- Store and push the normalized path from toReturnToPath(), so the router
opens the screen that was validated (/%28app%29/settings opens /settings).
- Move the link handling into rememberProtectedLink() and call it once at
startup on web, where +native-intent does not run.
- Turn PendingDeepLinkHandler into the useOpenPendingDeepLink hook, clearing
the typescript:S100 issue that pushed new_minor_violations over the gate.
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
Active issues can clear protected links before sign-in opens on public screens and misinterpret /--/ in queries or fragments, losing the intended post-login destination.
The reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
First-time users with a stored valid session can bypass onboarding through protected links because the native handler and root authenticated guard do not enforce onboarding precedence.
No open issues 🎉
1 previously missed issue
In code that hasn't changed since last review
Enforce onboarding before honoring protected links
src/app/+native-intent.tsx:14
ai:serious Onboarding is bypassed when isFirstTime is true but a valid session is stored: this early return forwards a protected link unchanged, and the root authenticated guard allows standalone screens such as feed/add-post without checking onboarding. Unlike the tabs, those screens have no onboarding redirect. Check onboarding before accepting a protected link and make the root guard exclude first-time users so native and web both honor onboarding precedence.
Addressed the onboarding-precedence finding from the latest Copilot review (src/app/+native-intent.tsx:14) in 11d7652:
rememberProtectedLink now checks onboarding before the session: on first launch a protected link goes to /onboarding and is not remembered, even when a valid session is stored.
The root guard for authenticated screens is now isAuthenticated && !isFirstTime, so standalone screens like feed/add-post and feed/[id] redirect to onboarding for first-time users on both native and web. After onboarding, the existing /sign-in redirect sends a logged-in user home.
Added tests for a first-time user with a stored session, for both the native intent and the root guards.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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]andfeed/add-postmove under the authenticatedStack.Protectedguard (they were reachable without a session), and the guarded screen lists now live insrc/lib/auth/deep-link-path.tsand are rendered from there. The app scheme is also suffixed per environment (RootstrapApp.development,.qa,.staging; production staysRootstrapApp) 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?
src/app/+native-intent.tsxrecords 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/onboardingon first launch) so the protected screen is never pushed. The session check is shared withAuthProviderthroughhasValidSession(), and when a link finds the stored token expired,syncSession()makes a runningAuthProviderdrop it too, so the guards and+native-intentagree. Only links that resolve to a protected route are remembered:isReturnToPath()matches an allow-list built fromAUTHENTICATED_SCREENSandAPP_TAB_SCREENS, so public, auth and unknown routes open unchanged. A test walkssrc/appand 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.src/app/_layout.tsx: the guards always send an authenticated user to/, andPendingDeepLinkHandlerthen 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 (LoginFormgets an optionalnotice). 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).guides/navigation.mdx,agent_docs/architecture.mdandREVIEW.md.Layer 2: Risk Classification
Author applies a risk label when opening the PR. When in doubt, escalate.
risk:lowrisk:mediumrisk:highSetup
risk:low,risk:medium,risk:highlabels in the repoHow did you test this?
pnpm check-allpasses (lint, type-check, translations, 45 suites / 404 tests).APP_ENV=<env> npx expo config --type publicshows the expected scheme for each environment. On the iOS Simulator with a development build (old schemeRootstrapApp) and Maestro:feed/1andfeed/add-postland on sign-in with the message, both on cold start and with the app running;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 forfeed/1→ Post;logged in with a 60-second token left to expire with the app in the foreground, a link to
feed/add-postgoes 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:
+native-intentonly 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'srenderRoutercan't run in this repo (transitivereact-test-renderer19.1 vs React 19.3), so the guard tests mockStack.Protected. A follow-up could alignreact-test-rendererand add an end-to-end router test for the post-login redirect.Screenshots / Previews
Logged out: cold start from a link to
feed/1lands on sign-in with the message, a link tosign-upopens normally, a link tosign-inshows no message, and a link tofeed/add-postgoes 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-postopens 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