Home > front end >  How to check if user email is verified in firebase, even though I don't have a literal User Obj
How to check if user email is verified in firebase, even though I don't have a literal User Obj

Time:06-19

private fun login(email: String, password: String) {
    val checkingDialog = Dialog(requireActivity())
    checkingDialog.setContentView(R.layout.checking_progressbar)
    checkingDialog.setCanceledOnTouchOutside(false)

    CoroutineScope(Dispatchers.IO).launch {
        withContext(Dispatchers.Main) {
            // Show Checking Dialog
            checkingDialog.show()
        }
        FirebaseAuth.getInstance().currentUser?.reload()
        Log.e("a7a", "login clicked")

        if (checkForEmptyInputs()) {
            Log.e("a7a", "input checks login fragment tmam")
            try {
                // TODO lazm a8ayr kosom elsatr da 3l4an a5ls mn ksom el7war da
                if (FirebaseAuth.getInstance().currentUser?.isEmailVerified == true) {
                    FirebaseAuth.getInstance().signInWithEmailAndPassword(email, password)
                        .addOnCompleteListener {
                            checkingDialog.dismiss()
                            Log.e("a7a", "complete")
                            if (it.isSuccessful) {
                                Log.e("a7a", "sign in fol")
                                val intent = Intent(requireActivity(), MainActivity::class.java)
                                intent.flags =
                                    Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
                                CoroutineScope(Dispatchers.IO).launch {
                                    val userDocumentId =
                                        getUserIdByEmail(binding?.etEmailSignIn?.text.toString())
                                    intent.putExtra("userDocumentId", userDocumentId)
                                    intent.putExtra("userEmail",
                                        binding?.etEmailSignIn?.text.toString())
                                    startActivity(intent)
                                    requireActivity().finish()
                                }
                            } else {
                                Log.e("a7a", it.exception?.message.toString())
                                lifecycleScope.launch {
                                    withContext(Dispatchers.Main) {
                                        binding?.tvSignInErrors?.visibility = View.VISIBLE
                                        var errorMessage = it.exception?.message.toString()
                                        if (errorMessage == "The password is invalid or the user does not have a password.") {
                                            errorMessage = "Invalid Password"
                                        }
                                        binding?.tvSignInErrors?.text = errorMessage
                                    }
                                }

                            }
                        }
                } else {
                    Log.e("a7a", "Please Verify your Account")
                    withContext(Dispatchers.Main) {
                        checkingDialog.dismiss()
                        binding?.tvSignInErrors?.visibility = View.VISIBLE
                        binding?.tvSignInErrors?.text = getString(R.string.verifyAccount)
                    }
                    FirebaseAuth.getInstance().currentUser?.sendEmailVerification()
                }
            } catch (e: NullPointerException) {
                withContext(Dispatchers.Main) {
                    binding?.tvSignInErrors?.visibility = View.VISIBLE
                    binding?.tvSignInErrors?.text = getString(R.string.sample_error)
                }
            } catch (e: Exception) {
                withContext(Dispatchers.Main) {
                    binding?.tvSignInErrors?.visibility = View.VISIBLE
                    binding?.tvSignInErrors?.text = e.toString()
                }
            }
        }
    }
}

That's the function I am using to sign into firebase This works fine when I sign in with the device that I created the account from, however, when I try to sign in from another device, it sais that the account isn't verified, and doens't pass the if statement of the verification. So the thing is, I understand that the userobject here isn't available for the new device, so How can I check if a specific email is verified, not the current user is verified because that's empty in that case?

CodePudding user response:

Simply Move the if block you're using to check verification, in the onsuccess listener block

  • Related