-
Notifications
You must be signed in to change notification settings - Fork 0
feat(routing): re-run redirects on auth-state change #105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4cf6494
feat(routing): re-run redirects on auth-state change
anilcancakir 45225bd
docs(changelog): note the auth-state redirect refresh
anilcancakir 6fc3430
style: dart format
anilcancakir d2177ef
fix(routing): dispose the GoRouter on reset to release the refresh li…
anilcancakir 1109e6f
fix(routing): log the stack trace when auth-notifier resolution fails
anilcancakir ade9d85
test(routing): cover the login-direction redirect
anilcancakir File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| import 'package:flutter/material.dart'; | ||
| import 'package:flutter_test/flutter_test.dart'; | ||
| import 'package:magic/magic.dart'; | ||
|
|
||
| /// Tests for wiring the GoRouter `refreshListenable` to the auth guard's | ||
| /// `stateNotifier`. | ||
| /// | ||
| /// Background: a passive 401 (expired or revoked token) routes through | ||
| /// `AuthInterceptor.onError`, which logs the user out when the refresh fails. | ||
| /// Logout bumps the guard's `stateNotifier`. With the notifier wired as the | ||
| /// router's refresh signal, GoRouter re-runs its redirect chain on that bump, | ||
| /// ejecting the user from a protected screen to the login route without an | ||
| /// explicit navigation call. | ||
| /// | ||
| /// These tests read live auth state inside the middleware (rather than a fixed | ||
| /// boolean), so they exercise the actual re-evaluation trigger and prove the | ||
| /// eject happens purely from a state change. | ||
| void main() { | ||
| setUpAll(() { | ||
| TestWidgetsFlutterBinding.ensureInitialized(); | ||
| }); | ||
|
|
||
| setUp(() { | ||
| MagicApp.reset(); | ||
| Magic.flush(); | ||
| TitleManager.reset(); | ||
| MagicRouter.reset(); | ||
| Kernel.flush(); | ||
| Gate.manager.flush(); | ||
| Log.fake(); | ||
| }); | ||
|
|
||
| tearDown(() { | ||
| Auth.unfake(); | ||
| }); | ||
|
|
||
| testWidgets('a passive logout re-runs redirects and ejects to /login', ( | ||
| tester, | ||
| ) async { | ||
| // 1. Start authenticated: the protected route is reachable. | ||
| Auth.fake(user: _fakeUser()); | ||
| Kernel.register('auth', () => _LiveAuthGuard()); | ||
| Kernel.register('guest', () => _LiveGuestGuard()); | ||
|
|
||
| MagicRoute.page('/', () => const Text('dashboard')).middleware(['auth']); | ||
| MagicRoute.page('/login', () => const Text('login')).middleware(['guest']); | ||
|
|
||
| await tester.pumpWidget( | ||
| MaterialApp.router(routerConfig: MagicRouter.instance.routerConfig), | ||
| ); | ||
| await tester.pumpAndSettle(); | ||
|
|
||
| expect(MagicRouter.instance.currentPath, '/'); | ||
|
|
||
| // 2. Simulate the passive 401 -> AuthInterceptor logout. This only bumps | ||
| // the guard's stateNotifier; there is no explicit navigation here. | ||
| await Auth.logout(); | ||
| await tester.pumpAndSettle(); | ||
|
|
||
| // 3. The router re-evaluated the redirect chain off the state change and | ||
| // ejected the now-guest user to the login route. | ||
| expect(MagicRouter.instance.currentPath, '/login'); | ||
| }); | ||
|
|
||
| testWidgets('a logged-out user resting on /login does not loop', ( | ||
| tester, | ||
| ) async { | ||
| // Guest (no user) sitting on the guest route: the guest middleware must | ||
| // return null (allow), so the refresh-driven re-evaluation stays put and | ||
| // go_router never exceeds its redirect budget. | ||
| Auth.fake(); | ||
| Kernel.register('auth', () => _LiveAuthGuard()); | ||
| Kernel.register('guest', () => _LiveGuestGuard()); | ||
|
|
||
| MagicRoute.page('/', () => const Text('dashboard')).middleware(['auth']); | ||
| MagicRoute.page('/login', () => const Text('login')).middleware(['guest']); | ||
|
|
||
| MagicRouter.instance.setInitialLocation('/login'); | ||
|
|
||
| await tester.pumpWidget( | ||
| MaterialApp.router(routerConfig: MagicRouter.instance.routerConfig), | ||
| ); | ||
| await tester.pumpAndSettle(); | ||
|
|
||
| expect(MagicRouter.instance.currentPath, '/login'); | ||
|
|
||
| // A spurious state bump (e.g. a failed restore) must not start a loop. | ||
| Auth.stateNotifier.value++; | ||
| await tester.pumpAndSettle(); | ||
|
|
||
| expect(MagicRouter.instance.currentPath, '/login'); | ||
| }); | ||
|
|
||
| testWidgets('a login on /login re-runs redirects and leaves for /', ( | ||
| tester, | ||
| ) async { | ||
| // The other half of the auth transition: a guest resting on the guest-only | ||
| // login route logs in, and the refresh-driven re-evaluation should send the | ||
| // now-authenticated user home with no explicit navigation call. | ||
| Auth.fake(); | ||
| Kernel.register('auth', () => _LiveAuthGuard()); | ||
| Kernel.register('guest', () => _LiveGuestGuard()); | ||
|
|
||
| MagicRoute.page('/', () => const Text('dashboard')).middleware(['auth']); | ||
| MagicRoute.page('/login', () => const Text('login')).middleware(['guest']); | ||
|
|
||
| MagicRouter.instance.setInitialLocation('/login'); | ||
|
|
||
| await tester.pumpWidget( | ||
| MaterialApp.router(routerConfig: MagicRouter.instance.routerConfig), | ||
| ); | ||
| await tester.pumpAndSettle(); | ||
|
|
||
| expect(MagicRouter.instance.currentPath, '/login'); | ||
|
|
||
| // Log in: the guard's stateNotifier bumps, the router re-evaluates, and the | ||
| // guest guard now sends the authenticated user home. | ||
| await Auth.login(const <String, dynamic>{'token': 'tok'}, _fakeUser()); | ||
| await tester.pumpAndSettle(); | ||
|
|
||
| expect(MagicRouter.instance.currentPath, '/'); | ||
| }); | ||
| } | ||
|
|
||
| /// Auth guard reading live auth state: redirects a guest off protected routes. | ||
| class _LiveAuthGuard extends MagicMiddleware { | ||
| @override | ||
| String? redirectTarget(String location) { | ||
| if (Auth.guest && location != '/login') return '/login'; | ||
| return null; | ||
| } | ||
|
|
||
| @override | ||
| Future<void> handle(void Function() next) async => next(); | ||
| } | ||
|
|
||
| /// Guest guard reading live auth state: redirects an authenticated user home. | ||
| class _LiveGuestGuard extends MagicMiddleware { | ||
| @override | ||
| String? redirectTarget(String location) { | ||
| if (Auth.check() && location != '/') return '/'; | ||
| return null; | ||
| } | ||
|
|
||
| @override | ||
| Future<void> handle(void Function() next) async => next(); | ||
| } | ||
|
|
||
| /// Minimal authenticated user for the fake auth manager. | ||
| class _FakeUser extends Model with Authenticatable { | ||
| @override | ||
| String get table => 'users'; | ||
|
|
||
| @override | ||
| String get resource => 'users'; | ||
|
|
||
| @override | ||
| List<String> get fillable => ['id', 'name']; | ||
| } | ||
|
|
||
| _FakeUser _fakeUser() { | ||
| final user = _FakeUser(); | ||
| user.fill({'id': 1, 'name': 'Alice'}); | ||
| user.exists = true; | ||
| return user; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.