0

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

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Is any other read from or write to the database working? If not, I suspect you may not have the necessary database URL in your `google-services.json` file. If that is indeed the problem, see the last code snippet here: https://stackoverflow.com/a/65566484/209103 and this answer: https://stackoverflow.com/a/68807113/209103 – Frank van Puffelen May 18 '22 at 21:44

1 Answers1

1

I have analyzed it multiple times and everything seems perfectly fine. What you can do is to add a listener to setValue(user) method like this:

firebaseDatabase.child(DATA_USERS).child(userId).setValue(user)
     .addOnCompleteListener { task ->
            if(task.isSuccessful) {
                 Toast.makeText(this, "Successful", Toast.LENGTH_SHORT).show()
                 } else {
                 Toast.makeText(this, task.getException().getMessage(),
                         Toast.LENGTH_SHORT).show();
                }
      }

Now task.getException().getMessage() will let you know what the actual problem is about saving your user data.

Abu bakar
  • 751
  • 1
  • 4
  • 16