A flexible and powerful guard system for Go Router with middleware-style navigation control, enabling elegant route protection with expressive guard composition.
Learn more at guards.aquiles.dev!
This package is built to work with:
The goal of this package is to make it easy to implement navigation guards with Go Router. Navigation guards allow you to protect routes based on custom logic, such as authentication status, user roles, or permissions. Following the guard pattern facilitates testability and reusability of navigation logic.
This package provides a middleware-style approach to route protection, allowing developers to compose multiple guards together and focus on writing clear, declarative navigation rules.
dependencies:
go_router_guards: ^2.0.0+2class 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');
}
}
}@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();
}
}GoRoute(
path: '/admin',
redirect: [AuthGuard(), RoleGuard(['admin'])].redirectAll(),
builder: (context, state) => const AdminScreen(),
)📚 Full documentation available at guards.aquiles.dev
See the Migration Guide for detailed instructions on upgrading from v1.x to v2.x.
// 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();
}
}
}- Simple Example - A minimal example of using guards with authentication
See CONTRIBUTING.md for details.
This project is licensed under the MIT License - see the LICENSE file for details.