Pregunta

As the title suggests I would like to provide functionality to allow a user to update the email they use to login to my app using Firebase Simple Login. Cannot figure out an elegant way to do this. App uses AngularFire if that is relevant.

Does one exist or do I need to create a new account and delete the old one using the $removeUser() and $createUser() methods?

¿Fue útil?

Solución

Update for Firebase 2.1.x

The Firebase SDK now provides a changeEmail method.

var ref = new Firebase('https://<instance>.firebaseio.com');
ref.changeEmail({
    oldEmail: 'kato@domain.com',
    newEmail: 'kato2@kato.com' ,
    password: '******'
}, function(err) {
    console.log(err ? 'failed to change email: ' + err : 'changed email successfully!');
});

Historical answer for Firebase 1.x

In Simple Login, this is equivalent to changing the user's ID. So there is no way to do this on the fly. Simply create the new account, remove the old one as you have already suggested.

If you're user profiles in Firebase, you'll want to move those as well. Here's brute force, safe method to migrate an account, including user profiles. You could, naturally, improve upon this with some objectification and futures:

var ref   = new Firebase('URL/user_profiles');
var auth  = new FirebaseSimpleLogin(ref);

// assume user has logged in, or we obtained their old UID by
// looking up email address in our profiles
var oldUid = 'simplelogin:123';

moveUser( auth, ref, oldUid, '123@abc.com', '456@def.com', 'xxx' );

function moveUser( auth, usersRef, oldUid, oldId, newId, pass ) {

   // execute activities in order; first we copy old paths to new
   createLogin(function(user) {
      copyProfile(user.uid, function() {
         // and once they safely exist, then we can delete the old ones
         removeOldProfile();
         removeOldLogin();
      });
   });   

   function copyProfile(newId, next) {
      ref.child(oldUid).once('value', function(snap) {
         if( snap.val() !== null ) {
            ref.child(newId, snap.val(), function(err) {
               logError(err);
               if( !err ) { next(); }
            });
         }
      });
   }

   function removeOldProfile() {
      ref.child(oldId).remove(logError);
   }

   function createLogin(next) {
      auth.createUser(newId, pass, function(err, user) {
         logError(err);
         if( !err ) { next(user); }
      });
   }

   function removeOldLogin() {
      auth.removeUser(oldId, pass, logError);
   }
}

function logError(err) {
   if( err ) { console.error(err); }
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top