'How to check if email or phone is already signed up Firebase Unity SDK?
I am using Firebase Unity SDK and I use phone numbers to signup users with the option to link an email address as well. The problem is that the system always signs up the user as new once they enter the OTP (when verifying the phone number in registration) even if the phone number is already signed up in the Authentication. I was looking for a way to check if the phone number is already signed up before proceeding to register the user but only found the getUserByPhone method that is in the Admin SDK (im using regular Firebase), same thing for checking if the email is in use before linking it to the user.
My signup code:
phoneAuthProvider = PhoneAuthProvider.GetInstance(FirebaseAPI.Auth);
phoneAuthProvider.VerifyPhoneNumber(UserData.phoneNo, 60000, null,
verificationCompleted: (credential) => {
Debug.Log("Phone Auth, auto-verification completed");
},
verificationFailed: (error) => {
Debug.LogError("Phone verification failed: " + error);
},
codeSent: (id, token) => {
phoneAuthID = id;
Prompt_OTP promptInstantiated = OpenPrompt(prompt_OTP).GetComponent<Prompt_OTP>();
promptInstantiated.Init(verifyOTP, null, null);
Debug.Log("Code sent to: " + UserData.phoneNo);
},
codeAutoRetrievalTimeOut: (id) => {
Debug.Log("Phone Auth, auto-verification timed out");
});
OTP verification:
Credential credential = phoneAuthProvider.GetCredential(phoneAuthID, OTP_StringField.Value);
if (credential == null)
{
OnFail("Failed, please try again\n" + phoneAuthID +"," + OTP_StringField.Value);
Debug.LogError("Failed, please try again\n" + phoneAuthID + "," + OTP_StringField.Value);
return;
}
FirebaseAPI.Auth.SignInWithCredentialAsync(credential).ContinueWith(task =>
{
if (task.IsFaulted)
{
Debug.LogError("Signing in with credentials errored: " + task.Exception);
OnFail("OTP verification error, please try again");
return;
}
FirebaseUser user = task.Result;
FirebaseAPI.User = user;
user.TokenAsync(true).ContinueWithOnMainThread(task =>
{
if (task.IsCanceled || task.IsFaulted)
{
Debug.Log("Could not get token");
OnFail("Failed, please try again");
return;
}
UserData.tokenID = task.Result;
authToken = task.Result;
});
AddUserToDB();
});
Email linking:
Credential credential = EmailAuthProvider.GetCredential(email, UserData.user_password);
Debug.Log("Email to link with: " + email);
if(credential != null)
{
FirebaseAPI.Auth.CurrentUser.LinkWithCredentialAsync(credential).ContinueWith(task =>
{
if (task.IsCanceled)
{
Debug.LogError("Linking with credentials canceled");
return;
}
if (task.IsFaulted)
{
Debug.LogError("Linking with credentials errored: " + task.Exception);
return;
}
FirebaseUser user = task.Result;
});
}
Solution 1:[1]
There is no call in the client-side APIs for Firebase to find a user by their phone number, or even to check if a phone number is already registered.
The two most common options are to:
Either write your own custom API endpoint, where you then implement the functionality using the Admin SDK.
Implement your own custom storage of the phone numbers that are used in your app, using a cloud-hosted database such as the ones in Firebase.
Whichever of these solutions you pick, be sure to secure access to the list of phone numbers properly.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Frank van Puffelen |
