Issue
I trying to practice on tutorial that uses Firebase Authentication, Realtime Database, and Storage, I have come a very long way in this exercise until this problem, I successfully stores the emails and passwords on authentication and it shows on firebase console but the problem is with database, it's supposed to store the following HashMap on it, but nothing showing up on firebase Realtime console
The rules
{
"rules": {
".read": true,
".write": true
}
}
Login Class
class LoginFragment : Fragment(R.layout.fragment_login) {
private var _binding: FragmentLoginBinding? = null
private var firebaseUser: FirebaseUser? = null
private lateinit var mAuth: FirebaseAuth
// This property is only valid between onCreateView and
// onDestroyView.
private val binding get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentLoginBinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
mAuth = FirebaseAuth.getInstance()
firebaseUser = FirebaseAuth.getInstance().currentUser
if(firebaseUser != null){
val action = LoginFragmentDirections.actionLoginFragmentToMainFragment()
findNavController().navigate(action)
}
binding.apply {
btnlogin.setOnClickListener {
loginUser()
}
textViewSignUp.setOnClickListener {
val action = LoginFragmentDirections.actionLoginFragment2ToRegisterFragment2()
findNavController().navigate(action)
}
}
}
private fun loginUser() {
val email: String = binding.inputEmail.text.toString()
val password: String = binding.inputPassword.text.toString()
if (TextUtils.isEmpty(email) || TextUtils.isEmpty(password)
) {
binding.apply {
inputEmail.error = "Email cannot be empty"
inputPassword.error = "Password cannot be empty"
}
}else {
mAuth.signInWithEmailAndPassword(email,password).addOnCompleteListener{ task->
if(task.isSuccessful){
val action = LoginFragmentDirections.actionLoginFragmentToMainFragment()
findNavController().navigate(action)
}else {
Toast.makeText(requireContext(), "Error ${task.exception?.message.toString()}", Toast.LENGTH_SHORT).show()
}
}
}
}
}
Registration Class
class RegisterFragment : Fragment(R.layout.fragment_register) {
private var _binding: FragmentRegisterBinding? = null
private lateinit var mAuth: FirebaseAuth
private lateinit var refUsers: DatabaseReference
// This property is only valid between onCreateView and
// onDestroyView.
private val binding get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentRegisterBinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
mAuth = FirebaseAuth.getInstance()
binding.apply {
btnRegister.setOnClickListener {
registerUser()
}
alreadyHaveAccount.setOnClickListener {
findNavController().navigate(RegisterFragmentDirections.actionRegisterFragment2ToLoginFragment2())
}
}
}
private fun registerUser() {
val userName: String = binding.inputUsername.text.toString()
val email: String = binding.inputEmail.text.toString()
val password: String = binding.inputPassword.text.toString()
val confirmedPassword: String = binding.inputConformPassword.text.toString()
if (TextUtils.isEmpty(userName) || TextUtils.isEmpty(email)
|| TextUtils.isEmpty(password) || TextUtils.isEmpty(confirmedPassword)
) {
binding.apply {
inputUsername.error = "Username cannot be empty"
inputEmail.error = "Email cannot be empty"
inputPassword.error = "Password cannot be empty"
inputConformPassword.error = "Confirmed password cannot be empty"
}
} else if (!password.equals(confirmedPassword, ignoreCase = false)) {
Snackbar.make(
requireView(),
"The Password and confirmation do not match", Snackbar.LENGTH_LONG
).show()
} else {
mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener { task ->
if (task.isSuccessful) {
val action =
RegisterFragmentDirections.actionRegisterFragmentToMainFragment()
val firebaseUserID = mAuth.currentUser!!.uid
refUsers = FirebaseDatabase.getInstance().reference
.child(firebaseUserID)
val userHashMap = HashMap<String, Any>()
userHashMap["uid"] = firebaseUserID
userHashMap["username"] = userName
userHashMap["profile"] =
"https://firebasestorage.googleapis.com/v0/b/mig33-94625.appspot.com/o/icons8-test-account-100.png?alt=media&token=ce231b05-d4a7-49cb-8003-027e0d5c76e1"
userHashMap["cover"] =
"https://firebasestorage.googleapis.com/v0/b/mig33-94625.appspot.com/o/cover.jpg?alt=media&token=6bbcf0fb-77d1-4870-b714-aa13eedff86c"
userHashMap["status"] = "offline"
userHashMap["search"] = userName.lowercase()
userHashMap["facebook"] = "https://m.facebook.com"
userHashMap["instagram"] = "https://m.instagram.com"
userHashMap["website"] = "https://www.google.com"
refUsers.updateChildren(userHashMap).addOnCompleteListener { task ->
if (task.isSuccessful) {
Log.d("mido",refUsers.get().result.toString())
findNavController().navigate(action)
}
}
//
} else {
Toast.makeText(
requireContext(),
"Error ${task.exception?.message.toString()}",
Toast.LENGTH_SHORT
).show()
}
}
}
}
}
build.gradle Dependencies
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:1.5.20"
implementation 'androidx.core:core-ktx:1.6.0'
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'androidx.navigation:navigation-fragment-ktx:2.3.5'
implementation 'androidx.navigation:navigation-ui-ktx:2.3.5'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation platform('com.google.firebase:firebase-bom:28.2.0')
implementation 'com.google.firebase:firebase-analytics-ktx'
implementation 'com.google.firebase:firebase-core:19.0.0'
implementation 'com.google.firebase:firebase-auth:21.0.1'
implementation 'com.google.firebase:firebase-storage-ktx:20.0.0'
implementation 'com.google.firebase:firebase-messaging:22.0.0'
implementation platform('com.google.firebase:firebase-bom:28.2.0')
// Declare the dependency for the Realtime Database library
// When using the BoM, you don't specify versions in Firebase library dependencies
implementation 'com.google.firebase:firebase-database-ktx'
implementation 'de.hdodenhof:circleimageview:2.2.0'
implementation 'com.squareup.picasso:picasso:2.71828'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'com.rengwuxian.materialedittext:library:2.1.4'
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation 'com.intuit.ssp:ssp-android:1.0.6'
implementation 'com.intuit.sdp:sdp-android:1.0.6'
implementation 'com.android.support:multidex:1.0.3'
}
Solution
My first guess is that you may have downloaded the google-services.json
file before you created the Realtime Database in the console, which means that the file doesn't contain the correct configuration string.
If that is the case, you'll need to:
- either download an updated configuration file and add that to your Android app,
- or you can specify the URL for the database in the code here:
FirebaseDatabase.getInstance("URL to database here").reference
If this is indeed the cause of the problem, we're working on surfacing this configuration problem more explicitly (it's currently hidden in a debug-level log message from the SDK, which isn't logged by default).
Answered By - Frank van Puffelen
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.