0

I am trying to add a new user to my real-time database but getting "Failed Login" every time. I don't know where's the problem. "Loginfirebase" is my function for adding new users to the database, if a user is added successfully then I am calling "loadmain" function from where I am changing to other activity.

package com.example.tictactoe

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.database.FirebaseDatabase
import kotlinx.android.synthetic.main.activity_room.*


class RoomActivity : AppCompatActivity() {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_room)

        login.setOnClickListener {
            buLoginEvent()
        }
    }

    fun buLoginEvent(){
        val emailString = email.text.toString()
        val passwordString = password.text.toString()
        if(emailString.isNotEmpty() && passwordString.isNotEmpty()) {
            LoginToFirebase(emailString, passwordString);
        } else {
            //show error message to user
            Toast.makeText(this, "Email or Password cannot be empty!!", Toast.LENGTH_LONG).show()
        }

    }

    fun LoginToFirebase(email:String, password:String){

        var mAuth: FirebaseAuth = FirebaseAuth.getInstance()
        var database = FirebaseDatabase.getInstance()
        var myRef = database?.reference
        var currentUser = mAuth!!.currentUser
        mAuth!!.createUserWithEmailAndPassword(email,password)
            .addOnCompleteListener(this){
                task->
                if(task.isSuccessful){
                    Toast.makeText(applicationContext,"Successful Login", Toast.LENGTH_LONG).show()
                    //saving in database
                    myRef.child("Users").child(splitString(currentUser!!.email.toString())).setValue(
                        currentUser!!.uid
                    )//creating current node iin realtime database
                    LoadMain()
                }
                else{
                    Toast.makeText(applicationContext,"Failed Login", Toast.LENGTH_LONG).show()
                }
            }

    }

    override fun onStart() {//2nd time when the application is started then call this method
        super.onStart()
        LoadMain()
    }
    fun  LoadMain(){
        var mAuth: FirebaseAuth = FirebaseAuth.getInstance()
        var currentUser = mAuth!!.currentUser

        if(currentUser!=null) {// doing this only when the user is not null


            var intent = Intent(this, OnlineGameActivity::class.java)
            intent.putExtra("email", currentUser!!.email)
            intent.putExtra("uid", currentUser!!.uid)

            startActivity(intent)
            finish()
        }
        else{

        }
    }
    fun splitString(str:String):String{
        var split=str.split("@")
        return split[0]
    }
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • I think that this article, [How to create an Android app using multiple Firebase products in Kotlin?](https://medium.com/firebase-tips-tricks/how-to-create-an-android-app-using-multiple-firebase-products-in-kotlin-16aade81ffec) might help you understand the concept better. – Alex Mamo Jan 22 '22 at 09:53

1 Answers1

0

If a Task fails it contains an exception that tells you the cause of the failure, so you might want to write that to the logcat to find the root cause of your problem with:

mAuth!!.createUserWithEmailAndPassword(email,password)
    .addOnCompleteListener(this){
        task->
        if(task.isSuccessful){
            Toast.makeText(applicationContext,"Successful Login", Toast.LENGTH_LONG).show()
            //saving in database
            myRef.child("Users").child(splitString(currentUser!!.email.toString())).setValue(
                currentUser!!.uid
            )//creating current node iin realtime database
            LoadMain()
        }
        else{
            Toast.makeText(applicationContext,"Failed Login", Toast.LENGTH_LONG).show()
            Log.e("FirebaseAuth", "Failed login", task.getException()); // 
        }
    }
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807