Firebase and Swift username case-sensitive - ios

When registering a new user, and when I look in the Firebase user database, if the user exists he does not create the accunt, otherwise the registration is completed. But if there is a user named "Tony" in the database and I try to register with the username "tony", Firebase don't understand that "Tony" and "tony" are the same username. I want to solve this.
I state that I wrote the code in swift.

I think the fastest way with querying is to do this:
Duplicate your username list in a new list where all your usernames are lowercased
Lowercase the new username
Check if that username exist in your duplicated list (where all usernames are lowercased)
When it does not exists, append the real username without lowercase to the list with the usernames where they are case-sensitive and to the lowercased username list.
This duplicates data, but it is not that bad since it are only usernames.

Related

Firebase: Maintain a Username Directory

I am creating a Social app and want to track if a username already exists or not. The username list is supposed to grow in future and the way I was doing it now was a key value pair of <string,bolean> like this:
name1: true,
name2: true
all the above data was to be stored in a single document and whenever I want to see if a user exists I would call this document and check accordingly. But here's the problem, firebase max document size is 1MBs and as the users grow this can be problematic, so wanted to know from firebase experts that what's the best way to solve this use case in firestore or realtime database but since I need to query exists maybe realtime db won't suit that well.
Note that I don't want any of firestore querying capabilities but only to check if an entry exists in the record or not and if not just add it.
The Realtime Database doesn't have a 1MB limit (since it has no concept of a document, and everything is just a tree of JSON), so I'd typically use that for the index of user names.
Checking whether a name exists is pretty simple there too, and in JavaScript would look something like:
const usernames = firebase.database().ref('usernames');
usernames.child('name1').once((snapshot) => {
if (snapshot.exists()) {
...
}
});

Is my User ID supposed to be different than my Document ID for each User in my Users Collection (Firestore)?

I know that this is probably a dumb question, but I'm only starting to learn Firebase/Firestore and I'm having trouble finding the answer that I'm looking for.
I know that I can get the User ID from the currentUser method, but how am I supposed to reference the uid in my file path if the uid and the document id for each user is a different value?
For example, in the reference variable bellow, "HEo4YjfjJ0p2B6hVJRXs" is the document id. If I replace this value with the current user's uid, would it still work the same despite the fact that the uid value is different?
var colRef = Firestore.firestore().collection("/users/HEo4YjfjJ0p2B6hVJRXs/Days")
For example, in the reference variable bellow, "HEo4YjfjJ0p2B6hVJRXs" is the document id.
If that's the id of the document, than that id should be used in your reference, as you already do.
If I replace this value with the current user's uid, would it still work the same despite the fact that the uid value is different?
If the current user's uid is different than the id of the document, then you won't be able to read that document. If you need a document that has as id, the user's uid then you should create that document. If you are also thinking about wildcards, please also note that in Firestore there are no wildcards paths to documents. You have to identify collections and documents by their ids.

How can I use user input to name a child in Firebase?

I'm trying to create a simple user database on Firebase for iOS. I have two textfields, one that takes the user's email address, and the other that takes the user's password. I want the email to be the user's uid. I try to accomplish this by creating a variable called "email" which I set equal to emailTextField.text! Then I implement the code that generates child(s) in Firebase, like so:
ref = Database.database().reference()
ref.child("users").child(email).setValue(true)
Here's my problem: creating a child and naming it using a variable, doesn't work. I can do the following just fine:
ref.child("users").child("email")
but once I take away the quotation marks around "email" so that it becomes the variable, the program crashes.
How can I use user input to name a child in Firebase?
Firebase node / document names do not support the same character set which email addresses support, for example, the . symbol in an email address would make the child name invalid.
If you tried setting a child as
ref.child("users").child("xyz#xyz.xyz")
you should see the same error.
If you absolutely need to use the email address as the node name, I recommend encoding the email in a way which is compatible with the firebase node name rules.
Link to the rules
Edit: If you are using firebase auth, the normal pattern is too use the UID returned by the authenticated user object as the node name, not the email address entered from the textfield.
A quick example:
Auth.auth().signIn(withEmail: email, password: password) { (user, error) in
if let user = user {
ref.child("users").child(user.uid).setValue(true)
}
}

Firebase: Swift - Query value in child node without knowing the parent key

I have a firebase database which I have imported a list of approx. 8200 Universities with the details seen above.
I want to be able to enter an email address into a text field then query the "domain" part of this JSON. My issue is I need to query all the domains in the list of 8000, however I do not know how to search domain ad I do not have the "7542" to use a childByAppendingPath.
So I have a reference to "universities" but I need to be able to query "domain" without knowing the parent key (i.e. 7542 in the example above, but I want to search the domain, "iautb.ac.ir").
You need to create a reference and Query that reference, check this link to find more about how to retrieve data from firebase.
Basically you need to do something like this
let myRef = Firebase(url: "https://your.firebaseio.com/universities")
let query = myRef.queryOrderedByChild("domain").queryEqualToValue("yourDomainSearchValue")
and then observe the events you need to on your query

Parse case insensitive login

I am using Parse in my Xcode application and when I try to login with:
username: Admin
password: test
it works. But when I enter
username: admin
password: test
the login parameters are invalid. Is there a way to make Parse not case sensitive?
Basically what the link in the comment says is that when you create a new user object or before you let a user login call:
let username = usernameLabel.text
..........................
username = username.lowercaseString
..........................
//Then save the object
So it's not a way to make Parse case-insensitive but it eliminates the need to make it case-insensitive since everything is lowercased.
Some parameters Parse uses are case sensitive so it's up to you to implement the proper methods to rectify this before a user saves any User column properties. After the fact will be too late. So in short, let the user type whatever username they want and save it to the backend as a lowercase string. Then when they re-enter it, translate the user input string into another lowercase string and validate it against the backend (which is now a lower case representation)

Resources