通过Unity实现Firebase的电话号码身份验证(C#)

时间:2018-02-06 04:35:04

标签: c# firebase unity3d firebase-authentication

我正在尝试为Unity启动并运行电话号码身份验证,但我没有收到短信响应。在线文档对于如何执行此操作非常不清楚。我宁愿使用Google Auth for Firebase,但显然它还不支持。任何人都可以帮我弄清楚如何启动并运行吗?

这是我到目前为止所拥有的......

public class SignIn : MonoBehaviour {

private string phoneNumber;
private string secureCode;
private uint phoneAuthTimeoutMs;
private Firebase.Auth.FirebaseAuth firebaseAuth;
private Credential credential;
private string verificationId;
private string verificationCode;

public Text PhoneNumberInputFieldText;
public Text SecureCodeInputFieldText;
public Button SendSecureCodeButton;
public Button SubmitSecureCodeButton;

// Use this for initialization
void Start()
{
    SendSecureCodeButton.GetComponent<Button>().onClick.AddListener(() => StartSignIn());



}
// Update is called once per frame
void Update () {

}

void StartSignIn()
{
    firebaseAuth = Firebase.Auth.FirebaseAuth.DefaultInstance;
    phoneAuthTimeoutMs = 60000;
    phoneNumber = PhoneNumberInputFieldText.text;
    Debug.Log(phoneNumber);

    if (PlayerPrefs.GetString("secureCode") != null)
    {
        verificationId = PlayerPrefs.GetString("secureCode");
    }
    PhoneAuthProvider provider = PhoneAuthProvider.GetInstance(firebaseAuth);
    provider.VerifyPhoneNumber(phoneNumber, phoneAuthTimeoutMs, null,
      verificationCompleted: (credential) =>
      {
          // Auto-sms-retrieval or instant validation has succeeded (Android only).
          // There is no need to input the verification code.
          // `credential` can be used instead of calling GetCredential().
      },
      verificationFailed: (error) =>
      {
          // The verification code was not sent.
          // `error` contains a human readable explanation of the problem.
      },
      codeSent: (id, token) =>
      {
          //Prompt here to type in SecureCode
          PlayerPrefs.SetString("secureCode", id);


          credential = provider.GetCredential(verificationId, verificationCode);

          firebaseAuth.SignInWithCredentialAsync(credential).ContinueWith(task => {
              if (task.IsFaulted)
              {
                  Debug.LogError("SignInWithCredentialAsync encountered an error: " +
                                  task.Exception);
                  return;
              }

              FirebaseUser newUser = task.Result;
              Debug.Log("User signed in successfully");
              // This should display the phone number.
              Debug.Log("Phone number: " + newUser.PhoneNumber);
              // The phone number providerID is 'phone'.
              Debug.Log("Phone provider ID: " + newUser.ProviderId);
          });

          // Verification code was successfully sent via SMS.
          // `id` contains the verification id that will need to passed in with
          // the code from the user when calling GetCredential().
          // `token` can be used if the user requests the code be sent again, to
          // tie the two requests together.
      },
      codeAutoRetrievalTimeOut: (id) =>
      {
          // Called when the auto-sms-retrieval has timed out, based on the given
          // timeout parameter.
          // `id` contains the verification id of the request that timed out.
      });


}

}

提前致谢!

以下是我的Firebase控制台:

enter image description here

1 个答案:

答案 0 :(得分:0)

我会发一个答案,但我会编辑这个。

尝试更简单的发送短信电话验证

Firebase.Auth.FirebaseAuth auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
phoneNumber = "your phone number here";//state the phone number here first
uint phoneAuthTimeoutMs = 10000;
PhoneAuthProvider provider = PhoneAuthProvider.GetInstance(auth);
provider.VerifyPhoneNumber(phoneNumber, phoneAuthTimeoutMs,null,
    verificationCompleted: (credential) => {

    },
    verificationFailed: (error) => {

    },
    codeSent: (id, token) => {
        MyText.text = "SMS Has been sent " + id;
    },
    codeAutoRetrievalTimeOut : (id) => {

    });
MyText.text += "HMM";

然后请告诉我它是否有效。现在,如果它有效,我们将继续下一步