GoRouter Auth Redirect
core/routing/app_router.dart · redirect()
Overview
The single redirect function in goRouter performs the entire auth funnel: not-logged-in → /login; logged-in but profileSetupComplete=false → /onboarding_register(_seller); ToS not accepted → /terms; seller-only routes hidden when !hasSellerRole. It is the de-facto auth state machine.
User Flow
- On every navigation, fetches isLoggedIn, isProfileSetupComplete, hasSellerRole, isPendingSellerRegistration, ToS status.
- Splash route is always allowed through (initial loading screen).
- Unauthenticated + auth route → pass; unauthenticated + anything else → /login.
- Authenticated + incomplete profile → /onboarding_register or /onboarding_register_seller (based on pending flag).
- Authenticated + complete + ToS pending → /terms; if already there → pass.
- Authenticated + on auth/onboarding route → bounce to /home.
- Authenticated + seller-only path + !hasSellerRole → /home.
Cases & Edge Cases
- _hasConsentedToS is a module-level memoization to stop the redirect spamming /tos/latest on every frame while the user is parked on the ToS screen — without it, the router enters a backend-pinging loop.
- isPendingSellerRegistration picks which onboarding wizard to route to, branching what is otherwise a uniform onboarding flow.
- isEditPrestationRoute uses a regex on /prestation/:id/edit because GoRouter does not expose the route name during the redirect phase.
Code References
- lib/src/core/routing/app_router.dart:76 // _checkIsLoggedIn
- lib/src/core/routing/app_router.dart:94 // _verifyTosConsent (memoized)
- lib/src/core/routing/app_router.dart:131 // GoRouter.redirect
- lib/src/core/routing/routes.dart // AppRoute enum
API Calls
Empty — fill this in later. Open the manifest and add content here.
Notes
Every redirect awaits SharedPreferences reads serially — adding more checks here will slow first-frame navigation. The pattern is to push new gates into _verifyTosConsent-style memoized helpers.