ng-token-auth icon indicating copy to clipboard operation
ng-token-auth copied to clipboard

Deferred promise not resolving - $auth.validateUser() in Ionic ui-router

Open przemekd opened this issue 9 years ago • 6 comments

Hi, I use $auth.validateUser() in ui-router resolve. When I want to change the state after login most times promise returned by $auth.validateUser() does not resolve.

Situation occurs every time that this.dfd object is already created when validateUser() function is executed.

validateUser: function(opts) {
              var clientId, configName, expiry, location_parse, params, search, token, uid, url;
              if (opts == null) {
                opts = {};
              }
              configName = opts.config;
              console.log(this.dfd);
              if (this.dfd == null) {

Is it safe to set this.dfd to null at the start of validateUser function to workaround the problem?

BTW, there is no such problem with web version of the app.

przemekd avatar Sep 02 '16 13:09 przemekd

I am also experiencing the same problem. Did setting this.dfd to null work for you? Are you using master branch or the 0.0.29 release?

mrsweaters avatar Sep 03 '16 17:09 mrsweaters

Just a note, I'm using the web version and it is not resolving correctly.

mrsweaters avatar Sep 03 '16 17:09 mrsweaters

Oh nevermind, it's working now. That was a slight misunderstanding on my part.

mrsweaters avatar Sep 03 '16 17:09 mrsweaters

@mrsweaters Unfortunatelly setting this.dfd to null breaks other things... I've discovered that if one changes state after some time (using setTimeout function for example), the promise successfully resolves, so there must be some time of race condition. I hadn't more time to investigate further..

przemekd avatar Sep 05 '16 09:09 przemekd

I've also encountered that bug:(

dkardanski avatar Sep 08 '16 23:09 dkardanski

I figured out at least one reason why this occurs. My error was that I was simultaneously calling submitLogin in a controller method on an ng-click of the submit button AND had the submitLogin() in the ng-submit of the form definition. This caused the submit to occur twice in rapid succession. Apparently the ng-click gets handled first which means the promise handed back from submitLogin is from the ng-click. Since the deferred is a global within ng-token-auth the first one gets clobbered when the second one (the one from the ng-submit) occurs and only the second one is resolved.

Check your network log and see if you happen to be submitting two auth requests in rapid succession for some reason. That could be where the root of the problem for some people.

SeraphAdmin avatar Apr 19 '17 14:04 SeraphAdmin