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: -
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; }]);
angularjs authentication
Sutikshan Dubey Sep 25 2018-12-18T00: 00Z
source share