如何在Flutter中将匿名帐户链接到电话号码?

时间:2018-10-01 21:12:24

标签: firebase firebase-authentication flutter

在Firebase身份验证中,通常可以将其设为described here
但是,对于Flutter,只有三种预制的链接方法,即 linkWithEmailAndPassword linkWithGoogleCredential linkWithFacebookCredential >。

我已经发现该in Flutter没有直接实现。
我想知道目前是否有任何方法可以实现这一目标。

1 个答案:

答案 0 :(得分:2)

firebaseAuth = FirebaseAuth.instance;

您可以使用verifyPhoneNumber发送验证短信,并从verificationID回调中检索创建PhoneAuthCredential所需的codeSent

firebaseAuth.verifyPhoneNumber(phoneNumber: ..., codeSent: (verificationId, [_]) async {
    // you need to get the SMS code from the user before the next step, e.g. by using a TextField
    // in this example that would be stored in [codeRetrieved]
    (await firebaseAuth.currentUser()).linkWithCredential(
        PhoneAuthProvider.getCredential(verificationId: verificationId, smsCode: codeRetrieved)
    );
  });

如果用户在Android上自动获得验证,则可以使用verificationCompleted,它为您提供了预先配置的凭据:

firebaseAuth.verifyPhoneNumber(..., verificationCompleted: (credential) async {
          (await firebaseAuth.currentUser()).linkWithCredential(credential);
        });