A guard needs a boolean, and in a real app that boolean depends on a session token that is read from storage, asynchronously, when the app starts. Until the token has loaded, the app can't tell which guards should pass, so the session, the splash screen and the guards are wired together in the root layout.

Key terms

  • SplashScreen: the namespace exported by expo-router that holds preventAutoHideAsync, hide and hideAsync.
  • SplashScreen.preventAutoHideAsync: stops Expo Router from hiding the native splash screen on its own, so it stays up until you hide it.
  • SplashScreen.hide: hides the native splash screen immediately.
  • redirectTo: the optional Protected prop that sends a user whose guard fails to a specific href.

The session sits above the navigator

Start with a React context that exposes the session to the whole app. The examples here import AuthProvider and useAuth from an auth-context.tsx module whose context value has this shape:

tsx
{ token: string | null; isRestoring: boolean; signIn: () => void; signOut: () => void }

isRestoring stays true until the stored token has been read. How you store the token is up to you, for example with expo-secure-store on native and local storage on web. Make useAuth throw when a component calls it outside the provider, with a message such as useAuth() needs an <AuthProvider> above it, so a provider rendered in the wrong place fails loudly.

Put the provider in the root layout, so every route can read the session. In the root layout example, RootLayout renders AuthProvider, and the Stack lives in a separate StoreNavigator component rendered inside the provider, so that it can call useAuth().

StoreNavigator uses two guards with opposite conditions. guard={!!token} wraps the screens for signed-in users, and guard={!token} wraps sign-in, so a signed-in user can't open the sign-in screen again.

When the signed-in screens share a layout, you can move them into an (app) route group, a folder with its own _layout.tsx, and guard the whole group in the root layout as a single screen, <Stack.Screen name="(app)" />. Keep sign-in out of that folder, so the group's layout doesn't run when the sign-in screen renders.

The sign-in screen calls signIn() and then router.replace('/'), with router from useRouter(), which SDK 58 prefers over the module-level router inside components. Signing out needs no navigation call. After signOut(), the guard around orders and account fails and sends the user to /sign-in. The same guards cover deep links: a signed-out visitor who opens a link to a guarded route is redirected too.

Holding the splash screen

Expo Router hides the splash screen automatically once its root has rendered. Loading the session is asynchronous, so keep the splash screen visible until it has loaded.

First, splash-gate.tsx calls SplashScreen.preventAutoHideAsync() at the top level of the module, without await. It belongs in global scope. Called inside a component or hook, it might run too late, after the splash screen is already hidden.

Second, SplashGate reads isRestoring and hides the splash screen when loading is done:

tsx
if (!isRestoring) {
  SplashScreen.hide();
}

It returns null and calls useAuth(), so it must render inside AuthProvider. hide() takes the splash screen down immediately, which means the first screen should be ready to draw when it runs. Hide it as soon as the session is known instead of waiting on other work.

Choosing the destination with redirectTo

The default redirect goes to the navigator's anchor or its first accessible route. In this store, that is index, the public home screen, which is not where a signed-out user who tapped Orders should end up. redirectTo names the destination instead:

tsx
<Stack.Protected guard={!!token} redirectTo="/sign-in">

redirectTo arrived in SDK 58. It accepts the same string or object href as Link, and its type is Href, the type of Link's href prop. An explicit redirectTo wins over the navigator's default. When guards are nested, the redirectTo of the guard that fails applies. If an inner guard also fails, its own redirectTo replaces the outer one, and an inner guard without one falls back to the navigator's default.

You may find older layouts that set redirect on a Stack.Screen. In SDK 58, layout Screen components no longer accept redirect or initialParams. Render a Redirect from the route file to forward a route, and use Protected with redirectTo for access control.

Code Examples

tsx
import { Stack } from 'expo-router';

import { AuthProvider, useAuth } from '../auth-context';
import { SplashGate } from '../splash-gate';

export default function RootLayout() {
  return (
    <AuthProvider>
      <SplashGate />
      <StoreNavigator />
    </AuthProvider>
  );
}

// A separate component, so it renders inside AuthProvider and can read the token.
function StoreNavigator() {
  const { token } = useAuth();

  return (
    <Stack>
      <Stack.Screen name="index" />

      <Stack.Protected guard={!!token} redirectTo="/sign-in">
        <Stack.Screen name="orders" />
        <Stack.Screen name="account" />
      </Stack.Protected>

      <Stack.Protected guard={!token}>
        <Stack.Screen name="sign-in" />
      </Stack.Protected>
    </Stack>
  );
}
tsx
import { SplashScreen } from 'expo-router';

import { useAuth } from './auth-context';

SplashScreen.preventAutoHideAsync();

export function SplashGate() {
  const { isRestoring } = useAuth();

  if (!isRestoring) {
    SplashScreen.hide();
  }

  return null;
}
tsx
import { useRouter } from 'expo-router';
import { Button, View } from 'react-native';

import { useAuth } from '../auth-context';

export default function SignInScreen() {
  const { signIn } = useAuth();
  const router = useRouter();

  return (
    <View style={{ flex: 1, justifyContent: 'center', padding: 16 }}>
      <Button
        title="Sign in"
        onPress={() => {
          signIn();
          // Make sure sign-in succeeded before navigating in a real app.
          router.replace('/');
        }}
      />
    </View>
  );
}
✓ Completed