Flutter: Autoroute : RouteGuard not working in AutoTabsScaffold

2.2k Views Asked by At

I am trying add auth guard for my AutoTabsScaffold [bottom nav] but it's not working, It's working in other navigation pages but not just inside my landing page [where AutoTabsScaffold| Bottom Nav resides] am I missing something here?

using : auto_route: ^3.2.4 [pub.dev]

class LandingPage

   return AutoTabsScaffold(

      backgroundColor: Theme
          .of(context)
          .scaffoldBackgroundColor,
        routes: const [
          HomeRouter(),
          CategoryRouter(),
          OrderRouter(),
          ProfileRoute(),
      ],
      bottomNavigationBuilder: (_, tabsRouter) {return}

class auth guard:

class RouteGuard extends AutoRedirectGuard {
  final AuthService authService;

  RouteGuard(this.authService) {
    authService.addListener(() {
      if (!authService.authenticated) {
        // should be called when the logic effecting this guard changes
        // e.g when the user is no longer authenticated
        reevaluate();
      }
    });
  }

  @override
  void onNavigation(NavigationResolver resolver, StackRouter router) {
    if (authService.authenticated) return resolver.next();
    router.push(
      LoginRoute(
        onLoginCallback: (_) {
          resolver.next();
          router.removeLast();
        },
      ),
    );
  }
}

router.dart

@MaterialAutoRouter(
  replaceInRouteName: 'Page|Screen,Route',
  routes: <AutoRoute>[
    AutoRoute(page: OrderPreviewPage),
    AutoRoute(page: AddCardPage,),
    AutoRoute(page: PaymentPage,),
    AutoRoute(page: SplashPage, initial: true),
    AutoRoute(page: MyCartPage,),
    AutoRoute(page: IntroPage),
    AutoRoute(page: RegisterPage),
    AutoRoute(page: ProductDetailPage),
    AutoRoute(page: ProductListingPage),
    AutoRoute(page: CartSummaryPage,),
    AutoRoute(page: LoginPage, path: 'login'), //name: 'LoginRoute'
    AutoRoute(
      initial: true,
      page: LandingPage,

      path: 'landing',
      children: [
        AutoRoute(
          path: '',
          name: 'homeRouter',
          page: HomePage,
        ),
        AutoRoute(
          path: 'category',
          name: 'categoryRouter',
          page: CategoryPage,
        ),
        AutoRoute(
          path: 'orders',
          name: 'orderRouter',
          page: OrdersPage,
        ),
        AutoRoute(
          path: 'profile',
          guards: [RouteGuard],    //<------- here registered router guard but not working in Tabscaffold page
          page: ProfilePage,
        ),
        // RedirectRoute(path: '*', redirectTo: ''),

      ],
    ),
  ],
)

Trying to add a route guard in the bottom navigation but it's not working as I expected. What am I missing here? Github Issue

2

There are 2 best solutions below

3
On

I encountered the same problem, guard isn't called with AutoTabsScaffold. After some investigation, it turns out that guards in TabsRouter are not supported .

0
On

I have a similar structure and for me it work by adding guard at the top of nested navigation in your case in landing :

AutoRoute(
  initial: true,
  page: LandingPage,
  guards: [RouteGuard],   
  path: 'landing',