Auth-protected routes via GoRouter redirect
guarding routes with redirect.
a top-level redirect reads auth state, sends unauthenticated users to login, sends logged-in users away from login, and uses refreshListenable to re-evaluate on auth change.
What's really being asked
Whether you can express authentication guarding declaratively in the router and avoid the classic pitfalls of redirect loops and stale state.
The full answer
GoRouter accepts a top-level redirect function called before navigation resolves. Inside it you read your auth state, often from a notifier or repository, and inspect the target location via the GoRouterState. The logic returns a path to redirect to, or null to proceed. The core rules: if the user is not authenticated and is not already going to the login or signup route, return the login path; if the user is authenticated but currently on the login route, return the home path; otherwise return null. To make the router re-evaluate when login or logout happens, pass a refreshListenable, typically a Listenable that pulses on auth changes, so the redirect runs again and moves the user appropriately.
The mistakes people make
Forgetting to exempt the login route, which creates an infinite redirect loop. Putting the guard in a widget build method and calling context.go manually, which races with the router. Reading auth state once at startup so logout never redirects. Returning the same location you are on.
What usually comes next
Why refreshListenable matters. How do you preserve the originally requested location to return after login. Per-route versus top-level redirect. How do you avoid loops.
A concrete example
You create GoRouter with a redirect that does: final loggedIn = auth.isLoggedIn; final goingToLogin = state.matchedLocation == '/login'; if not loggedIn and not goingToLogin return '/login'; if loggedIn and goingToLogin return '/home'; return null. You pass refreshListenable: auth, a ChangeNotifier that notifies on sign-in and sign-out. Now visiting /profile while logged out lands on /login, and signing in pulses the listenable so the router re-runs and forwards to /home automatically.
Interview question
Why must a GoRouter auth redirect exempt the login route when redirecting unauthenticated users?
- a.Login routes are always public and ignore redirect entirely
- b.Without the exemption, redirecting to login would itself trigger another redirect, looping foreverCorrect
- c.refreshListenable only fires on the login route
- d.The login route cannot be matched by GoRouterState otherwise
Why? this is the answer
If the redirect sends unauthenticated users to login without checking that they are already heading there, navigating to login re-triggers the redirect endlessly. Exempting the login target breaks the loop; login routes are not auto-exempt from redirect.
Just read this? Test yourself on what you have been reading.
Read the original → docs.page
- #flutter
- #go_router
- #authentication
- #redirect
- #routing
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on flutter — each one lists the topics its interview covers.
See open roles