I am making a Tinderr clone and i want to store the users in the realtime database of Firebase. I have followed the documentation and multiple youtube vids but nothing works. The users do get stored in the authentication so why not in the database?
SignupActivity :
package com.example.dogsharev2.activities
import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Toast
import com.example.dogsharev2.R
import com.example.dogsharev2.util.DATA_USERS
import com.example.dogsharev2.util.User
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.database.FirebaseDatabase
import kotlinx.android.synthetic.main.activity_signup.*
class SignupActivity : AppCompatActivity() {
private val firebaseDatabase = FirebaseDatabase.getInstance().reference
private val firebaseAuth = FirebaseAuth.getInstance()
private val firebaseAuthListener = FirebaseAuth.AuthStateListener {
val user = firebaseAuth.currentUser
if(user != null) {
startActivity(MainActivity.newIntent(this))
finish()
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_signup)
}
override fun onStart() {
super.onStart()
firebaseAuth.addAuthStateListener(firebaseAuthListener)
}
override fun onStop() {
super.onStop()
firebaseAuth.removeAuthStateListener(firebaseAuthListener)
}
fun onSignup(v: View) {
if(!emailET.text.toString().isNullOrEmpty() && !passwordET.text.toString().isNullOrEmpty()) {
firebaseAuth.createUserWithEmailAndPassword(emailET.text.toString(), passwordET.text.toString())
.addOnCompleteListener { task ->
if(!task.isSuccessful) {
Toast.makeText(this, "Signup error ${task.exception?.localizedMessage}", Toast.LENGTH_SHORT).show()
} else {
val email = emailET.text.toString()
val userId = firebaseAuth.currentUser?.uid ?: ""
val user = User(userId, "", "", email, "", "", "")
firebaseDatabase.child(DATA_USERS).child(userId).setValue(user)
}
}
}
}
companion object {
fun newIntent(context: Context?) = Intent(context, SignupActivity::class.java)
}
}
Data.kt
package com.example.dogsharev2.util
data class User(
val uid: String? = "",
val name: String? = "",
val age: String? = "",
val email: String? = "",
val gender: String? = "",
val preferredGender: String? = "",
val imageUrl: String? = ""
)
Constabts.kt
ackage com.example.dogsharev2.util
val DATA_USERS = "Users"
I have implemented 'com.google.firebase:firebase-database-ktx:20.0.5' also. And set the rules of my database to true.
Thanks in advance