I am working on an android app, in where I just want to verify mobile number without creating a user account. Is it Possible? I am using the following code
private void startPhoneNumberVerification(String phoneNumber) {
PhoneAuthProvider.getInstance().verifyPhoneNumber(
phoneNumber, // Phone number to verify
60, // Timeout duration
TimeUnit.SECONDS, // Unit of timeout
this, // Activity (for callback binding)
mCallbacks); // OnVerificationStateChangedCallbacks
}
private void verifyPhoneNumberWithCode(String verificationId, String code) {
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId, code);
signInWithPhoneAuthCredential(credential); // this function is creating user account , if not present. But We Don't want this
}
The following function will create user account if user account is not there, but I don't want to create account, I just want to verify the code entered by the user. Is there any call back method available for that?
private void signInWithPhoneAuthCredential(final PhoneAuthCredential credential) {
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
dialog.dismiss();
FirebaseUser user = task.getResult().getUser();
Toast.makeText(LoginActivity.this, "Success " + user.getEmail(), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(LoginActivity.this, "Failed ", Toast.LENGTH_SHORT).show();
verifyPhoneNumberWithCode(mVerificationId, editText.getText().toString().trim());
}
}
});
}
In the Firebase console, open the Authentication section. In the Sign in method tab, enable the Phone provider if you haven't already. Open the Phone numbers for testing accordion menu. Provide the phone number you want to test, for example: +1 650-555-3434.
Phone-based authentication involves sending a one-time password (OTP) to a user over a separate communication channel (e.g. SMS, MMS, WhatsApp, Facebook Messenger, Viber or even voice) from the IP channel (internet) used by the application, providing security in case the IP channel is compromised.
In phone authentication, the user has to verify his identity with his phone number. Inside the app user has to enter his phone number after that he will receive a verification code on his mobile number. He has to enter that verification code and verify his identity.
Yes, you can use Firebase for auth only and link it to your own database. You'll have to use the Firebase Admin SDK in your backend. Check out the set up guide.
You can't verify what the user typed without linking the phone provider to the Firebase user in the process.
But you can unlink the phone from the user account soon after, by calling:
FirebaseAuth.getInstance().getCurrentUser().
unlink(PhoneAuthProvider.PROVIDER_ID)
.addOnCompleteListener(this, onCompleteListener);
There are plenty of uses for verifying that the user has access to this phone number, but shouldn't login with it. I really think that Firebase should allow developers to verify first, and use the credential to login after.
Also:
There is a good chance of Google Play Services verifying automatically. When onVerificationCompleted(PhoneAuthCredential)
in your PhoneAuthProvider.OnVerificationStateChangedCallbacks
is called. This way the user won't need to type the verification code, and the phone won't be linked automatically.
Verifying a phone number automatically creates a Firebase Authentication account for that user. There is no way to prevent creating this account, as it is what Firebase uses to ensure it knows that user next time they start the app.
You can also update the phone number on the user account.
This will not create new user; instead, it will update the phone number in the existing user account.
val credential = PhoneAuthProvider.getCredential(verificationId!!, smsCode)
FirebaseAuth.getInstance().currentUser?.updatePhoneNumber(credential)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With