InterDependency corner service for transferring an authentication token in the Http Header

I created three services: OpenApi, SecureApi, and TokenHandler. I want to use the TokenHandler service from the SecureApi service. When authentication occurs, the controller will set the token in the TokenHandler.Set () method. and I want SecureApi to be cal tokenHandler.get () as shown below. Is it possible. Right now I'm getting below the error: -

EDIT: - fixed below error (also updated below code, but my token still does not go through part of the header :()

Unknown provider: tokenHandlerProvider <- tokenHandler <- SecureApi

The code: -

/* Services */ angular.module('MyApp.services', ['ngResource']) .factory('OpenApi', function ($resource) { var openApi = $resource( '/api/:controller/:id', [], { postLogOn: { method: 'POST', params: { controller: 'Account' } }, postCustomer: { method: 'POST', params: { controller: 'Employee' } } } ); return openApi; }) .factory('TokenHandler', function () { var tokenHandler = {}; var token = "none"; tokenHandler.set = function (newToken) { token = newToken; }; tokenHandler.get = function () { return token; }; return tokenHandler; }) .factory('SecureApi', ['$resource', 'TokenHandler', function(res, tokHandler) { var secureApi = $resource( '/api/:controller/:id', [], { getInsightCustomer: { method: 'GET', params: { controller: 'MyCustomer' }, headers: {Authorization_Token: tokenHandler.get()} } } ); return secureApi; }]); 
+2
angularjs authentication
Sep 25
source share
1 answer

I got an answer to this in AngularJS googlegroups: -

https://groups.google.com/forum/?hl=en&fromgroups=#!topic/angular/jfttVnvga9M

It was a syntax error in the above code.

Your TokenHandler service has an uppercase T, but your SecureApi service "requests" a Handler token with a lowercase t. This is usually better - and important if you are going to minimize the use of js to use the input format of the array, which will also allow you to use whatever you like as the parameter name for this service. For example:

.factory ('SecureApi', ['$ resource', 'TokenHandler', function (res, tokHandler) {...}]);

+1
Sep 26 '12 at 4:16
source



All Articles