go_router_auth 0.1.0 copy "go_router_auth: ^0.1.0" to clipboard
go_router_auth: ^0.1.0 copied to clipboard

A simple auth guard for GoRouter. Handles login redirect, public routes, loading state, and protected UI widgets. Framework-agnostic.

go_router_auth #

A simple auth guard for GoRouter. Handles login redirect, public routes, loading state, and protected UI widgets.

Framework-agnostic — works with GetX, Riverpod, Bloc, or plain state management.

Features #

  • Auto-redirect to login — when not authenticated
  • Auto-redirect from login — to home when already logged in
  • Public routes — whitelist routes that don't require auth
  • Loading state — prevents redirects while checking auth on startup
  • ChangeNotifier — GoRouter's refreshListenable updates automatically
  • ProtectedRoute widget — conditionally show/hide protected UI
  • AuthStateBuilder widget — switch UI based on auth state

Getting started #

dependencies:
  go_router_auth: ^0.1.0
flutter pub get

Usage #

1. Create the guard #

import 'package:go_router_auth/go_router_auth.dart';

final authGuard = AuthGuard(
  isLoggedIn: () => myAuthService.isLoggedIn,
  loginPath: '/login',
  homePath: '/dashboard',
  publicRoutes: ['/login', '/register', '/forgot-password'],
);

2. Wire to GoRouter #

final router = GoRouter(
  initialLocation: '/dashboard',
  redirect: authGuard.redirect,
  refreshListenable: authGuard,
  routes: [
    GoRoute(path: '/login', builder: (_, __) => LoginPage()),
    GoRoute(path: '/register', builder: (_, __) => RegisterPage()),
    GoRoute(path: '/dashboard', builder: (_, __) => DashboardPage()),
    GoRoute(path: '/profile', builder: (_, __) => ProfilePage()),
  ],
);

3. Set loading state (on app start) #

// While checking stored token
authGuard.setLoading(true);
await myAuthService.checkToken();
authGuard.setLoading(false);

4. Notify on login/logout #

// After successful login
await myAuthService.login(email, password);
authGuard.notify();  // GoRouter re-evaluates redirect → goes to homePath

// After logout
await myAuthService.logout();
authGuard.notify();  // GoRouter re-evaluates redirect → goes to loginPath

5. ProtectedRoute widget (conditional UI) #

ProtectedRoute(
  authGuard: authGuard,
  child: ProfileCard(user: user),
  unauthenticatedBuilder: (context) => LoginPromptCard(),
)

6. AuthStateBuilder widget (auth-based UI switching) #

AuthStateBuilder(
  authGuard: authGuard,
  authenticated: (context) => DashboardPage(),
  unauthenticated: (context) => LoginPage(),
  loading: (context) => SplashScreen(),
)

API #

AuthGuard #

Member Type Description
redirect(context, state) String? GoRouter redirect callback
notify() void Trigger redirect re-evaluation
isLoading() bool Whether guard is in loading state
setLoading(value) void Set loading state (prevents redirects)

Constructor #

Parameter Type Default Description
isLoggedIn bool Function() (required) Auth status checker
loginPath String (required) Redirect target for unauthenticated users
homePath String? null Redirect target after login
publicRoutes List<String> [] Routes that bypass auth check
onUnauthenticated void Function()? null Called when redirecting to login
onAuthenticated void Function()? null Called when redirecting from login

ProtectedRoute #

Parameter Type Description
authGuard AuthGuard The auth guard instance
child Widget Widget shown when authenticated
unauthenticatedBuilder WidgetBuilder? Widget shown when not authenticated

AuthStateBuilder #

Parameter Type Description
authGuard AuthGuard The auth guard instance
authenticated WidgetBuilder Widget for authenticated state
unauthenticated WidgetBuilder Widget for unauthenticated state
loading WidgetBuilder? Widget for loading state

License #

MIT

0
likes
150
points
23
downloads

Documentation

API reference

Publisher

verified publisherciptaantaradigital.com

Weekly Downloads

A simple auth guard for GoRouter. Handles login redirect, public routes, loading state, and protected UI widgets. Framework-agnostic.

Repository (GitHub)
View/report issues

Topics

#go-router #authentication #routing #guard #navigation

License

MIT (license)

Dependencies

flutter, go_router

More

Packages that depend on go_router_auth