I'm working on an android app which require Firebase phone authentication, When I released my app, a recapture still required as in debug mode, I enabled Android Device Verification in google cloud console, also I added google play console SHA-256, SHA-1 to Firebase project setting.
Bellow my Kotlin code of Firebase authentication:
fun initFirebase() {
mAuth = FirebaseAuth.getInstance()
}
fun sendOtp(mobile: String, resend: Boolean) {
if (resend) resend(mobile)
else send(mobile)
}
private fun send(mobile: String) {
val options = PhoneAuthOptions.newBuilder(mAuth)
.setPhoneNumber(mobile)
.setTimeout(5L, TimeUnit.SECONDS)
.setActivity(requireActivity())
.setCallbacks(mCallbacks)
.build()
PhoneAuthProvider.verifyPhoneNumber(options)
}
Also at many time I get VerificationFailed without getting reason of this failure
private val mCallbacks = object : PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
override fun onVerificationCompleted(authCredential: PhoneAuthCredential) {}
override fun onVerificationFailed(e: FirebaseException) {
events.value = Constant.EVENT_SEND_FAILED
Log.v("TAG","VerificationFailed")
}
override fun onCodeSent(
verificationId: String,
forceResendingToken: PhoneAuthProvider.ForceResendingToken
) {
Log.v("TAG","Success")
}
and in verification fragment:
private lateinit var mAuth: FirebaseAuth
var mVerificationId: String = ""
fun verifyOtp(otp: String) {
val credential = PhoneAuthProvider.getCredential(
mVerificationId,
otp
)
mAuth.signInWithCredential(credential).addOnCompleteListener(requireActivity()) {
if (it.isSuccessful) {
mAuth.currentUser?.getIdToken(true)?.addOnCompleteListener { task ->
if (task.isSuccessful && task.result != null) {
val phoneVerificationToken = task.result.token
if (phoneVerificationToken != null) {
Log.v("TAG","Success")
}
} else {
Log.v("TAG","Error")
}
}
} else {
Log.v("TAG","Invalid code")
}
}
}