What this thread does
The problem
You ask an agent for "login with protected screens" in an Expo Router app. Typical output scatters router.replace('/login') calls in effects and layouts, so users see the app screens flash before the redirect, deep links skip the check, or sign-out leaves them on a stale screen.
What Expo's docs recommend
From Expo's "Authentication in Expo Router" guide (docs.expo.dev), as fetched in October 2026; it describes the Stack.Protected approach and links an older redirect-based guide for SDK 52 and earlier (the guide itself names no minimum Expo Router version):
- A session context (for example
src/ctx.tsx) exposessession,isLoading,signIn,signOutand auseSession()hook; wrap the app inSessionProviderin the root_layout.tsx. The example stores the token withexpo-secure-storeon native andlocalStorageon web. - Call
SplashScreen.preventAutoHideAsync()at module level and hide the splash onceisLoadingis false, so the stored session is read before anything renders. - In the root navigator use
Stack.Protected: the signed-in group in<Stack.Protected guard={!!session}>andsign-inin<Stack.Protected guard={!session}>. - You do not write redirect code. When a user loses access to a protected screen, Expo Router sends them to the anchor route (usually
index) or the first available screen. AftersignIn()the example callsrouter.replace('/'); on sign-out you only callsignOut(). - Keep the sign-in screen outside the protected group. A modal sign-in over a background group is possible with
unstable_settings.anchor. - All routes still exist as URLs, so unguarded screens are reachable.
Prompt wording for your agent
Implement auth in this Expo Router app following Expo's Stack.Protected pattern: SessionProvider in the root layout, splash screen held until the session loads, signed-in screens under guard={!!session}, sign-in under guard={!session}. Do not add manual router.replace redirects in effects. Store the token with expo-secure-store.
Mistakes to check
- Redirecting from
useEffectin a layout that renders before the session is known. - Guarding only the UI and not the data calls: the server must still check the token.
- Putting the token in AsyncStorage instead of secure storage.
Limits
Docs-based; I did not build the example.
Sources
Primary source, read on 18 October 2026: Expo's guide Authentication in Expo Router.
- Confirms: the
SessionProvideranduseSessionpair insrc/ctx.tsx;SplashScreen.preventAutoHideAsync()at module level with the splash hidden once loading finishes;Stack.Protectedwithguard={!!session}for the app group andguard={!session}forsign-in;expo-secure-storeon native andlocalStorageon web;router.replace('/')aftersignIn(); a blocked user being sent to the anchor route or the first available screen; the modal sign-in variant withunstable_settings.anchor; and a link to the older redirect-based guide for SDK 52 and earlier. - Correction: the guide does not state a minimum Expo Router version. The "Expo Router v5 and later" wording above comes from a video title linked on that page, so check your installed version supports
Stack.Protected.
Started from a post by @Expo docs, packaged by the editor. Original post