Phone Number Authentication for Firebase Through Unity (C#)

0

I’m trying to get phone number authentication for Unity up and running but I am getting no SMS response. The documentation online is super unclear about how to do this. I’d much rather have Google Auth for Firebase but apparently it isn’t supported yet. Can anyone help me figure out how to get this up and running?

Here is what I have so far…

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().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.
  });

}
}

Thanks in advance!

Here is my Firebase Console:

Update: Since the documentation to use Firebase along with Unity is really unclear I had to find an alternative. I found the solution. I am using TextLocal as a SMS gateway provider. Here’s the tutorial I followed on Youtube: How to Generate and Send OTP SMS For Free using C#.NET?[With Source Code] - YouTube

The tutorial is in C# but is not meant for Unity.* I modified the code to suit Unity and here’s my code:*

using System;
using System.Collections.Generic;
using System.Net;
using System.Collections.Specialized;
using System.IO;
using System.Text;
using UnityEngine;
using System.Collections.Generic;
using System.Net;
using System.Collections.Specialized;
using System.Threading.Tasks;
using System.Linq;
using Random = System.Random;
using UnityEngine.UI;

public class OTPSMS : MonoBehaviour
{
// public string txtPhone;
// public string txtName;
string randomNumber;
public GameObject MessageBox;
public GameObject MessageBox2;
public GameObject MessageBox3;
public GameObject MessageBox4;
public InputField txtPhone;
public InputField txtName;
public InputField txtVerOTP;

// Start is called before the first frame update
void Start()
{
    MessageBox.SetActive(false);
    MessageBox2.SetActive(false);
    MessageBox3.SetActive(false);
    MessageBox4.SetActive(false);
}

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

}

public void login()
{

    String result;
    string apiKey = "YOUR API KEY"; //The one which you get on TextLocal after registering . Follow the youtube tutorial
    string numbers = txtPhone.text; // in a comma seperated list
   
    string sender = "TXTLCL";
    string name = txtName.text;
    var rnd = new Random();
    randomNumber = (rnd.Next(100000, 999999)).ToString();
    string message = "Hey " +name+ " Your OTP is " +randomNumber ;

    String url = "https://api.textlocal.in/send/?apikey=" + apiKey + "&numbers=" + numbers + "&message=" + message + "&sender=" + sender;
    //refer to parameters to complete correct url string

    StreamWriter myWriter = null;
    HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(url);

    objRequest.Method = "POST";
    objRequest.ContentLength = Encoding.UTF8.GetByteCount(url);
    objRequest.ContentType = "application/x-www-form-urlencoded";
    try
    {
        myWriter = new StreamWriter(objRequest.GetRequestStream());
        myWriter.Write(url);
    }
    catch (Exception e)
    {
        MessageBox.SetActive(true);
    }
    finally
    {
        myWriter.Close();
    }

    HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
    using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
    {
        result = sr.ReadToEnd();
        // Close and clean up the StreamReader
        sr.Close();
    }
    MessageBox2.SetActive(true);

}

public void Verify()
{
    if(txtVerOTP.text ==randomNumber)
    {
        MessageBox3.SetActive(true);
    }
    else
    {
        MessageBox4.SetActive(true);
    }
}

}

And this is what the UI and hierarchy looks like: