0

So I've been skimming through many blogs/tutorials trying to figure this out but to no avail.

What I want is to be able to pass to @CanActivate() functions which are supposed to figure out if the user is logged in. I have tried making a static function

static isUserInjector() {
    return (next, prev) => Injector.resolveAndCreate([AuthService, HTTP_PROVIDERS]).get(AuthService).isUser();
}

inside my AuthService class so I just import it into the directive's .ts and call the static function but this seems a bit clumsy and I don't feel too good using this Injector because I suppose I was to bootstrap everything I'd need but I don't know how to get to it outside of my components. (I know only that you can get to the bootstrapped services by 'demanding' the in the constructor.

Is dependency injection a wrong pattern to go with @CanActivate?

ditoslav
  • 4,563
  • 10
  • 47
  • 79

1 Answers1

0

You can check out my answer to a similar question here: Angular2 Router - Anyone know how to use canActivate in app.ts so that I can redirect to home page if not logged in

I explain how to extend the router-outlet for route authentication. Hope it helps.

Community
  • 1
  • 1
inoabrian
  • 3,762
  • 1
  • 19
  • 27