0

I'm trying put the authentication of my firebase in a service. But I stumbled on some problems with program flow. The response from firebase is slower and the code needs to wait for it to complete.

I tried to create a promise, but it doesnt work properly. Here is my code:

//controller.js

articleControllers.controller('AuthController', 
    ['$scope', '$firebaseObject', 'AuthenticationService', 
    function($scope, $firebaseObject, AuthenticationService){


        $scope.login = function() {    
             AuthenticationService.login($scope.loginForm)
            .then(function(result) {
                if(result.error) {
                    $scope.message= result.error.message;
                }
                else {
                    console.log("user");
                }
            });
        };  
    }
 ]);

services.js

myApp.factory('AuthenticationService',
function($firebase, $firebaseAuth, $routeParams, FIREBASE_URL) {

    var auth = new Firebase(FIREBASE_URL);

    var myReturnObject = {

        //user login
        login: function(user) {
            return auth.authWithPassword({
                email: user.loginEmail,
                password: user.loginPassword
            },
            function(error, authData) {
                console.log("hit after .then");
                return {
                    error: error,
                    authData: authData
                }
            });
         }

    };

    return myReturnObject;
});

I already used a promise once in my code for a $http get request. But for firebase it doesnt seem to work. I get the error: Cannot read property 'then' of undefined in controller.js. Anyone an idea how I can let angular wait for the service?

4
  • Do you want that semi colon before the .then? Commented May 16, 2015 at 9:20
  • No, its a typo while simplifying my code. I edit it. Commented May 16, 2015 at 9:21
  • firebase function authWithPassword does not return promise, it is a callback style. you need to create promise in your code. Commented May 16, 2015 at 9:23
  • in service.js or controller.js? Could you provide a short example? It would be much appreciated. I already tried a 100 things. Commented May 16, 2015 at 9:26

1 Answer 1

1

remember to inject $q.

myApp.factory('AuthenticationService',
function($q, $firebase, $firebaseAuth, $routeParams, FIREBASE_URL) {

    var auth = new Firebase(FIREBASE_URL);

    var myReturnObject = {

        //user login
        login: function(user) {
          return $q(function(resolve, reject) {
            auth.authWithPassword({
                email: user.loginEmail,
                password: user.loginPassword
            }, function authCallback(error, authData) {
              if (error) {
                return reject(error);
              }
              resolve(authData);

            });
          });

         }

    };

    return myReturnObject;
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.