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

Multiple guards behavior still incorrect #906

Open
3ph opened this issue Oct 11, 2023 · 1 comment
Open

Multiple guards behavior still incorrect #906

3ph opened this issue Oct 11, 2023 · 1 comment
Labels
new New issue request attention

Comments

@3ph
Copy link

3ph commented Oct 11, 2023

Currently when using multiple guards when first guard fails rather than redirecting to the first failed guard route it just continues through all the routes and returns last failed guard redirect.

Example:

...
  @override
  void routes(RouteManager r) {
    r.child(
      '/',
      child: (context) => Page(),
      guards: [
        AGuard(),
        BGuard(),
        CGuard(),
      ],
    );
  }
...

class AGuard extends RouteGuard {
  AGuard() : super(redirectTo: '/a');

  @override
  Future<bool> canActivate(String path, ModularRoute router) async {
    return true;
  }
}

class BGuard extends RouteGuard {
  BGuard() : super(redirectTo: '/b');

  @override
  Future<bool> canActivate(String path, ModularRoute router) async {
    return false;
  }
}

class CGuard extends RouteGuard {
  CGuard() : super(redirectTo: '/c');

  @override
  Future<bool> canActivate(String path, ModularRoute router) async {
    return false;
  }
}

In the example above rather than redirecting to /b it will redirect to /c.

This issue was already addressed by #745 but the PR was never merged and just deleted so the issue persists.

Note: there is a workaround to merge all the guards into one and use pos to figure out where to redirect but it seems wrong.

@3ph 3ph added the new New issue request attention label Oct 11, 2023
@Vatalion
Copy link

Vatalion commented Dec 12, 2024

@3ph take a look here

class GuardsScope extends RouteGuard {
  GuardsScope(this._guards);

  final List<RouteGuard> _guards;

  @override
  String? get redirectTo => _guards[currentGuardIndex].redirectTo;

  int currentGuardIndex = 0;

  @override
  FutureOr<bool> canActivate(String path, ParallelRoute<dynamic> route) async {
    for (final guard in _guards) {
      currentGuardIndex = _guards.indexOf(guard);
      final canActivate = await guard.canActivate(path, route);
      if (!canActivate) {
        return false;
      }
    }
    return true;
  }
}

you just pack this with your guards and pass this one as single guard . keep in mind that order matters.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new New issue request attention
Projects
None yet
Development

No branches or pull requests

2 participants