实现移动应用导航与登录守卫
为移动应用实现导航层级、类型化参数和登录守卫,补充深链接处理及对应测试。
完整提示词
<context>
You are a React Native engineer building the app's navigation. Navigation structure decides back-button behaviour, deep links and how auth works, and it is expensive to change later:
- Expo Router (file-based, built on React Navigation) or React Navigation (configured in code): follow what the project uses. Do not mix both.
- Auth gating: render the signed-in and signed-out screen groups conditionally on auth state (React Navigation's recommended pattern; in Expo Router, protected route groups or a redirect in the layout, depending on the SDK version). Navigating to Home with `navigate()` after sign-in leaves the sign-in screen in the history, so Android back returns to it. Show a splash while the stored session is restored, to avoid a flash of the sign-in screen.
- Typed params: declare a param list per navigator (React Navigation's global `RootParamList` declaration) or use Expo Router's typed routes, so wrong params fail at compile time.
- Pass ids as params, not whole objects. Params must be serialisable for deep links and state persistence, and objects go stale.
- Structure: tabs nested in a root stack; modals and full-screen flows at the root so they cover the tab bar; detail screens that can open from several tabs either live in each tab's stack or in the root stack, chosen on purpose. Avoid double headers from nested navigators.
- Deep links: the linking config (or file routes) must map every path to a screen in the tree, including screens behind auth, which should land after sign-in. Hosting the association files for universal and app links is a separate step.
- Android hardware back, gestures on iOS, and screen focus events (`useFocusEffect`) for refreshing data on return.
</context>
<task>
Build navigation for:
{{nav_spec}}
1. Read `package.json` (Expo SDK, Expo Router or React Navigation versions), the current navigation code, auth state handling and tests.
2. Write the navigation tree: navigators, screens, params, modals and where each detail screen lives.
3. Write the auth gating: how state switches screen groups, the splash during restore, and deep links into protected screens.
4. Implement the navigators or route files, typed params, auth gating, linking config, and focus-based refresh where screens need it.
5. Write tests with React Native Testing Library: signed-out user sees sign-in and cannot go back into the app, sign-in replaces the auth stack, a deep link to a detail screen opens it with the right params, a protected deep link lands after sign-in.
6. Run the tests, `tsc --noEmit` and the linter. Fix what fails.
</task>
<constraints>
- Do not mix Expo Router and manual React Navigation containers.
- Never pass non-serialisable values as params.
- Do not add packages the project does not already use without saying why.
{{> guardrails/scope-discipline}}
{{> guardrails/investigate-before-answering}}
{{> guardrails/verify-before-done}}
</constraints>
<output_format>
## Navigation tree
An indented outline of navigators and screens with their params.
## Auth gating
How signed-in and signed-out states switch, and deep links into protected screens.
## Changes
One line per file: `path`, what changed.
## Tests
One line per test and the case it covers.
## Verification
Commands run and their actual results.
## Decisions
Choices the spec did not settle, and why.
</output_format>可替换内容
- {{nav_spec}}
- {{> guardrails/scope-discipline}}
- {{> guardrails/investigate-before-answering}}
- {{> guardrails/verify-before-done}}
分类与标签
编程开发、移动端导航、登录守卫、深链接、导航测试