Home > OS >  Firebase Authentication failing to create a user in Kotlin
Firebase Authentication failing to create a user in Kotlin

Time:10-06

need some help here, I am doing an android chat application using kotlin, the chat consists of user sign up and login, it consists of 2 classes, Signup.kt & Login.kt, the problem here is after signing up, I cant get the user data uploaded (email & password) in the firebase and it shows me with the "Toast" of "some error occurred", I have tried to debug but still it shows the error.

-> Signup.kt

    class SignUp : AppCompatActivity() {
    private lateinit var mAuth: FirebaseAuth

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_sign_up)
        supportActionBar?.hide()

        mAuth = FirebaseAuth.getInstance()

        btnSignup.setOnClickListener{
            val email = edt_email.text.toString()
            val password = edt_pwd.text.toString()

            signup(email, password)
        }
    }

    private fun signup(email: String, password: String){
        mAuth.createUserWithEmailAndPassword(email, password)
            .addOnCompleteListener(this) { task ->
                if (task.isSuccessful) {
                    val intent = Intent(this@SignUp, MainActivity::class.java)
                    startActivity(intent)
                } else {
                    Toast.makeText(this@SignUp, "Some error occured", Toast.LENGTH_SHORT).show()
                }
            }
    }
}

-> activity_sign_up.xml

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SignUp">

    <ImageView
        android:id="@ id/app_logo"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:src="@drawable/whatsapp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="70dp"/>

    <EditText
        android:id="@ id/edt_name"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_below="@ id/app_logo"
        android:layout_marginTop="10dp"
        android:background="@drawable/edt_background"
        android:hint="Name"
        android:paddingLeft="13dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"/>

    <EditText
        android:id="@ id/edt_email"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_below="@ id/edt_name"
        android:layout_marginTop="10dp"
        android:paddingLeft="13dp"
        android:hint="Email"
        android:background="@drawable/edt_background"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"/>

    <EditText
        android:id="@ id/edt_pwd"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_below="@ id/edt_email"
        android:layout_marginTop="10dp"
        android:background="@drawable/edt_background"
        android:hint="Password"
        android:paddingLeft="13dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"/>

    <Button
        android:id="@ id/btnSignup"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_below="@ id/edt_pwd"
        android:text="Sign Up"
        android:background="@drawable/btn_background"
        android:textColor="@color/white"
        android:textSize="20sp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"/>

</RelativeLayout>

CodePudding user response:

When a task fails it contains an exception that gives details about the cause of that failure. You should always log that exception to find out what went wrong.

mAuth.createUserWithEmailAndPassword(email, password)
    .addOnCompleteListener(this) { task ->
        if (task.isSuccessful) {
            val intent = Intent(this@SignUp, MainActivity::class.java)
            startActivity(intent)
        } else {
            Toast.makeText(this@SignUp, "Some error occured", Toast.LENGTH_SHORT).show()
            Log.e("AUTH", "Error signing in", task.exception);
        }
    }

Also see the example in the Firebase documentation on creating an email password account, which does precisely this.

  • Related