Skip to content

Unsubscribe from routing guards as soon as one of them returns false聽#36538

Closed

Description

@sod

馃殌 feature request

Unsubscribe from routing guards as soon as one of them returns false

For @angular/router

Description

Right now if you use two or more guards on a route, the router will run them all. Even if one of them already returned false.

Practical example: You have a checkout route:

  • Guard 1 checks, if you are logged in. If not, it shows the login
  • Guard 2 checks, if your address is missing. If it is, it redirects you to the page to enter an address
  • Guard 3 checks, if a payment is missing. If it is, it redirects you to the page to choose a payment method

Route looks like this:

{
    path: '/checkout',
    canActivate: [LoginRequired, AddressRequired, PaymentRequired],
}

If you write it like that, and all 3 guards return false, you get 3 redirects. And if redirect AddressRequired wins, that routes LoginRequired will execute again.

Reproduction: https://ng-run.com/edit/uFlyQUYoejI0PkJhBUnm
Open the browser devtools console and then click the link to navigate to the blocked route to see the order of execution of the guards.

Describe the solution you'd like

If a guard returns false, unsubscribe from previous guards and don't even execute remaining guards.

Right now we use a service that wraps the observables returned by canActivate to achieve this behavior. Looks like this https://ng-run.com/edit/HmMVqtmmHB52knus3yjk

Activity

  1. added this to the needsTriage milestone on Apr 9, 2020
  2. changed the title [-]Unsubscribe from routing guards as soon as one of them already returns false[/-] [+]Unsubscribe from routing guards as soon as one of them returns false[/+] on Apr 9, 2020
  3. added
    featureLabel used to distinguish feature request from other issues
    on Apr 16, 2020
  4. modified the milestones: needsTriage, Backlog on Apr 16, 2020
  5. reopened this on Jan 9, 2021
  6. angular-automatic-lock-bot commented on Feb 9, 2021

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  7. locked and limited conversation to collaborators on Feb 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions