如何更改与firebase简单登录用户关联的电子邮件? [英] How do you change the email associated with a user in firebase simple login?

查看:168
本文介绍了如何更改与firebase简单登录用户关联的电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所示,我想提供一些功能,允许用户使用Firebase简单登录来更新他们用来登录我的应用程序的电子邮件。无法找出一个优雅的方式来做到这一点。如果这是相关的应用程序使用AngularFire

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.

是否存在或我需要创建一个新的帐户,并删除旧的使用 $ removeUser () $ createUser() methods?

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

推荐答案

Firebase 2.1.x更新

Firebase SDK现在提供 changeEmail方法

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!');
});

Firebase 1.x的历史回答

在简单登录中,这相当于更改用户的ID。所以没有办法在飞行中这样做。您只需创建新帐户,然后按照您的建议移除旧帐户即可。

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.

如果您是Firebase中的用户个人资料,则还需要移动这些帐户。这里是蛮力,安全的方法来迁移一个帐户,包括用户配置文件。你可以自然而然地用一些客观化和期货来改善这一点:

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); }
}

这篇关于如何更改与firebase简单登录用户关联的电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆