拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 向已在firebase、flutter中使用电子邮件密码注册的用户添加电话号码验证

向已在firebase、flutter中使用电子邮件密码注册的用户添加电话号码验证

白鹭 - 2022-01-23 1999 0 0

在我的应用程序中,有一个登录荧屏、注册荧屏和一个 showProfile 荧屏。我正在使用电子邮件和密码注册用户。在注册荧屏中,有包含图片、名字、姓氏和电话号码的栏位。我可以通过执行此操作将用户显示名称、影像 URL 添加到特定用户 -

UserCredential user = await _auth.createUserWithEmailAndPassword(email: _email, password: _password);
var newUser = user.user;
if (newUser != null) {
    Reference imgReference = firebaseStorage.ref().child('UserImages/$_email');
    UploadTask uploadTask = imgReference.putFile(imgFile);
    TaskSnapshot taskSnapshot = await uploadTask;
    String url = await taskSnapshot.ref.getDownloadURL();
    if (url != null) {
       setState(() {
          imgUrl = url;
       });
    }
    await newUser.updatePhotoURL(imgUrl);
    await newUser.updateDisplayName(_firstName   ' '   _lastName);
    await newUser.reload();
}

现在我想在注册程序中向用户添加带有 OTP 验证的电话号码。. 但是如果不创建另一个带有电话号码的用户账户,我就无法为电话号码做同样的事情。

以便成功登录后,在我的 showProfile 荧屏中,我可以决议用户电话号码。通过user.phoneNumber到荧屏就像我可以为user.displayName & user.photoURL执行此操作而不会遇到任何问题。

有什么办法可以将电话号码添加到注册的电子邮件账户中吗?我搜索了它,但找不到任何可以开始的东西。

uj5u.com热心网友回复:

使用 Firebase 身份验证验证电话号码被视为使用该提供商登录。因此,不是将电话号码添加到现有登录,您实际上是要通过另一个提供商登录用户,然后将该新提供商链接到现有用户组态档。

这些步骤在很大程度上类似于使用电话号码登录,但不是signInWithCredential(credential)使用电话凭据呼叫,而是呼叫linkWithCredential(credential)当前用户,如账户链接档案中所示

上面是 Android 档案的链接,因为我觉得这些档案最清楚地记录了这个程序,而且这个程序跨平台是相似的。对于 FlutterFire 等效项,请参阅有关电话身份验证账户链接的档案

uj5u.com热心网友回复:

因此,经过长时间的搜索、查找和尝试解决方案,我终于能够解决我的问题。添加了使用 firebase OTP 验证的电话号码到已注册的电子邮件通行证账户。谢天谢地,这个答案对我来说非常好。这是代码:

final _auth = FirebaseAuth.instance;
final userInfo = _auth.currentUser;    

Future<void> phoneVerification() async {
        await _auth.verifyPhoneNumber(
          phoneNumber: _mobile,
          verificationCompleted: (PhoneAuthCredential phoneAuthCredential) async {
            /// This is the main important line to link
            await userInfo.linkWithCredential(phoneAuthCredential);
            // Not nessecary
            /* User? refreshedUser = await refreshUser(userInfo);
            if (refreshedUser != null) {
              setState(() {
                userInfo = refreshedUser;
              });
            } */
          },
          verificationFailed: (FirebaseAuthException e) {
           // Handle error
            print(e);
          },
          codeSent: (String verificationId, int? resendToken) async {
            // write the code to handle the OTP code sent for manual verification in case autoverify doesn't work
          },
          codeAutoRetrievalTimeout: (String verificationId) {
            // Do Something for SMS request timeout
          },
        );
      }

标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *