颤音:自动安装:RouteGuard在AutotabsScaffold中不工作

发布于 2025-01-24 04:07:23 字数 2811 浏览 0 评论 0原文

我正在尝试为我的autotabsscaffold添加Auth Guard [底部NAV],但它不起作用,它在其他导航页面上工作,而不仅仅是在我的着陆页中[autotabsscaffold |底部的奈斯居住]我在这里错过了什么吗?

使用:auto_route: ^3.2.4 [pub.dev]

   return AutoTabsScaffold(

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

班级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: ''),

      ],
    ),
  ],
)

试图在底部导航中添加路线防护设备,但它无法正常工作。我在这里想念什么? github问题

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

罪歌 2025-01-31 04:07:25

我遇到了同样的问题,没有使用autotabsscaffold来调用后卫。经过一番调查,事实证明,tabsrouter中的守卫不是支持

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

缺⑴份安定 2025-01-31 04:07:25

我也有类似的结构,对我而言,它可以通过在您案件的嵌套导航顶部添加警卫来工作:

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

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',
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文