Expo Router (tabs) inside (auth) cannot use hardcoded route

Please provide the following:

  1. SDK Version: 48
  2. Platforms(Android/iOS/web/all): all

I am starting a new project, and I would like to put this behind (auth) group, as suggested in the Expo Router Docs.

Here is changes I have pending: Add routes & tabs by lucksp · Pull Request #2 · lucksp/myflyid · GitHub

I am not sure if the committed code change is correct for the auth check hook:

function useProtectedRoute(user: User | null) {
  const segments = useSegments();
  const router = useRouter();

  useEffect(() => {
    const inAuthGroup = segments[0] === '(auth)';
    if (
      // If the user is not signed in and the initial segment is not anything in the auth group.
      !user &&
      !inAuthGroup
    ) {
      // Redirect to the sign-in page.
      router.replace('/sign-in');
    } else if (user && inAuthGroup) {
// without this block, if a user switches tabs, the route would be hardcoded and go back to tab 1 if the user selects any other tab
      if (segments.includes('(tabs)')) {
        router.replace(`/${segments[segments.length - 1]}`);
      } else {
        router.replace('(tabs)');
      }
      // Redirect away from the sign-in page.
    }
  }, [user, segments, router]);
}

Also, I am not sure when to use a Stack or a Slot, even with the docs.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.