edited. it got error in dataSnapshot.child(decodeUserEmail).child("passwordRegister").getValue(String::class.java) line
fun decodeUserEmail(email: String) = email.replace(",", ".")
private fun isUser() {
val userEnteredEmail = email.getText().toString().trim()
val userEnteredPassword = password1.getText().toString().trim()
val decodeUserEmail = decodeUserEmail(userEnteredEmail)
val reference = FirebaseDatabase.getInstance().getReference("User")
val checkUser: Query = reference.orderByChild("emailAddress").equalTo(decodeUserEmail)
checkUser.addListenerForSingleValueEvent(object : ValueEventListener {
override fun onDataChange(@NonNull dataSnapshot: DataSnapshot) {
if (dataSnapshot.exists()) {
val passwordFromDB =
dataSnapshot.child(decodeUserEmail).child("passwordRegister").getValue(String::class.java)
if (passwordFromDB.equals(userEnteredPassword)) {
startActivity(Intent(this@MainActivity, Home::class.java))
val intent = Intent(baseContext, Home::class.java)
intent.putExtra("EXTRA_SESSION_ID", decodeUserEmail)
startActivity(intent)
} else {
}
} else{
}
}
override fun onCancelled(@NonNull databaseError: DatabaseError) {}
})
CodePudding user response:
As your error sais:
Firebase Database paths must not contain '.', '#', '$', '[', or ']'`
This means that Firebase does not allow you to use those symbols in the key names. So instead of using the email address as a key for the user's node, I recommend you to use the UID:
val user = FirebaseAuth.getInstance().currentUser
currentUser?.apply {
myRef.child(uid).setValue(User)
}
It's always better such an approach because the UID it's always the same. The email address can be changed by the user.
If you don't want to use this approach, then you might consider encoding the email address like this:
[email protected] -> name@email,com
To achieve this, I recommend you using the following methods:
fun encodeUserEmail(userEmail: String) = userEmail.replace(".", ",")
fun decodeUserEmail(userEmail: String) = userEmail.replace(",", ".")
Edit:
val encodedEmail = encodeUserEmail(EmailAddress)
myRef.child(encodedEmail).setValue(User)