firebase - 如何更改与 Firebase 简单登录中的用户关联的电子邮件?

标签 firebase angularfire firebase-security

正如标题所示,我希望提供功能,允许用户更新用于使用 Firebase Simple Login 登录我的应用的电子邮件。无法找出一种优雅的方法来做到这一点。应用程序使用 AngularFire(如果相关)。

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

最佳答案

Firebase 2.1.x 更新

Firebase SDK 现在提供 changeEmail method .

var ref = new Firebase('https://<instance>.firebaseio.com');
ref.changeEmail({
    oldEmail: '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3e555f4a517e5a51535f5750105d5153" rel="noreferrer noopener nofollow">[email protected]</a>',
    newEmail: '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e883899c87daa883899c87c68b8785" rel="noreferrer noopener nofollow">[email protected]</a>' ,
    password: '******'
}, function(err) {
    console.log(err ? 'failed to change email: ' + err : 'changed email successfully!');
});

Firebase 1.x 的历史答案

在简单登录中,这相当于更改用户的ID。所以没有办法即时执行此操作。只需创建新帐户,按照您的建议删除旧帐户即可。

如果您是 Firebase 中的用户个人资料,您也需要移动这些个人资料。这是迁移帐户(包括用户配置文件)的强力、安全方法。当然,您可以通过一些客观化和 future 来改进这一点:

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, '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a6979495e6c7c4c588c5c9cb" rel="noreferrer noopener nofollow">[email protected]</a>', '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c7f3f2f187a3a2a1e9a4a8aa" rel="noreferrer noopener nofollow">[email protected]</a>', '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 - 如何更改与 Firebase 简单登录中的用户关联的电子邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21500365/

相关文章:

angular - 类型 'AngularFireAction<DatabaseSnapshot<{}>>[]' 不可分配给类型 'Product[]'

javascript - 无法部署 Firestore Cloud Function

ios - 将文件上传到 Firebase 会跳过代码行吗?

android - Gradle 同步失败 - com.google.android.gms :play-services-basement is being requested by various other libraries

Firebase 存储安全规则 - 对象的属性大小未定义

firebase - 在 Firestore 规则中声明一个函数

firebase - 在 Firestore 安全规则中创建新文档时如何验证另一个文档引用

firebase react native : convert anonymous user to a permanent user via phone auth

firebase - 在 $onAuthStateChanged 上找到正确的 Firebase 身份验证 id_token

javascript - 唯一 IP 写入权限 Firebase 集合