0

I am trying to build a service called AuthService with AngularJS to use in all my controllers. Below is my code.

.factory('AuthService', function(){

    return {
      isAuthenticated : function(){

        var AuthStatus;

        Stamplay.User.currentUser().then(function(res){
          if(res.user){
            console.log("A");
            AuthStatus = true;
          }
          else{
            console.log("B");
            AuthStatus = false;
          }
        }, function(err){
          console.log("C");
          AuthStatus = false;
        });

        return AuthStatus;
      }
    }
})

When I try to use AuthService.isAuthenticated() in my controllers, I always get undefined and also B is logged to the console which means that the else block runs fine. But I do not get any return value from the service as a whole. Any help is highly appreciated.

Thanks in advance.

Samarth Agarwal
  • 2,044
  • 8
  • 39
  • 79
  • 4
    `Stamplay.User.currentUser().then` is an async function - so you can't just `return` from it. Use a callback or continue the promise pattern in the controller – tymeJV Feb 29 '16 at 19:07
  • Could you please provide some supporting code? – Samarth Agarwal Feb 29 '16 at 19:09
  • 2
    It's common issue, see [this](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) for more details. – Slava N. Feb 29 '16 at 19:15

0 Answers0