angularjs - Configure angular $http with a value obtained from server -


i'm building (first) angular app have tokens inserted headers (the content shown part taken here)

angular.module('myapp')

.factory('sessioninjector', ['sessionservice', function(sessionservice) {      var sessioninjector = {         request: function(config) {             config.headers['x-session-token'] = sessionservice.gettoken();             return config;         }     };     return sessioninjector; }])  .config(['$httpprovider', function($httpprovider) {       $httpprovider.interceptors.push('sessioninjector'); }]) 

the trouble i'm having sessionservice - how can initialize call server?

for example, didn't work:

.factory('sessionservice', ['$injector', function($injector){     var token = "";      return {         gettoken: function () {             var http = $injector.get('$http');             if (token === "") {                 http.get('http://localhost/api/auth/gettoken').success(function (ret) {                     token = ret;                 });             }             return token;              //i can see $q/deferred/promise should used somehow here...              //but i'm not sure solves problem i'm having...         }     } }]); 

because overloads cpu 100%...

since it's first angular app, i'm sure i'm missing something, but... what?

edit:
take on matter... still doesn't work though... (again, uses cpu, infinite loop)

  .factory('sessiondata', function () {     var currenttoken = '[uninitialized-token]';      return {         gettoken: function () {             return currenttoken;         },         settoken: function (token) {             currenttoken = token;         }     } })  .factory('sessioninjector', ['sessiondata', '$injector', '$q', function (sessiondata, $injector, $q) {     var sessioninjector = {         request: function (config) {             var deferred = $q.defer();             var http = $injector.get('$http');             http.get('http://localhost/api/auth/gettoken').success(function (ret) {                 sessiondata.settoken(ret);                 console.log("successfully authenticated token " + sessiondata.gettoken());                 config.headers['x-header-sessionid'] = sessiondata.gettoken();                 deferred.resolve(config);             })             .error(function(){                 console.log("failed authenticate");                 deferred.resolve(config);             });              return deferred.promise;         }     };     return sessioninjector; }])  .config(['$httpprovider', function ($httpprovider) {     $httpprovider.interceptors.push('sessioninjector'); }])  .run(['$http', 'sessiondata', function ($http, configs, sessiondata) {     $http.get('http://localhost/api/auth/testmethod').then(function (ret) {         //do something...     }); }]) 

check whether modified code fragment solve issues.

.factory('sessionservice', ['$http', '$q', function($http, $q) {     var token = null;     var sessionservice = {};     var differred = $q.defer();      sessionservice.readtoken = function() {         return $http.get('http://localhost/api/auth/gettoken')             .success(function (res) {                 console.log('auth success , token received: ' + json.stringify(res.data));                  // extract token details received json object                 token = res.data;                 differred.resolve(res);             }, function (res) {                 console.log('error occurred : ' + json.stringify(res));                 differred.reject(res);             }         )     };      sessionservice.gettoken = function() {         return token;     };      sessionservice.isanonymous = function() {         if (token)             return true;         else             return false;     };      return sessionservice; }])   .factory('sessioninjector', ['sessionservice', function(sessionservice) {     var sessioninjector = {         request: function(config) {             if (!sessionservice.isanonymous) {                 config.headers['x-session-token'] = sessionservice.gettoken();                 return config;             }         }     };     return sessioninjector; }])  .config(['$httpprovider', function($httpprovider) {     $httpprovider.interceptors.push('sessioninjector'); }]) 

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -