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]
}
}