1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

How to provide an angular guard in a specific injection context?

Discussão em 'Angular' iniciado por Duncan McPherson, Setembro 23, 2024.

  1. I have a monorepo that contains several applications as well as some shared libraries. What I am trying to do is implement an authGuard for some of the pages in the applications, however the issue I am running into is that, in the context of the router, my AuthService has no provider.

    This is because the dependencies of AuthService look like this:

    AuthService -> FirebaseAuthService -> FirebaseAppService -> EnvironmentService

    On top of that, I am trying to prevent services and modules being initialized unnecessarily. So, AuthService should only be initialized if I try to navigate to a page that requires authentication, or I am on the authentication page (ideally, I would have some informational pages that don't require auth).

    Attempting to wrap my guard in runInInjectionContext seems to make things worse because not only can I not use an NgModule type as a value for the Injector parameter, but writing the function for the second parameter in a way that satisfies the CanMatchFn specs makes it so that there are even more errors.

    I really can't provide AuthService in 'root' because of the initialization process for the app's environment.

    Continue reading...

Compartilhe esta Página