Validate Username and Email Crashed and Cannot Insert to Firebase Database

How to add email address to firebase kotlin?

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:

name@email.com -> 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)

Firebase can't use a Gmail account as child

You can't use special characters in your firebase node names, you rather put the user ID instead of the email:

child("Members").child("userX").setValue(people);

or better with the unique ID of the user logged in.

child("Members").child(FirebaseAuth.getInstance().getCurrentUser().getUid()).setValue(people);

How do I enable forbidden characters on google firebase?

Firebase keys in the Realtime Databse cannot contain those characters and there's no way to alter that behavior. From the docs

If you create your own keys, they must be UTF-8 encoded, can be a
maximum of 768 bytes, and cannot contain ., $, #, [, ], /, or ASCII
control characters 0-31 or 127. You cannot use ASCII control
characters in the values themselves, either.

Firestore also has some requirements from the docs

Must be valid UTF-8 characters
Must be no longer than 1,500 bytes
Cannot contain a forward slash (/)
Cannot solely consist of a single period (.) or double periods (..)
Cannot match the regular expression __.*__
(for documentIds) If you import Datastore entities into a Firestore database, numeric entity IDs are exposed as __id[0-9]+__

A lot of people when just getting started with Firebase have the tendency to want to store users by their email address

users
jay@example.com
name: "asdadsa"

but there's an inhenent issue with that strategy; you'll end up storing a reference to that user all over your database and you would then use the email as that reference

my_friends
jay@example.com: true

or

people_I_know
0: jay@example.com

etc.

But... Email addresses change if that happens you would have to not only delete the main users node (node keys cannot be changed) and then re-write it, you would also have to change every single occurrence of that email in your entire database!

The best practice is to disassociate node keys (documentsId's) from the data they contain

users
uid_0 //a users uid or a randomly generated firebase id
name: "Jay"
email: "jay@example.com"

then you would store the uid or generated id as the reference instead of the email which makes changing the email address a snap since it won't affect any other nodes

my_friends
uid_0: true

and

people_I_know
0: uid_0

So, to answer the question, users will sign up using their email address which will create their user account in Firebase with an associated uid. Store other user information by that users uid in the Realtime Database or Firestore instead of their email.

Sign In User or Admin

You can't use something as a intermediate node that has special symbols like '.'(dot)

From docs

If you create your own keys, they must be UTF-8 encoded, can be a maximum of 768 bytes, and cannot contain ., $, #, [, ], /, or ASCII control characters 0-31 or 127. You cannot use ASCII control characters in the values themselves, either.

You can simply use some unique id of user as key or if you really want to use email as key, replace all '.' with '_' underscore or something similar;

Live App is unable to write data into Firebase database

Do you have firebase crashlytics on the app? If so check on firebase console what the error could be.
If the app were not writing to the database only I would say it is probably the signing keys, but since it crashes, I would suggest you look at the pojo classes, if you use any. If you use it to update to firebase and you have minifyEnabled true, you have to add @Keep to the class so, the method firebase needs are not deleted.
But then again, without seeing the actual code, we can't really help much.



Related Topics



Leave a reply



Submit