# #android #android-studio #kotlin #firebase-authentication
Вопрос:
- Я работал над аутентификацией телефона firebase, я просто хочу проверить устройство пользователей. при получении 6-значного кода он должен проверить телефон, но его проверка не выполняется. сообщение об ошибке — Невозможно создать учетную запись PhoneAuthCredential без проверки подлинности, sessionInfo, временного подтверждения или идентификатора регистрации.
- Я загрузил SHA 1 в консоль, при отправке телефонного номера я получаю код, и идентификатор проверки не равен нулю
// Initialize phone auth callbacks-[start]
callBacks = object : PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
override fun onVerificationCompleted(credential: PhoneAuthCredential) {
Log.d(TAG, "onVerificationCompleted: $credential")
signInWithPhoneAuthCredential(credential)
}
override fun onVerificationFailed(e: FirebaseException) {
progressDialog.dismiss()
Log.w(TAG, "onVerificationFailed", e)
if (e is FirebaseAuthInvalidCredentialsException){
setToast("Invalid OTP",1)
}
}
override fun onCodeSent(
verificationId: String,
token: PhoneAuthProvider.ForceResendingToken
) {
Log.d(TAG, "onCodeSent:$verificationId")
// Save verification ID and resending token so we can use them later
storedVerificationId = verificationId
resendToken = token
progressDialog.dismiss()
// super.onCodeSent(verificationId, token)
}
}
//phone auth callbacks-[End]
//Starting the Verification-sending the phone number-inside onCreate
val number = " 91 ${enteredNumber}"
startPhoneNumberVerification(number)
//Fun to send the ph number for verification[START]- 1
private fun startPhoneNumberVerification(phoneNumber: String) {
progressDialog.setMessage("Getting the OTP..")
progressDialog.show()
val options = PhoneAuthOptions.newBuilder(auth)
.setPhoneNumber(phoneNumber)
.setTimeout(60L, TimeUnit.SECONDS)
.setActivity(this)
.setCallbacks(callBacks)
.build()
PhoneAuthProvider.verifyPhoneNumber(options)
}
//Fun to send the ph number for verification[END]
//Verifying Code [START]-2
private fun verifyPhoneNumberWithCode(verificationId: String?, code: String) {
try {
progressDialog.setMessage("Verifying Phone Number..")
progressDialog.show()
val credential = PhoneAuthProvider.getCredential(verificationId!!,code)
signInWithPhoneAuthCredential(credential)
}
catch (e:Exception){
setToast("verifyPhoneNumberWithCode error",1)
binding.txtErrorMsgs.text="verifyPhoneNumberWithCode error"
Log.d(TAG,e.toString())
}
}
//Verifying Code [END]
// [START sign_in_with_phone]-3
private fun signInWithPhoneAuthCredential(credential: PhoneAuthCredential){
// FirebaseAuth.getInstance().
auth.signInWithCredential(credential)
.addOnCompleteListener(this){task->
if (task.isSuccessful){
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "signInWithCredential:success")
val user=task.result?.user?.phoneNumber
val intent = Intent(this, MainActivity::class.java)
startActivity(intent)
}
else{
// Sign in failed, display a message and update the UI
progressDialog.dismiss()
Log.w(TAG, "signInWithCredential:failure", task.exception)
if (task.exception is FirebaseAuthInvalidCredentialsException){
progressDialog.dismiss()
setToast("Invalid OTP",1)
}
}
}
}
// [END sign_in_with_phone]