go_router_guards 2.0.0+2 copy "go_router_guards: ^2.0.0+2" to clipboard
go_router_guards: ^2.0.0+2 copied to clipboard

A flexible and extensible guard system for Go Router that allows you to chain multiple guards together for route protection.

go_router_guards

go_router_guards #

ci coverage pub package style: very good analysis License: MIT

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:


Overview #

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.

Quick Start #

Installation #

dependencies:
  go_router_guards: ^2.0.0+2

Creating 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');
    }
  }
}

Using 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();
  }
}

Using with Traditional GoRouter #

GoRoute(
  path: '/admin',
  redirect: [AuthGuard(), RoleGuard(['admin'])].redirectAll(),
  builder: (context, state) => const AdminScreen(),
)

Documentation #

📚 Full documentation available at 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();
    }
  }
}

Examples #

  • Simple Example - A minimal example of using guards with authentication

Contributing #

See CONTRIBUTING.md for details.


Maintainers #


License #

This project is licensed under the MIT License - see the LICENSE file for details.

0
likes
160
points
4
downloads

Publisher

verified publisheraquiles.dev

Weekly Downloads

A flexible and extensible guard system for Go Router that allows you to chain multiple guards together for route protection.

Repository (GitHub)
View/report issues
Contributing

Documentation

API reference

License

MIT (license)

Dependencies

flutter, go_router, meta

More

Packages that depend on go_router_guards