go_router_guards
A flexible and powerful guard system for Go Router with middleware-style navigation control, enabling elegant route protection with expressive guard composition.
Quick Start
Install:
dependencies:
go_router_guards: ^2.0.0+1
Create a guard:
class AuthGuard extends RouteGuard {
@override
void onNavigation(
NavigationResolver resolver,
BuildContext context,
GoRouterState state,
) async {
final isAuthenticated = await checkAuth();
if (isAuthenticated) {
resolver.next();
} else {
resolver.redirect('/login');
}
}
}
Use with type-safe routes:
@TypedGoRoute<AdminRoute>(path: '/admin')
class AdminRoute extends GoRouteData with GuardedRoute {
@override
RouteGuard get guard => [
AuthGuard(),
RoleGuard(['admin']),
].all();
@override
Widget build(BuildContext context, GoRouterState state) {
return const AdminScreen();
}
}
Or with traditional GoRouter:
GoRoute(
path: '/admin',
redirect: [AuthGuard(), RoleGuard(['admin'])].redirectAll(),
builder: (context, state) => const AdminScreen(),
)
Documentation
📚 Full documentation available at https://guards.aquiles.dev
Migrating from v1.x
See the Migration Guide for detailed instructions on upgrading from v1.x to v2.x.
Quick summary of breaking changes:
// Before (v1.x)
class MyGuard implements RouteGuard {
@override
FutureOr<String?> redirect(BuildContext context, GoRouterState state) {
if (condition) return '/redirect';
return null;
}
}
// After (v2.x)
class MyGuard extends RouteGuard {
@override
void onNavigation(
NavigationResolver resolver,
BuildContext context,
GoRouterState state,
) {
if (condition) {
resolver.redirect('/redirect');
} else {
resolver.next();
}
}
}
Contributing
See CONTRIBUTING.md for details.
License
This project is licensed under the MIT License - see the LICENSE file for details.