如何在 firebase_auth 中更新 FirebaseUser 的电话号码? [英] How do I update a FirebaseUser's phone number in firebase_auth?

查看:18
本文介绍了如何在 firebase_auth 中更新 FirebaseUser 的电话号码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 Flutter 应用程序中,我使用 Firebase 的电话号码身份验证作为我的主要身份验证形式.身份验证后,我在我的 users 集合中创建一个用户,其中包含以下详细信息:

In my Flutter app I use Firebase's phone number authentication as my main form of authentication. After authenticating, I create a user in my users collection with these details:

{
    phoneNumber: FirebaseAuth.instance.currentUser().phoneNumber,
    displayName: 'Comes from user textbox',
    ...
}

但是假设有一天用户想要更改他们的电话号码.我该怎么做呢?因为我不能简单地更改文档中用户的电话号码,因为电话号码需要进行身份验证.并且在认证之后,用户得到一个新的authUID.那么哪个应该是新用户?

But say one day a user want's to change their phone number. How do I do this? Because I cannot simply change the user's phone number in the document, because the phone number needs to be authenticated. And after authentication, the user gets a new authUID. Which should then be a new user?

有人可以解释想要保留其个人资料详细信息但更改其号码的用户背后的逻辑吗.

Could someone explain the logic behind a user that wants to keep their profile details but change their number.

推荐答案

为了实现这一点,您可以使用 FirebaseUser.updatePhoneNumberCredential.这允许您更新用户的电话号码.

In order to achieve this, you can use FirebaseUser.updatePhoneNumberCredential. This allows you to update the phone number of a user.

您会以与首先使用电话号码进行身份验证的方式相同的方式使用它(使用 signInWithCredential),即您使用 FirebaseAuth.verifyPhoneNumber 并传递您从 verificationCompleted 获得的凭证 或您的用户,当他们输入收到的 SMS 代码时.我只会勾勒出这会是什么样子,因为我假设您知道如何执行此任务:

You would use it in the same manner that you also authenticated with phone number in the first place (using signInWithCredential), i.e. you retrieve a credential using FirebaseAuth.verifyPhoneNumber and pass the credential that you get from either verificationCompleted or your user when they enter the SMS code they received. I will only sketch out what this would look like as I assume that you know how to perform this task:

FirebaseAuth.instance.verifyPhoneNumber(
        phoneNumber: phoneNumber,
        timeout: const Duration(minutes: 2),
        verificationCompleted: (credential) async {
          await (await FirebaseAuth.instance.currentUser()).updatePhoneNumberCredential(credential);
          // either this occurs or the user needs to manually enter the SMS code
        },
        verificationFailed: null,
        codeSent: (verificationId, [forceResendingToken]) async {
          String smsCode;
          // get the SMS code from the user somehow (probably using a text field)
          final AuthCredential credential =
            PhoneAuthProvider.getCredential(verificationId: verificationId, smsCode: smsCode);
          await (await FirebaseAuth.instance.currentUser()).updatePhoneNumberCredential(credential);
        },
        codeAutoRetrievalTimeout: null);

当调用 updatePhoneNumberCredential 时,您可能还想更新您的数据库文档.或者,您可以收听 onAuthStateChanged 并以这种方式更新您的文档.

When updatePhoneNumberCredential is called, you probably also want to update your database document. Alternatively, you could listen to onAuthStateChanged and update your document this way.

这篇关于如何在 firebase_auth 中更新 FirebaseUser 的电话号码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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