Re: [angular.js] Circular Dependency Issue while attempting to provide an Authentication Service

3,294 views
Skip to first unread message

Witold Szczerba

unread,
Jul 14, 2012, 10:36:28 AM7/14/12
to ang...@googlegroups.com
Hi,
I had the same issue. I could not find a nice solution, so I worked it
around. It is not very pretty, but works and I am eager to hear about
alternate solutions :)
OK, so here is the implementation:
https://gist.github.com/3111582
(see lines #14 and #16)

I wanted to create a new blog entry describing my implementation, but
did not manage to do it yet. My goal is to demonstrate it as a
separate module, so applications can use it just by declaring
dependency on "angular-auth" module.

Comments, suggestions are welcome :)

Regards,
Witold Szczerba


On 14 July 2012 01:30, Brice Burgess <bric...@gmail.com> wrote:
> I am following the patterns outlined by Witold Szczerba in
> http://www.espeo.pl/2012/02/26/authentication-in-angularjs-application to
> provide an authorization layer to my application -- although am refactoring
> to use services in an attempt to keep the rootScope clean and to learn more
> about Angular! ;)
>
> After many hours spent in an extreme fight to push a .service() derived
> interceptor to $httpProvider, I had to relent and create a .factory()
> provided one. (for the curious; I was trying to do something like
> `$httpProvider.responseInterceptors.push(AuthenticationService.interceptor);`).
> So I have 2 services;
>
> 1. AuthenticationService (depends on $http)
> 2. AuthenticationInterceptor (depends on $q, AuthenticationService)
>
> When I add the $http dependency to the Authentication service, I get:
>
> Circular dependency: AuthenticationService <- AuthenticationInterceptor <-
> $http
>
> My initial thought is that this involves $q in the Interceptor -- but I'm
> unsure && don't easily see the circle.
>
> A jsFiddle is up @
> http://jsfiddle.net/rBZkE/
>
> If anyone has the time to look at this & comment, please consider yourself a
> saint. I'm also curious as to feedback re: using this strategy for
> implementing an authorization layer. It seems a hot topic!
>
> Many thanks,
>
> ~ Brice
>
> --
> You received this message because you are subscribed to the Google Groups
> "AngularJS" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/angular/-/ryTHk37KYTUJ.
> To post to this group, send email to ang...@googlegroups.com.
> To unsubscribe from this group, send email to
> angular+u...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/angular?hl=en.

Ben Priebe

unread,
Jul 29, 2012, 5:45:48 PM7/29/12
to ang...@googlegroups.com
Get's the job done. I couldn't think of a better way.
> angular+unsubscribe@googlegroups.com.

Philipp Burgmer

unread,
Jul 30, 2012, 5:03:35 AM7/30/12
to ang...@googlegroups.com
Hi,

I think the cycle builds like this:
1) modul configuration is order independent. all configuration is stored and then the bootstrapping starts.
2) $http service is requested somewhere, so angular asks $httpProvider to construct it
3) $httpProvider aks the injector for AuthenticationInterceptor (configured by you)
4) AuthenticationInterceptor needs AuthenticationService
5) AuthenticationService needs $http, which isn't constructed yet, because $httpProvider is waiting for its dependencies!


Greetings
Philipp

Brice Burgess

unread,
Aug 1, 2012, 6:36:58 PM8/1/12
to ang...@googlegroups.com
Thanks for sharing. I think it would be great if interceptors could be registered at runtime - such as providing a methods to the $http service to add and remove them at will. Would this be a worthwhile feature request?

~ Brice

Witold Szczerba

unread,
Aug 11, 2012, 7:40:57 PM8/11/12
to ang...@googlegroups.com
Yes, that is exactly the case.
> --
> You received this message because you are subscribed to the Google Groups
> "AngularJS" group.
> To post to this group, send email to ang...@googlegroups.com.
> To unsubscribe from this group, send email to
> angular+u...@googlegroups.com.
> Visit this group at http://groups.google.com/group/angular?hl=en.
>
>
Reply all
Reply to author
Forward
0 new messages