Chrillewoodz
Chrillewoodz

Reputation: 28348

CreateUser function not running at all

I'm trying to achieve a user creation in Firebase but the function that would do it just gets skipped completely for some reason. I've done this a thousand times before but now I can't figure out why this function isn't being run at all, it's driving me insane so hopefully someone can point out what I've done wrong here.

I've got a button that is connected to a form, and when you click this button it will call a validate function:

<button ng-click="validate()">Submit</button>

$scope.validate = function() {
    $scope.error = validation.validateSignup($scope.user);
    $scope.error ? false : $scope.error = dbEntry.createNewUser($scope.user);
}

Which will call a factory method and if no errors are returned, it will call the createNewUser method of the dbEntry factory.

A factory which points to either a reference, synced reference or an authentication object. The latter is what I'm using to create a new user. See documentation for this here.

forumApp.factory('fbRef', ['$firebase', '$firebaseAuth', function($firebase, $firebaseAuth) {

    return {
        getReference: function(path) {
            return path ? new Firebase('https://blabla.firebaseio.com/' + path) : new Firebase('https://forum-app.firebaseio.com/');  
        },
        getSyncedReference: function(path) {
            return $firebase(new Firebase('https://blabla.firebaseio.com/' + path));
        },
        getAuthObj: function() {
            return $firebaseAuth(new Firebase('https://blabla.firebaseio.com/'));
        }
    };
}]);

Example user object that gets passed to createNewUser method:

$scope.user = {
    firstname: 'Chris',
    lastname: 'Ferguson',
    age: '21',
    country: 'Sweden',
    displayName: 'BadBoy13',
    email: '[email protected]',
    password: 'mothercow',
    confirmedPassword: 'mothercow',
    isTermsAccepted: true
}

The factory which contains the createNewUser method that will register a new account in the Firebase database.

UPDATED FACTORY - It now runs but I keep getting the default "Connection error- try again later" in the switch statement when I try to insert anything into the database:

forumApp.factory('dbEntry', ['$firebase', '$firebaseAuth', 'fbRef', function($firebase, $firebaseAuth, fbRef) {
    return {
        /**
         * Create a new user account in Firebase
         *
         * @param Object $user
         *  User details
         *
         * @return String
         *  If error -> error message
         *  If successful -> success message
         */
        createNewUser: function(user) {

            return fbRef.getAuthObj().$createUser({
                email: user.email,
                password: user.password
            }).then(function(userData) {
                console.log(userData);
                return 'Registration successful';
            }).catch(function(error) {

                switch (error.code) {

                    case 'EMAIL_TAKEN': 
                        return 'Email already registered';

                    default: 
                        return 'Connection error - try again later';
                }
            });
        }
    };
}]);

Could someone point out what is wrong here?

Upvotes: 0

Views: 107

Answers (1)

Pankaj Parkar
Pankaj Parkar

Reputation: 136154

createNewUser method of dbEntry factory is returning a promise, but in your you didn't returned the promise. fbRef.getAuthObj().$createUser({}) method should return from factory.

CODE

forumApp.factory('dbEntry', ['$firebase', '$firebaseAuth', 'fbRef', function($firebase, $firebaseAuth, fbRef) {
    return {
        createNewUser: function(user) {
            //returning promise that will return to method who is accessing it.
            return fbRef.getAuthObj().$createUser({
                email: user.email,
                password: user.password
            }).then(function(userData) {
                console.log(userData);
                return 'Registration successful';
            }).catch(function(error) {

                switch (error.code) {

                    case 'EMAIL_TAKEN': 
                        return 'Email already registered';

                    default: 
                        return 'Connection error - try again later';
                }
            });
        }
    };
}]);

Controller

$scope.validate = function() {
    $scope.error = validation.validateSignup($scope.user);
    $scope.error ? false : $scope.error = dbEntry.createNewUser($scope.user);
}

Hope this could help you. Thanks.

Upvotes: 1

Related Questions