Question

In this git issue AngularFire disabled the auto-login functionality associated with $createUser. (The docs are outdated.)

So what's the best practice / cleanest way to create a user and then log them in?

 app.factory('Auth', function($firebaseSimpleLogin, FIREBASE_URL, $rootScope) { 
    var ref = new Firebase(FIREBASE_URL);
    var auth = $firebaseSimpleLogin(ref);

    var Auth = {
        register: function (user) {
            return auth.$createUser(user.email, user.password);
        },
        login: function (user) {
            return auth.$login('password', user);
        },
        ...

Note: $createUser returns a md5 hash but $login uses plaintext password.

I'm new to promises. Thanks!

Was it helpful?

Solution

I didn't realize what Kato meant. But now that I understand promises the solution is pretty straight forward:

    register: function (user) {
        return auth.$createUser(user.email, user.password)
            .then(function() {
                return auth.$login('password', user);
            });
    },

OTHER TIPS

I think this should do it. You have to use q or inject angular.$q.

app.factory('Auth', function($firebaseSimpleLogin, FIREBASE_URL, $rootScope) { 
var ref = new Firebase(FIREBASE_URL);
var auth = $firebaseSimpleLogin(ref);
var deferred = Q.defer(); // from Q lib
// var deferred = $q.defer(); //angularjs q

var Auth = {
    register: function (user) {

        auth.$createUser(user.email, user.password)
        .then(function(result) {
          deferred.notify('user created.'); // optional notify progress
          return auth.$login('password', user); // return a new promise for chaining.
        },
        function(reason) {
          // createUser failed
          deferred.reject(reason);
        })
        .then(function(result) {
          // resolve the login promise return from above
          deferred.resolve(result);
        },
        function(reason) {
          // login failed
          deferred.reject(reason);
        });

        return deferred;
    },
    login: function (user) {
        return auth.$login('password', user);
    },
    ...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top