<commit_before>'use strict';
angular.module('my.login.factory', ['my.firebase.factory', 'my.user.factory'])

        // a simple wrapper on simpleLogin.getUser() that rejects the promise
        // if the user does not exists (i.e. makes user required)
        .factory('loginRequireUserFactory', ['loginFactory', '$q', function (loginFactory, $q) {
                return function () {
                    return loginFactory.getUser().then(function (user) {
                        return user ? user : $q.reject({authRequired: true});
                    });
                };
            }])

        .factory('loginFactory', ['firebaseFactory', 'userFactory', 'changeEmailFactory', '$q', '$rootScope',
            function (firebaseFactory, userFactory, changeEmailFactory, $q, $rootScope) {
                var fbref = firebaseFactory.ref();
                var fbauth = firebaseFactory.auth();
                var listeners = [];
                function statusChange() {
                    fns.getUser().then(function (user) {
                        fns.user = user || null;
                        angular.forEach(listeners, function (fn) {
                            fn(user || null);
                        });
                    });
                }

                var fns = {
                    user: null,
                    getUser: function () {
                        var deferred = $q.defer();
                        deferred.resolve(fbref.getAuth());
                        return deferred.promise;
                    },
                    /**
                     * @param {string} email
                     * @param {string} pass
                     * @returns {*}
                     */
                    login: function (email, pass) {
                        var def = $q.defer();
                        if (!email) {
                            $rootScope.$broadcast('login:error', null);
                            def.reject('Email is required to login.');
                        } else if (!pass) {
                            $rootScope.$broadcast('login:error', null);
                            def.reject('Password is required to login.');
                        } else {
                            fbauth.$authWithPassword({'email': email, 'password': pass})
                                    .then(function (authData) {
                                        $rootScope.$broadcast('login:login', authData);
                                        def.resolve(authData);
                                    })
                                    .catch(function (error) {
                                        $rootScope.$broadcast('login:error', null);
                                        def.reject(error.message);
                                    });
                        }
                        return def.promise;
                    },
                    logout: function () {
                        fbauth.$unauth();
                        $rootScope.$broadcast('login:logout', null);
                    },
                    createAccount: function (profile, pass) {
                        return fns.createUser(profile.email, pass)
                                .then(function () {
                                    // authenticate so we have permission to write to Firebase
                                    return fns.login(profile.email, pass);
                                })
                                .then(function (user) {
                                    // store user data in Firebase after creating account
                                    return userFactory.createProfile(user.uid, profile).then(function () {
                                        return user;
                                    });
                                });
                    },
                    createUser: function (email, pass) {
                        var def = $q.defer();
                        fbref.createUser({'email': email, 'password': pass},
                        function (error) {
                            if (error) {
                                switch (error.code) {
                                    case 'EMAIL_TAKEN':
                                        def.reject('The new user account cannot be created because the email is already in use.');
                                    case 'INVALID_EMAIL':
                                        def.reject('The specified email is not a valid email.');
                                    default:
                                        def.reject(error);
                                }
                            } else {
                                // User account created successfully!
                                def.resolve();
                            }
                        });
                        return def.promise;
                    },
                    changePassword: function (email, oldpass, newpass) {
                        var def = $q.defer();
                        //fbauth.$changePassword({email: email, oldPassword: <PASSWORD>, newPassword: <PASSWORD>})
                        fbauth.$changePassword(email, oldpass, newpass)
                                .then(function () {
                                    def.resolve();
                                })
                                .catch(function (error) {
                                    console.log('change password error: ' + error);
                                    switch (error.code) {
                                        case 'INVALID_PASSWORD':
                                            // The specified user account password is incorrect.
                                            def.reject('invalid password');
                                        case 'INVALID_USER':
                                            // The specified user account does not exist.
                                            def.reject('invalid user');
                                        default:
                                            def.reject(error);
                                    }
                                    // def.reject(error)
                                });
                        return def.promise;
                    },
                    changeEmail: function (password, newEmail) {
                        return changeEmailFactory(password, fns.user.password.email, newEmail, this);
                    },
                    removeUser: function (email, pass) {
                        var def = $q.defer();
                        fbref.removeUser({'email': email, 'password': <PASSWORD>}, function (error) {
                            if (error === null) {
                                def.resolve();
                            } else {
                                switch (error.code) {
                                    case 'INVALID_PASSWORD':
                                        // The specified user account password is incorrect.
                                        def.reject('invalid password');
                                    case 'INVALID_USER':
                                        // The specified user account does not exist.
                                        def.reject('invalid user');
                                    default:
                                        def.reject(error);
                                }
                            }
                        });
                        return def.promise;
                    },
                    watch: function (cb, $scope) {
                        fns.getUser().then(function (user) {
                            cb(user);
                        });
                        listeners.push(cb);
                        var unbind = function () {
                            var i = listeners.indexOf(cb);
                            if (i > -1) {
                                listeners.splice(i, 1);
                            }
                        };
                        if ($scope) {
                            $scope.$on('$destroy', unbind);
                        }
                        return unbind;
                    }
                };
                $rootScope.$on('login:login', statusChange);
                $rootScope.$on('login:logout', statusChange);
                $rootScope.$on('login:error', statusChange);
                statusChange();
                return fns;
            }])

        .factory('changeEmailFactory', ['firebaseFactory', '$q', function (firebaseFactory, $q) {
                return function (password, oldEmail, newEmail, loginFactory) {
                    var ctx = {old: {email: oldEmail}, curr: {email: newEmail}};
                    var oldProfile = {};
                    var newProfile = {};
                    // execute activities in order; first we authenticate the user
                    return authOldAccount()
                            // then we fetch old account details
                            .then(loadOldProfile)
                            // then we create a new account
                            .then(createNewAccount)
                            // then we copy old account info
                            //.then(copyProfile) this is done in createNewAccount!!!
                            // and once they safely exist, then we can delete the old ones
                            // we have to authenticate as the old user again
                            .then(authOldAccount)
                            .then(removeOldProfile)
                            .then(removeOldLogin)
                            // and now authenticate as the new user
                            .then(authNewAccount)
                            .catch(function (err) {
                                console.error(err);
                                return $q.reject(err);
                            });
                    function authOldAccount() {
                        // return 
                        var def = $q.defer();
                        loginFactory.login(ctx.old.email, password)
                                .then(function (user) {
                                    ctx.old.uid = user.uid;
                                    console.log('authOldAccount ctx.old.uid ' + ctx.old.uid);
                                    def.resolve();
                                })
                                .catch(function (err) {
                                    console.error('authOldAccount ' + err);
                                    def.reject(err);
                                });
                        return def.promise;
                    }

                    function loadOldProfile() {
                        var def = $q.defer();
                        ctx.old.ref = firebaseFactory.ref('users', ctx.old.uid);
                        ctx.old.ref.once('value',
                                function (snap) {
                                    var dat = snap.val();
                                    if (dat === null) {
                                        def.reject(oldEmail + ' not found');
                                    }
                                    else {
                                        oldProfile = dat;
                                        def.resolve();
                                    }
                                },
                                function (err) {
                                    def.reject(err);
                                });
                        return def.promise;
                    }

                    function createNewAccount() {
                        newProfile = oldProfile;
                        newProfile.email = ctx.curr.email;
                        return loginFactory.createAccount(newProfile, password).then(function (user) {
                            ctx.curr.uid = user.uid;
                            console.log('createNewAccount ctx.curr.uid ' + ctx.curr.uid);
                        });
                    }

                    function copyProfile() {
                        // not used anymore since adding user profile properties.
                        var d = $q.defer();
                        ctx.curr.ref = firebaseFactory.ref('users', ctx.curr.uid);
                        newProfile = oldProfile;
                        newProfile.email = ctx.email;
                        ctx.curr.ref.set(newProfile, function (err) {
                            if (err) {
                                d.reject(err);
                            } else {
                                d.resolve();
                            }
                        });
                        return d.promise;
                    }

                    function removeOldProfile() {
                        var d = $q.defer();
                        ctx.old.ref.remove(function (err) {
                            if (err) {
                                d.reject(err);
                            } else {
                                d.resolve();
                            }
                        });
                        return d.promise;
                    }

                    function removeOldLogin() {
                        var def = $q.defer();
                        loginFactory.removeUser(ctx.old.email, password).then(function () {
                            def.resolve();
                        }, function (err) {
                            def.reject(err);
                        });
                        return def.promise;
                    }

                    function authNewAccount() {
                        return loginFactory.login(ctx.curr.email, password);
                    }
                };
            }]);
;
<commit_msg>change to use firebase api $createUser<commit_after>'use strict';
angular.module('my.login.factory', ['my.firebase.factory', 'my.user.factory'])

        // a simple wrapper on simpleLogin.getUser() that rejects the promise
        // if the user does not exists (i.e. makes user required)
        .factory('loginRequireUserFactory', ['loginFactory', '$q', function (loginFactory, $q) {
                return function () {
                    return loginFactory.getUser().then(function (user) {
                        return user ? user : $q.reject({authRequired: true});
                    });
                };
            }])

        .factory('loginFactory', ['firebaseFactory', 'userFactory', 'changeEmailFactory', '$q', '$rootScope',
            function (firebaseFactory, userFactory, changeEmailFactory, $q, $rootScope) {
                var fbref = firebaseFactory.ref();
                var fbauth = firebaseFactory.auth();
                var listeners = [];
                function statusChange() {
                    fns.getUser().then(function (user) {
                        fns.user = user || null;
                        angular.forEach(listeners, function (fn) {
                            fn(user || null);
                        });
                    });
                }

                var fns = {
                    user: null,
                    getUser: function () {
                        var deferred = $q.defer();
                        deferred.resolve(fbref.getAuth());
                        return deferred.promise;
                    },
                    /**
                     * @param {string} email
                     * @param {string} pass
                     * @returns {*}
                     */
                    login: function (email, pass) {
                        var def = $q.defer();
                        if (!email) {
                            $rootScope.$broadcast('login:error', null);
                            def.reject('Email is required to login.');
                        } else if (!pass) {
                            $rootScope.$broadcast('login:error', null);
                            def.reject('Password is required to login.');
                        } else {
                            fbauth.$authWithPassword({'email': email, 'password': <PASSWORD>})
                                    .then(function (authData) {
                                        $rootScope.$broadcast('login:login', authData);
                                        def.resolve(authData);
                                    })
                                    .catch(function (error) {
                                        $rootScope.$broadcast('login:error', null);
                                        def.reject(error.message);
                                    });
                        }
                        return def.promise;
                    },
                    logout: function () {
                        fbauth.$unauth();
                        $rootScope.$broadcast('login:logout', null);
                    },
                    createAccount: function (profile, pass) {
                        return fns.createUser(profile.email, pass)
                                .then(function () {
                                    // authenticate so we have permission to write to Firebase
                                    return fns.login(profile.email, pass);
                                })
                                .then(function (user) {
                                    // store user data in Firebase after creating account
                                    return userFactory.createProfile(user.uid, profile).then(function () {
                                        return user;
                                    });
                                });
                    },
                    createUser: function (email, pass) {
                        var def = $q.defer();
                        fbauth.$createUser(email, pass)
                                .then(function () {
                                    def.resolve();
                                })
                                .catch(function (error) {
                                    switch (error.code) {
                                        case 'EMAIL_TAKEN':
                                            def.reject('The new user account cannot be created because the email is already in use.');
                                        case 'INVALID_EMAIL':
                                            def.reject('The specified email is not a valid email.');
                                        default:
                                            def.reject(error);
                                    }
                                });
                        return def.promise;
                    },
                    changePassword: function (email, oldpass, newpass) {
                        var def = $q.defer();
                        //fbauth.$changePassword({email: email, oldPassword: oldpass, newPassword: newpass})
                        fbauth.$changePassword(email, oldpass, newpass)
                                .then(function () {
                                    def.resolve();
                                })
                                .catch(function (error) {
                                    console.log('change password error: ' + error);
                                    switch (error.code) {
                                        case 'INVALID_PASSWORD':
                                            // The specified user account password is incorrect.
                                            def.reject('invalid password');
                                        case 'INVALID_USER':
                                            // The specified user account does not exist.
                                            def.reject('invalid user');
                                        default:
                                            def.reject(error);
                                    }
                                    // def.reject(error)
                                });
                        return def.promise;
                    },
                    changeEmail: function (password, newEmail) {
                        return changeEmailFactory(password, fns.user.password.email, newEmail, this);
                    },
                    removeUser: function (email, pass) {
                        var def = $q.defer();
                        fbref.removeUser({'email': email, 'password': pass}, function (error) {
                            if (error === null) {
                                def.resolve();
                            } else {
                                switch (error.code) {
                                    case 'INVALID_PASSWORD':
                                        // The specified user account password is incorrect.
                                        def.reject('invalid password');
                                    case 'INVALID_USER':
                                        // The specified user account does not exist.
                                        def.reject('invalid user');
                                    default:
                                        def.reject(error);
                                }
                            }
                        });
                        return def.promise;
                    },
                    watch: function (cb, $scope) {
                        fns.getUser().then(function (user) {
                            cb(user);
                        });
                        listeners.push(cb);
                        var unbind = function () {
                            var i = listeners.indexOf(cb);
                            if (i > -1) {
                                listeners.splice(i, 1);
                            }
                        };
                        if ($scope) {
                            $scope.$on('$destroy', unbind);
                        }
                        return unbind;
                    }
                };
                $rootScope.$on('login:login', statusChange);
                $rootScope.$on('login:logout', statusChange);
                $rootScope.$on('login:error', statusChange);
                statusChange();
                return fns;
            }])

        .factory('changeEmailFactory', ['firebaseFactory', '$q', function (firebaseFactory, $q) {
                return function (password, oldEmail, newEmail, loginFactory) {
                    var ctx = {old: {email: oldEmail}, curr: {email: newEmail}};
                    var oldProfile = {};
                    var newProfile = {};
                    // execute activities in order; first we authenticate the user
                    return authOldAccount()
                            // then we fetch old account details
                            .then(loadOldProfile)
                            // then we create a new account
                            .then(createNewAccount)
                            // then we copy old account info
                            //.then(copyProfile) this is done in createNewAccount!!!
                            // and once they safely exist, then we can delete the old ones
                            // we have to authenticate as the old user again
                            .then(authOldAccount)
                            .then(removeOldProfile)
                            .then(removeOldLogin)
                            // and now authenticate as the new user
                            .then(authNewAccount)
                            .catch(function (err) {
                                console.error(err);
                                return $q.reject(err);
                            });
                    function authOldAccount() {
                        // return 
                        var def = $q.defer();
                        loginFactory.login(ctx.old.email, password)
                                .then(function (user) {
                                    ctx.old.uid = user.uid;
                                    console.log('authOldAccount ctx.old.uid ' + ctx.old.uid);
                                    def.resolve();
                                })
                                .catch(function (err) {
                                    console.error('authOldAccount ' + err);
                                    def.reject(err);
                                });
                        return def.promise;
                    }

                    function loadOldProfile() {
                        var def = $q.defer();
                        ctx.old.ref = firebaseFactory.ref('users', ctx.old.uid);
                        ctx.old.ref.once('value',
                                function (snap) {
                                    var dat = snap.val();
                                    if (dat === null) {
                                        def.reject(oldEmail + ' not found');
                                    }
                                    else {
                                        oldProfile = dat;
                                        def.resolve();
                                    }
                                },
                                function (err) {
                                    def.reject(err);
                                });
                        return def.promise;
                    }

                    function createNewAccount() {
                        newProfile = oldProfile;
                        newProfile.email = ctx.curr.email;
                        return loginFactory.createAccount(newProfile, password).then(function (user) {
                            ctx.curr.uid = user.uid;
                            console.log('createNewAccount ctx.curr.uid ' + ctx.curr.uid);
                        });
                    }

                    function copyProfile() {
                        // not used anymore since adding user profile properties.
                        var d = $q.defer();
                        ctx.curr.ref = firebaseFactory.ref('users', ctx.curr.uid);
                        newProfile = oldProfile;
                        newProfile.email = ctx.email;
                        ctx.curr.ref.set(newProfile, function (err) {
                            if (err) {
                                d.reject(err);
                            } else {
                                d.resolve();
                            }
                        });
                        return d.promise;
                    }

                    function removeOldProfile() {
                        var d = $q.defer();
                        ctx.old.ref.remove(function (err) {
                            if (err) {
                                d.reject(err);
                            } else {
                                d.resolve();
                            }
                        });
                        return d.promise;
                    }

                    function removeOldLogin() {
                        var def = $q.defer();
                        loginFactory.removeUser(ctx.old.email, password).then(function () {
                            def.resolve();
                        }, function (err) {
                            def.reject(err);
                        });
                        return def.promise;
                    }

                    function authNewAccount() {
                        return loginFactory.login(ctx.curr.email, password);
                    }
                };
            }]);
;

{'fraction_non_alphanumeric': 0.08614045104196404, 'fraction_numerical': 0.00014273479874393378, 'mean_word_length': 0.7078001218769043, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 151, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6798908', 'n_tokens_mistral': 5533, 'n_tokens_neox': 5112, 'n_words': 1193}