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

查看:19
本文介绍了您如何在 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() 方法删除旧帐户?

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天全站免登陆