Skip to content
Route Guards - Security shield protecting navigation paths

Route Guards

Middleware-style route protection for modern applications.

Route Guards provide middleware-style navigation protection for your applications. Think of them as security checkpoints that can allow, redirect, or block navigation based on your custom logic.

🛡️ Middleware Pattern

Clean, composable guards that intercept navigation attempts before they reach your screens.

🔧 Framework Agnostic

Core guard logic works with any routing system. Framework-specific integrations provide seamless developer experience.

⚡ Composable Logic

Combine simple guards into complex protection schemes with logical operators: AND, OR, XOR.

🎯 Precise Control

Fine-grained control over which routes are protected, with inclusion/exclusion patterns and conditional logic.

Every guard implements a simple middleware pattern:

class AuthGuard extends RouteGuard {
@override
FutureOr<void> onNavigation(
NavigationResolver resolver,
Object context,
Object state,
) async {
if (await isAuthenticated()) {
resolver.next(); // ✅ Allow navigation
} else {
resolver.redirect('/login'); // 🔀 Redirect to login
}
}
}

Guards receive a resolver that controls the navigation flow:

  • resolver.next() - Allow navigation to continue
  • resolver.redirect('/path') - Redirect to a different route
  • resolver.block() - Block navigation entirely

Framework-agnostic foundation providing:

  • Base RouteGuard class
  • NavigationResolver pattern
  • Guard composition utilities
  • Works with any router

Specialized integrations for popular frameworks:

  • go_router_guards - Flutter Go Router integration
  • More integrations coming soon…

Combine guards to create sophisticated protection:

// All guards must pass (AND logic)
final adminGuard = Guards.all([
AuthGuard(),
RoleGuard(['admin']),
PermissionGuard(['manage_users']),
]);
// Any guard can pass (OR logic)
final accessGuard = Guards.anyOf([
SubscriptionGuard(),
StaffAccessGuard(),
BetaTesterGuard(),
]);
// Exactly one guard must pass (XOR logic)
final exclusiveGuard = Guards.oneOf([
DevelopmentModeGuard(),
ProductionAccessGuard(),
]);

Authentication

Protect authenticated routes, handle login flows, manage session validation.

Authorization

Role-based access control, permission systems, organizational hierarchies.

Business Logic

Subscription gates, feature flags, trial limitations, geographic restrictions.

Security

Rate limiting, device verification, suspicious activity detection, compliance checks.

Multi-Tenant

Tenant isolation, cross-tenant security, workspace-specific access control.

A/B Testing

Experiment-based routing, feature rollouts, user segmentation.

Here’s how it looks with Go Router integration:

// Type-safe route with guards
@TypedGoRoute<AdminRoute>(path: '/admin')
class AdminRoute extends GoRouteData with GuardedRoute {
@override
RouteGuard get guards => Guards.all([
AuthGuard(),
RoleGuard(['admin']),
]);
@override
Widget build(context, state) => AdminScreen();
}
// Traditional route with guards
GoRoute(
path: '/admin',
builder: (context, state) => AdminScreen(),
redirect: RouteGuardUtils.createGuardRedirect(
Guards.all([AuthGuard(), RoleGuard(['admin'])]),
),
)
  • 🔒 Security First: Fail securely with proper error handling
  • 📦 Reusable: Write once, use everywhere
  • 🧪 Testable: Clean interfaces make testing straightforward
  • ⚡ Performant: Efficient execution with caching support
  • 🛠️ Developer Friendly: Clear patterns and comprehensive documentation
  • 🌐 Production Ready: Used in real applications with enterprise requirements

Route Guards - Secure navigation made simple