forked from Coco3D/Firebase-Authentication
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhoneAuth
102 lines (89 loc) · 3.7 KB
/
PhoneAuth
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using UnityEngine;
using TMPro;
using Firebase.Auth;
using Firebase.Extensions;
public class PhoneAuth : MonoBehaviour
{
public TMP_InputField phoneInp,EnterCode_Inp;
public TextMeshProUGUI logTxt;
public GameObject AllOptions;//all options
public GameObject SuccessUi;
uint phoneAuthTimeoutMs = 3 * 60000;//minutes to milisec
FirebaseAuth auth;
PhoneAuthProvider provider;
private void Start()
{
auth = FirebaseAuth.DefaultInstance;
provider = PhoneAuthProvider.GetInstance(auth);
}
public void sendSMS() {
print("send sms pressed");
string userNumber = "+91" + phoneInp.text;
PhoneAuthProvider provider = PhoneAuthProvider.GetInstance(auth);
provider.VerifyPhoneNumber(
new PhoneAuthOptions
{
PhoneNumber = userNumber,
TimeoutInMilliseconds = 60,
ForceResendingToken = null
},
verificationCompleted: (credential) => {
print("Verification completed");
showLogMsg("Verification completed");
// 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) => {
print("Error: " + error);
showLogMsg(error);
// The verification code was not sent.
// `error` contains a human readable explanation of the problem.
},
codeSent: (id, token) => {
showLogMsg("Code send success!!");
// Verification code was successfully sent via SMS.
// `id` contains the verification id that will need to passed in with
PlayerPrefs.SetString("verificationId",id);
// 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.
});
}
public void VerifyOtp() {
print("verify otp pressed");
string verificationId = PlayerPrefs.GetString("verificationId");
string verificationCode = EnterCode_Inp.text;
PhoneAuthCredential credential =provider.GetCredential(verificationId, verificationCode);
auth.SignInAndRetrieveDataWithCredentialAsync(credential).ContinueWithOnMainThread(task => {
if (task.IsFaulted)
{
Debug.LogError("SignInAndRetrieveDataWithCredentialAsync encountered an error: " +
task.Exception);
return;
}
FirebaseUser newUser = task.Result.User;
Debug.Log("User signed in successfully");
showLogMsg("Success");
// 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);
AllOptions.SetActive(false);
SuccessUi.SetActive(true);
SuccessUi.transform.Find("Desc").GetComponent<TextMeshProUGUI>().text = "Id: " + task.Result.User.UserId;
});
}
#region extra
void showLogMsg(string msg)
{
logTxt.text = msg;
logTxt.GetComponent<Animation>().Play("textFadeout");
}
#endregion
}