javascript - Login Timing in AngularJs using `$q(resolve, reject)` -


i having issue login timing in angular app. intention when user logs in, api key set in localstorage , available call on accessing api. after login, want app redirect user home state. happening user redirected, gets 401 - , based off have setup - redirected login. if hard refresh on browser after logging in works. here have going.

.factory('localstor', function($q, $log, $window, $rootscope) { var apikey = false;  return {     getapi: function() {         return $q(function(resolve, reject) {             var data = $window.localstorage.getitem('apikey');              if (data == null) {                  return $rootscope.$broadcast('notauthorized');             } else {                 var key = data;                 return resolve(key);              }          });     },     setapi: function(key) {         return $q(function(resolve, reject) {             apikey = false;             if (key === undefined) {                 return reject();             }              return $window.localstorage.setitem('apikey', key);         });     } }   })  function getlogin(email, password) {     return $q(function(resolve, reject) {         $http({             method: 'post',             data: {                 'email': email,                 'password': password             },             url: config.api.baseurl + '/user/login'         }).then(function(result) {             if (result) {                 var token = result.data.data.token;                 resolve(token);             } else {                 $log.debug('incorrect email or password');                 reject(1);             }         }, function(reason) {             reject(2);         });      }) }  $scope.submitlogin = function(email, password, form) {      $scope.errornote = false;      if (form.$valid) {         $scope.loading = true;         login.getapi(email, password).then(function(data) {             var key = data;             localstor.setapi(key).then(function() {                 $timeout(function() {                     $state.go('app.loading', {}, { reload: true });                 }, 30);             });             $scope.loading = false;           }, function(reason) {             $scope.errornote = reason;             $scope.loading = false;         });      } }; 

my calls api set

var apikey = false; localstor.getapi().then(function(data) {     apikey = data;     $log.debug(apikey); }, function() {     $rootscope.$broadcast('notauthorized'); });  function withapikey(callback, errorcallback) {     localstor.getapi().then(function(data) {         if (typeof callback === 'function') {             callback(data);         }     }, function(err) {         if (typeof errorcallback === 'function') {             errorcallback(err);         }     }); }  function notificationcount() {     return $q(function(resolve, reject) {         withapikey(function(key) {             $http({                 method: 'get',                 headers: { 'authorization': 'bearer ' + key },                 url: config.api.baseurl + '/user/notifications/count'             }).then(function(result) {                 if (result) {                     resolve(result);                 } else {                     reject('uh oh');                 }             }, function() {                 reject('uh oh');             });         });     }) } 

is $q(resolve, reject) anti-pattern?

using $q(resolve, reject) $http service example of $q defer anti-pattern. unnecessary , prone erroneous implementation.

simply return $http promise. use .then method transform response:

function getlogin(email, password) {     ̶r̶e̶t̶u̶r̶n̶ ̶$̶q̶(̶f̶u̶n̶c̶t̶i̶o̶n̶(̶r̶e̶s̶o̶l̶v̶e̶,̶ ̶r̶e̶j̶e̶c̶t̶)̶ ̶{̶     ͟r͟e͟t͟u͟r͟n͟ $http({             method: 'post',             data: {                 'email': email,                 'password': password             },             url: config.api.baseurl + '/user/login'         }).then(function(result) {             if (result) {                 var token = result.data.data.token;                 ̶r̶e̶s̶o̶l̶v̶e̶(̶t̶o̶k̶e̶n̶)̶;̶                 ͟r͟e͟t͟u͟r͟n͟ token;             } else {                 $log.debug('incorrect email or password');                 ̶r̶e̶j̶e̶c̶t̶(̶2̶)̶;̶                 ͟t͟h͟r͟o͟w͟  1;             }         }, function(reason) {             ̶r̶e̶j̶e̶c̶t̶(̶2̶)̶;̶             ͟t͟h͟r͟o͟w͟  2;         });     }) } 

the .then method returns new promise settles respective handlers either return or throw.

for more information, see you're missing point of promises.


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -