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

编程入门 行业动态 更新时间:2024-10-27 16:24:43
本文介绍了您如何在 firebase 简单登录中更改与用户关联的电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

正如标题所暗示的,我想提供一种功能,允许用户使用 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');
ref.changeEmail({
    oldEmail: 'kato@domain',
    newEmail: 'kato2@kato' ,
    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', '456@def', '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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-22 11:19:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1023496.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:电子邮件   简单   用户   如何在   firebase

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!