0

For days. I am trying to make a login with Firebase in Kotlin using Phone Authentication. I have spent days going around in a circle with this code. I'm new to this. I've added my Package name, SHA-1, SHA-256, generated the API keys and i had the latest google-services.json file. Here I left a portion of the code. If you could help me find the error. When trying to login from external device it throws a FirebaseAuthException: (We have blocked all requests from this device due to unusual activity), And now, from any different device or emulator i got this message ["onVerificationFailed / com.google.firebase.auth.FirebaseAuthException: This request is missing a valid app identifier, meaning that neither SafetyNet checks nor reCAPTCHA checks succeeded."], the code was running well before but with a message of ["The sms code has expired. Please re-send the verification code to try again"], but after some edits it stops. The error is on the 75th line of the code "Toast appears with 'A'". Thank You...

MobileNumberFragment.kt

class MobileNumberFragment : Fragment() {
    private lateinit var binding: FragmentMobileNumberBinding
    //
    private lateinit var auth: FirebaseAuth
    private lateinit var fullNumber: String
    private lateinit var loadingDialogue: LoadingDialogue

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // 
        val view = inflater.inflate(R.layout.fragment_mobile_number, container, false)
        binding = FragmentMobileNumberBinding.bind(view)
        //Initialization
        val ccp = binding.mobileNumCountryPicker
        val editTxt = binding.mobileNumEdit
        val cntBtn = binding.mobileBtn
        val error = binding.mobileErrorTxt
        val help = binding.otpHelpTxt
        help.setOnClickListener {  }
        error.visibility = View.GONE
        editTxt.isFocusableInTouchMode = true
        editTxt.isFocusable = true
        editTxt.requestFocus()
        auth = FirebaseAuth.getInstance()
        cntBtn.setOnClickListener {
            if (editTxt.text.isNotEmpty()){
                if (ccp.isValidFullNumber){
                    fullNumber = ccp.formattedFullNumber
                    loadingDialogue = LoadingDialogue(requireActivity())
                    loadingDialogue.startLoadingDialog()
                    val options = PhoneAuthOptions.newBuilder(auth)
                        .setPhoneNumber(fullNumber)
                        .setTimeout(60L, TimeUnit.SECONDS)
                        .setActivity(requireActivity())   
                        .setCallbacks(callbacks)     
                        .build()
                    PhoneAuthProvider.verifyPhoneNumber(options)
                } else{
                    Toast.makeText(context, "Please enter a valid mobile number", Toast.LENGTH_SHORT).show()
                }
            } else {
                error.visibility = View.VISIBLE
            }
        }
        editTxt.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                if (error.isVisible){
                    error.visibility = View.GONE
                }
            }
            override fun afterTextChanged(s: Editable?) {
            }

        })
        ccp.registerCarrierNumberEditText(editTxt)
        ccp.isValidFullNumber
        ccp.setPhoneNumberValidityChangeListener {
            //Toast.makeText(context, "Please enter a valid mobile number", Toast.LENGTH_SHORT).show()
        }
        return view
    }
    private val callbacks = object : PhoneAuthProvider.OnVerificationStateChangedCallbacks() {

        override fun onVerificationCompleted(credential: PhoneAuthCredential) {
            Log.d(TAG, "onVerificationCompleted:$credential")
            signInWithPhoneAuthCredential(credential)
        }

        override fun onVerificationFailed(e: FirebaseException) {
            Log.w(TAG, "onVerificationFailed", e)
            Toast.makeText(context, "A", Toast.LENGTH_SHORT).show()

            if (e is FirebaseAuthInvalidCredentialsException) {
                Log.d("TAG", "onVerificationFailed: ${e.message.toString()}")
                Toast.makeText(context, "B", Toast.LENGTH_SHORT).show()
            } else if (e is FirebaseTooManyRequestsException) {
                Log.d("TAG", "onVerificationFailed: ${e.message.toString()}")
                Toast.makeText(context, "C", Toast.LENGTH_SHORT).show()
            }
        }

        override fun onCodeSent(
            verificationId: String,
            token: PhoneAuthProvider.ForceResendingToken
        ) {
            Log.d(TAG, "onCodeSent:$verificationId")
            findNavController().navigate(R.id.action_mobileNumberFragment2_to_phoneVerificationFragment2,
                Bundle().apply {
                    putString("fullNum", fullNumber)
                    putString("otp", verificationId)
                    putParcelable("token", token)
            })
        }
    }
    private fun signInWithPhoneAuthCredential(credential: PhoneAuthCredential) {
        auth.signInWithCredential(credential).addOnCompleteListener(requireActivity()) { task ->
                if (task.isSuccessful) {
                    loadingDialogue.dismissDialog()
                    Toast.makeText(context, "Successfully Authenticated", Toast.LENGTH_SHORT).show()
                    findNavController().navigate(R.id.action_mobileNumberFragment2_to_viewPagerFragment)
                    val user = task.result?.user
                } else {
                    Log.w(TAG, "signInWithPhoneAuthCredential: ${task.exception.toString()}")
                    if (task.exception is FirebaseAuthInvalidCredentialsException) {
                    }
                }
            }
    }
}

bild.gradle [Dependencies]

dependencies {

    implementation 'androidx.core:core-ktx:1.9.0'
    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'com.google.android.material:material:1.8.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    //Navigation
    implementation 'androidx.navigation:navigation-fragment-ktx:2.5.3'
    implementation 'androidx.navigation:navigation-ui-ktx:2.5.3'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'androidx.recyclerview:recyclerview-selection:1.1.0'
    implementation 'com.squareup.picasso:picasso:2.71828'
    implementation 'androidx.recyclerview:recyclerview:1.2.1'
    implementation 'de.hdodenhof:circleimageview:3.1.0'
    implementation 'com.google.code.gson:gson:2.9.0'
    implementation 'androidx.multidex:multidex:2.0.1'
    implementation 'androidx.core:core-ktx:1.9.0'
    implementation 'androidx.core:core-ktx:1.9.0'
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1'
    implementation 'androidx.test.ext:junit-ktx:1.1.5'
    implementation 'junit:junit:4.13.2'
    implementation 'androidx.core:core-ktx:1.9.0'
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'

    //Addition
    implementation 'com.mikhaellopez:circularprogressbar:3.1.0'
    implementation "xyz.schwaab:avvylib:1.2.0"
    implementation 'com.hbb20:ccp:2.7.0'
    implementation 'com.github.togisoft:jetpack_compose_country_code_picker:1.1.4'
    implementation 'com.github.TuleSimon:xMaterialccp:1.11'
    implementation 'com.github.vardemin.MaterialCountryPicker:materialcountrypicker:0.2.3'
    implementation 'com.fredporciuncula:phonemoji:1.4.0'
    implementation 'com.github.Ferfalk:SimpleSearchView:0.2.0'
    implementation 'com.airbnb.android:lottie:5.2.0'
    implementation 'com.github.zcweng:switch-button:0.0.3@aar'
    implementation 'nl.bryanderidder:themed-toggle-button-group:1.4.1'
    implementation 'com.github.alirezat775:carousel-view:1.1.1' //Carousel View
    implementation 'com.github.iammert:ReadableBottomBar:0.2' //Readable Bottom Bar
    implementation 'com.github.ismaeldivita:chip-navigation-bar:1.4.0'
    implementation 'com.github.ibrahimsn98:SmoothBottomBar:1.7.9'
    implementation 'nl.joery.animatedbottombar:library:1.1.0' //My Animated Bottom Bar, Thanks
    implementation 'com.github.GoodieBag:Pinview:v1.5'
    implementation 'androidx.activity:activity-compose:1.6.1'
    implementation 'io.github.chaosleung:pinview:1.4.4'
    implementation 'com.github.mukeshsolanki:android-otpview-pinview:2.1.2'
    implementation platform('com.google.firebase:firebase-bom:31.2.0')
    implementation 'com.google.firebase:firebase-analytics'
    implementation 'com.google.firebase:firebase-auth'
    implementation 'com.google.firebase:firebase-database'
    implementation 'com.google.firebase:firebase-storage'
    implementation 'com.google.firebase:firebase-messaging'
    implementation "com.github.pgreze:android-reactions:1.3"
    implementation 'com.github.3llomi:CircularStatusView:V1.0.3'
    implementation 'com.github.OMARIHAMZA:StoryView:1.0.2-alpha'
    implementation 'com.github.sharish:ShimmerRecyclerView:v1.3'

    //Coil
    implementation("io.coil-kt:coil:2.2.2")

    // Extensions = ViewModel + LiveData
    implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
    implementation "androidx.lifecycle:lifecycle-common-java8:2.5.1"
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1"

    kapt "com.android.databinding:compiler:3.1.4"

    //Kotlin components
    kapt "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.8.10"
    api "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4"
    api "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4"

    //glide
    def glide_version = "4.11.0"
    implementation "com.github.bumptech.glide:glide:$glide_version"
    annotationProcessor "com.github.bumptech.glide:compiler:$glide_version"

    //Retrofit
    implementation 'com.squareup.retrofit2:retrofit:2.6.2'
    //GSON
    implementation 'com.squareup.retrofit2:converter-gson:2.6.0'
    //Logging
    implementation 'com.squareup.okhttp3:okhttp:4.10.0'
    implementation 'com.squareup.okhttp3:logging-interceptor:4.10.0'

    // Extensions = ViewModel + LiveData
    implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
    implementation "androidx.lifecycle:lifecycle-common-java8:2.5.1"
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1"

    //
    implementation 'com.github.mohammadatif:Animatoo:master'
    //Camera Access
    /*implementation "androidx.camera:camera-camera2:1.2.1"
    implementation "androidx.camera:camera-lifecycle:1.2.1"
    implementation "androidx.camera:camera-view:1.2.1"
    implementation "androidx.camera:camera-video:1.2.1"*/

    //Firebase
    // Import the Firebase BoM
    implementation platform('com.google.firebase:firebase-bom:31.2.1')

    implementation 'com.google.firebase:firebase-analytics-ktx'
    implementation 'com.google.firebase:firebase-auth:21.1.0'
    //Play Integrity
    implementation "com.google.android.play:integrity:1.1.0"

    implementation 'com.google.firebase:firebase-database:20.1.0'
    implementation 'com.google.android.gms:play-services-safetynet:18.0.1'
    implementation 'androidx.browser:browser:1.5.0'

}

....................

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Mahmoud Nabil
  • 243
  • 2
  • 12
  • Does one of [these](https://stackoverflow.com/questions/37601942/firebase-3-we-have-blocked-all-requests-from-this-device-due-to-unusual-activi) answers help? – Alex Mamo Feb 16 '23 at 07:06
  • 2
    @AlexMamo I have already read and applied all these answers before. The test was running normally on an external device but it was sending me an expired OTP code. This question here was my last step after I tried to find a solution in more than one way. But can the block last for more than two days? Thanks.. – Mahmoud Nabil Feb 16 '23 at 17:18

0 Answers0