Skip to content

Remain subscribed to Observables returned in CanActivate guards for lifetime of route #50502

Description

@deefdragon

Which @angular/* package(s) are relevant/related to the feature request?

No response

Description

One of the possible values that can be returned from auth guards is an observable. Currently this observable is (effictivly) treated no differently than an asynchronous single result. The ability for an Observable to return multiple values over time could be much better leveraged in guards to allow for continuously checking if a page should be accessible.

Proposed solution

I propose that, instead of just taking first() on the observable response in canActivate, first() is taken and checked with the other types, and any Observables are merged and subscribed to for the lifetime of the route. Should a canActivate's observable return false/a url tree, it will redirect the same way it would if it had returned that in the initial load.

Alternatives considered

The only way currently to redirect after page load should a user state change is to listen for those state changes on every page that requires it. (The exact problem that guards are trying to solve I believe). This results in quite a bit of code duplication.

Yes in most cases with guards you will likely be listening for user state changes regardless, but having to add the step of "check if still logged in, redirect if not", when we already have a place that's largely handling that in the guard is redundant.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions