Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When guard redirects from AutoTabsRouter the navigation history doesnt clear #1718

Open
Mayb3Nots opened this issue Sep 3, 2023 · 0 comments

Comments

@Mayb3Nots
Copy link

Mayb3Nots commented Sep 3, 2023

I have a AutoRouteGuard like this

import 'package:auto_route/auto_route.dart';
import 'package:hijaulife/src/src.dart';

class AuthGuard extends AutoRouteGuard {
  AuthGuard({required AuthRepository auth}) : _auth = auth;

  final AuthRepository _auth;
  @override
  void onNavigation(NavigationResolver resolver, StackRouter router) {
    if (_auth.getCurrentUser().isRight()) {
      resolver.next();
    } else {
      resolver.redirect(
        WelcomeInitialRoute(
          children: [
            LoginRoute(
              onOtpVerified: (registered) {
                if (registered) {
                  resolver.next();
                } else {
                  router.push(RegistrationRoute(onRegistered: (context) => resolver.next()));
                }
              },
            ),
          ],
        ),
        replace: true,
      );
    }
  }
}

This is my dashboard screen

AutoTabsRouter(
      routes: const [
        HomeRoute(),
        ActivitiesRoute(),
        RewardRoute(),
        ProfileRoute(),
      ],
      builder: (context, child) {
        final tabsRouter = AutoTabsRouter.of(context);

        return Scaffold(
          body: child,
          bottomNavigationBar: NavigationBar(
            selectedIndex: tabsRouter.activeIndex,
            onDestinationSelected: tabsRouter.setActiveIndex,
            destinations: NavigationTabs.values
                .map(
                  (e) => NavigationDestination(icon: Icon(e.icon), label: e.name.capitalizeFirst()),
                )
                .toList(),
          ),
        );
      },
    );

When the user logs out the in the ProfileRoute guard re-evaluates and redirects to the WelcomeInitialRoute and when logged back in it initializes the TabView in profile view instead of HomeRoute. I have checked and it seems to be because of the history. I have tried by placing keepHistory false and still doesn't work. Any suggestions on how we could fix this?

My router config

import 'package:auto_route/auto_route.dart';
import 'package:hijaulife/src/src.dart';

@AutoRouterConfig()
class CoreRouter extends $CoreRouter {
  CoreRouter({required AuthRepository auth}) : _authRepository = auth;

  final AuthRepository _authRepository;
  @override
  List<AutoRoute> get routes => [
        AutoRoute(
          page: WelcomeInitialRoute.page,
          children: [
            AutoRoute(page: LoginRoute.page, initial: true),
            AutoRoute(
              page: PhoneAuthInitialRoute.page,
              children: [
                AutoRoute(page: PhoneAuthRoute.page, initial: true),
                AutoRoute(page: OTPRoute.page),
                AutoRoute(page: RegistrationRoute.page),
              ],
            ),
          ],
        ),
        AutoRoute(
          page: DashboardRoute.page,
          initial: true,
          guards: [
            AuthGuard(auth: _authRepository),
          ],
          children: [
            AutoRoute(
              page: HomeRoute.page,
              initial: true,
            ),
            AutoRoute(page: ProfileRoute.page),
            AutoRoute(page: RewardRoute.page),
            AutoRoute(page: ActivitiesRoute.page),
          ],
        ),
        AutoRoute(page: StartCarpoolTripRoute.page),
      ];
}

@RoutePage()
class WelcomeInitialScreen extends AutoRouter {
  const WelcomeInitialScreen({super.key});
}

@RoutePage()
class PhoneAuthInitialScreen extends AutoRouter {
  const PhoneAuthInitialScreen({super.key});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant