Firebase验证码检查失败,并阻止用户进行身份验证

时间:2020-11-11 12:38:19

标签: android firebase kotlin firebase-authentication

我不知道为什么会弹出此错误。当我在其他设备上运行该应用程序时,该应用程序运行良好,但是当我在其他设备上遇到问题时。当我通过wifi访问Firebase时,我也遇到问题。我已经联系了Firebase支持人员,他们在印度说他们正面临服务器和isp问题

otpVerification.kt

const val USER_REF:String="user"
class OtpVerification : AppCompatActivity() {


lateinit var  auth: FirebaseAuth

private var mAuthVerificationId: String? = null

private var mOtpText: EditText? = null
private var mVerifyBtn: Button? = null

private var mOtpProgress: ProgressBar? = null

private var mOtpFeedback: TextView? = null

lateinit var email:String
lateinit var password: String

private lateinit var database: DatabaseReference

lateinit  var uname:String


lateinit  var mCurrentUser: FirebaseUser
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_otp_verification)


    mAuthVerificationId = intent.getStringExtra("AuthCredentials")
    Log.d("AuthCredentials", mAuthVerificationId.toString())

    email= intent.getStringExtra("email").toString()
    password= intent.getStringExtra("password").toString()
    uname= intent.getStringExtra("name").toString()

    mOtpFeedback = findViewById(R.id.otp_form_feedback)
    mOtpProgress = findViewById(R.id.otp_progress_bar)
    mOtpText = findViewById(R.id.otp_text_view)


    auth=Firebase.auth

    database = FirebaseDatabase.getInstance().reference


    mVerifyBtn = findViewById(R.id.verify_btn)

    mVerifyBtn?.setOnClickListener(View.OnClickListener {
        val otp = mOtpText?.text.toString()
        if (otp.isEmpty()) {
            mOtpFeedback?.visibility = View.VISIBLE
            mOtpFeedback?.text = "Please fill in the form and try again."
        } else {
            mOtpProgress?.visibility = View.VISIBLE
            mVerifyBtn?.isEnabled = false
            val credential = PhoneAuthProvider.getCredential(mAuthVerificationId!!, otp)
            signInWithPhoneAuthCredential(credential)
        }
    })
  }

  private fun signInWithPhoneAuthCredential(credential: PhoneAuthCredential) {
    auth?.signInWithCredential(credential)
            ?.addOnCompleteListener(this,
                    OnCompleteListener<AuthResult?> { task ->
                        if (task.isSuccessful) {

                            createAccount(email,password)
                            mCurrentUser= auth.currentUser!!
                            Prefs.put(this, mCurrentUser)
                            Log.d("user",mCurrentUser.toString())
                            sendUserToHome()
                            Log.d("TAG", "Task is succesful")
                            // ...
                        } else {
                            if (task.exception is FirebaseAuthInvalidCredentialsException) {
                                // The verification code entered was invalid
                                mOtpFeedback?.visibility = View.VISIBLE
                                mOtpFeedback?.text = "There was an error verifying OTP"
                            }
                        }
                        mOtpProgress?.visibility = View.INVISIBLE
                        mVerifyBtn?.isEnabled = true
                    })
 }


 fun sendUserToHome() {
    val intent = Intent(this, MainActivity::class.java)
    intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
    startActivity(intent)
    finish()
}

private fun createAccount(email: String, password: String) {
    Log.d("TAG", "createAccount:${email.toString()}")
    //Log.d("TAG", "validate form is ${validateForm().toString()}")

    // [START create_user_with_email]
    auth.createUserWithEmailAndPassword(email, password)
            .addOnCompleteListener(this) { task ->
                if (task.isSuccessful) {
                    // Sign in success, update UI with the signed-in user's information
                    Log.d("TAG", "createUserWithEmail:success")
                    val user = auth.currentUser

                    Toast.makeText(baseContext, "Authentication Success $user", Toast.LENGTH_SHORT)
                            .show()
                    sendEmailVerification()

                    val intent = Intent(this, MainActivity::class.java)
                    intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
                    startActivity(intent)
                    Log.d("Delete", "deleting activity signup")

                    finish()

                } else {
                    // If sign in fails, display a message to the user.
                    Log.w("TAG", "createUserWithEmail:failure", task.exception)
                    Toast.makeText(
                            baseContext, "Authentication failed.",
                            Toast.LENGTH_SHORT
                    ).show()
                    //                   updateUI(null)
                }

                // [START_EXCLUDE]

                // [END_EXCLUDE]
            }
    // [END create_user_with_email]
createUSer()
}

private fun sendEmailVerification() {
           // Send verification email
    // [START send_email_verification]
    val user = auth.currentUser!!
    user.sendEmailVerification()
            .addOnCompleteListener(this) { task ->

                if (task.isSuccessful) {
                    Toast.makeText(baseContext,
                            "Verification email sent to ${user.email} ",
                            Toast.LENGTH_SHORT).show()

                    Log.d("TAG", "sendEmailVerification")
                } else {
                    Log.d("TAG", "sendEmailVerification", task.exception)
                    Toast.makeText(baseContext,
                            "Failed to send verification email.",
                            Toast.LENGTH_SHORT).show()
                }
                // [END_EXCLUDE]
            }
    // [END send_email_verification]
}

fun createUSer(){
    database.child(USER_REF).child(uname).child("email").setValue(email)
    Log.d("CREATED","create user"+uname)
}


}

logcat

2020-11-11 17:57:45.290 28364-28364/com.bva_valai_pada E/zza: Problem 
retrieving SafetyNet Token: 7: 
2020-11-11 17:57:45.358 28364-28364/com.bva_valai_pada W/ActivityThread: 
handleWindowVisibility: no activity for token android.os.BinderProxy@5308a64
2020-11-11 17:57:45.873 28364-30352/com.bva_valai_pada E/FirebaseAuth: 
[GetAuthDomainTask] Error getting project config. Failed with {
  "error": {
    "code": 400,
    "message": "INVALID_CERT_HASH",
    "errors": [
      {
        "message": "INVALID_CERT_HASH",
        "domain": "global",
        "reason": "invalid"
      }
    ]
  }
}
 400
2020-11-11 17:57:45.919 28364-28364/com.bva_valai_pada E/zza: Failed to get 
reCAPTCHA token - calling backend without app verification
2020-11-11 17:57:45.962 28364-28364/com.bva_valai_pada I/AssistStructure: 
Flattened final assist data: 4392 bytes, containing 1 windows, 13 views
2020-11-11 17:57:45.966 28364-28410/com.bva_valai_pada W/System: Ignoring 
header X-Firebase-Locale because its value was null.
2020-11-11 17:57:46.611 28364-28410/com.bva_valai_pada E/FirebaseAuth: 
[SmsRetrieverHelper] SMS verification code request failed: unknown status 
code: 17093 null
2020-11-11 17:57:46.621 28364-28364/com.bva_valai_pada D/Exception: 
com.google.firebase.auth.FirebaseAuthException: This request is missing a 
valid app identifier, meaning that neither SafetyNet checks nor reCAPTCHA 
checks succeeded. Please try again, or check the logcat for more details.

3 个答案:

答案 0 :(得分:4)

您的问题与 SHA1 和 SHA256 密钥有关。您必须添加它们以验证您的证书。

  • 列表项

  • 转到

    您项目的 Firebase 控制台身份验证项目设置(来自 项目概览附近的设置按钮)添加指纹添加SHA-1和 密钥库的 SHA-256 值。您可以从 gradle 获取密钥库 像这样。

<块引用>

./gradlew 签名报告

阅读本文了解更多信息:https://firebase.google.com/docs/auth/android/phone-auth#enable-app-verification

答案 1 :(得分:2)

<块引用>

第 1 步:选择一个项目。

第 2 步:启用 Android 设备验证

使用以下链接启用 Android 设备验证。

https://console.cloud.google.com/apis/library/androidcheck.googleapis.com

enter image description here

答案 2 :(得分:0)

嗯,您似乎需要在Firebase控制台的应用程序注册中添加SHA-1证书哈希。我想您可以找到执行此操作的说明here