-1

I hope you are doing great.

I have the following code, that I am using to create an account using firebase. I have used the statements to validate the credentials. I don´t know what to right more to validade the post of this question, so sorry for having to read this.

class SignUp : AppCompatActivity() {

    private lateinit var binding: ActivitySignUpBinding

    private lateinit var actionBar: ActionBar

    private lateinit var progressDialog: ProgressDialog

    private lateinit var firebaseAuth: FirebaseAuth

    private var email=""
    private var password=""
    private var passwordrepeat=""

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivitySignUpBinding.inflate(layoutInflater)
        setContentView(binding.root)

        actionBar = supportActionBar!!
        actionBar.title="Sign Up"
        actionBar.setDisplayHomeAsUpEnabled(true)
        actionBar.setDisplayShowHomeEnabled(true)

        progressDialog = ProgressDialog(this)
        progressDialog.setTitle("Please Wait")
        progressDialog.setMessage("Creating account")
        progressDialog.setCanceledOnTouchOutside(false)

        firebaseAuth = FirebaseAuth.getInstance()

        binding.buttonSignUp.setOnClickListener{
            validateData()
        }

    }
    val EMAIL_ADDRESS_PATTERN = Pattern.compile(
        "[a-zA-Z0-9\\+\\.\\_\\%\\-\\+]{1,256}" +
                "\\@" +
                "test" +
                "(" +
                "\\." +
                "com" +
                ")+"
    )

    fun isValidString(str: String): Boolean{
        return EMAIL_ADDRESS_PATTERN.matcher(str).matches()
    }
    private fun validateData() {
        email = binding.emailText.text.toString().trim()
        password = binding.passwordText.text.toString().trim()
        passwordrepeat = binding.passwordText2.text.toString().trim()

          if (!isValidString(email)) {
            binding.emailTF.error = "Use ipvc email"

       // } else if(){

        }else if (TextUtils.isEmpty(password)) {
            binding.passwordTF.error = "Please enter password"
        }else if(TextUtils.isEmpty(passwordrepeat)){
            binding.passwordTF2.error="Please repeat password"
        }else if(password != passwordrepeat) {
            binding.passwordTF2.error="Passwords don´t match"
        }else if (password.length < 6){
            binding.passwordTF.error = "Password must have atleast 6 caracters"
        }else{
            firebaseSignUp()
        }
    }

    private fun firebaseSignUp() {
        progressDialog.show()

        firebaseAuth.createUserWithEmailAndPassword(email, password)
            .addOnSuccessListener {
                val firebaseUser = firebaseAuth.currentUser
                val email = firebaseUser!!.email
                Toast.makeText(this, "Account have been created with email $email", Toast.LENGTH_LONG)

                startActivity(Intent(this, Perfil::class.java))
                finish()
            }
            .addOnFailureListener{e->
                progressDialog.dismiss()
                Toast.makeText(this, "Sign Up Failded due to ${e.message}", Toast.LENGTH_LONG)
            }
    }

    override fun onSupportNavigateUp(): Boolean {
        onBackPressed() //Voltar a atrás quando o botão é pressionado
        return super.onSupportNavigateUp()
    }

How can I add a verification thats verifies if the account has already been created while creating it? The username in this case is the Email.

1 Answer 1

1

Your failure listener will be invoked with a FirebaseAuthUserCollisionException indicating the user's email is already taken.

You can read more about this and the other failure cases in the documentation.

Sign up to request clarification or add additional context in comments.

3 Comments

I understood that. It does not create a second account The problem is that does not make the Toast saying the email already exists , it just reloads the page. I am forgetting something?
You are not calling show() on your toast.
Lol, nevermind...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.