I want to combine multiple Firebase Auth uids in a single string. So I need to know which characters can't possibly be in a uid. That way I can use one of them as a delimiter.
I've looked throughout Firebase's documentation and can't seem to find the answer
According to the documentation any string between 1 and 128 characters is allowed.
Source: https://firebase.google.com/docs/auth/admin/manage-users (see: Table 1. Properties supported by the create user operation)
Take care: While the uid can be any string, the realtime database can't use any string. So if you want to use the uid to create a document you must obey those rules Paths must be non-empty strings and can't contain ".", "#", "$", "[", or "]"
The default randomly-generated Firebase Authentication uids are 28-character alphanumeric strings with no special characters.
Further research brought me to an answer from Firebase User ID Allowed Characters which suggests a pattern of:
^[0-9a-zA-Z]{27}[0-9]$
This matches the uids I have across a couple of Firebase projects, so seems to be correct.
From my experience, the latest version of Firebase follows the ^[0-9a-zA-Z]{27}[0-9]$ pattern.
I don't know why there's always a digit at the end, but that seems to be consistent.
Related
I am currently creating an App with a firebase backend. When creating a new User it gets automatically assigned to an Id in an Format that looks like this: v4xpr8hLrLR3W5VUTN2zZ3XXKrF3.
Has this format a name? Like the 00000000-0000-0000-0000-000000000000 is called a Guid?
A UUID/GUID (RFC 4122) is a 128-bit number written as 32 hexadecimal characters.
A Firebase RTDB Push ID is 20 Base64 characters (a 120-bit number) and a Firebase user ID is (currently) 28 Base62 characters (a 166-bit number). This particular ID format doesn't appear to have an official name at the moment, as they are simply referred to as Push IDs and User UIDs.
However, inspired by the Firebase Push IDs, there is a new term that could be applied here: ULID - a Universally Unique Lexicographic Sortable IDentifier. The Firebase implementation of these IDs is not compatible with the proposed ULID spec (as the spec is Base32 and designed to be UUID compatible) but is similar enough that the name could be used here.
Assuming I have a list of data I would like to store with Firebase realtime database, and search it later.
What would be the best way to store the data and query it to get the best performance?
My data is a list of names (containing a lot of names).
["Bob", "Rob", ...]
Note that I have multiple clients searching in a given time.
If the names are supposed to be unique and order doesn't matter, you'll want to store them as a mathematical set. In the Firebase Realtime Database you'll model this as:
"usernames": {
"Bob": true,
"Rob": true
}
A few things of note about this model:
We use the names as keys, which means that each name is by definition unique (since each key can exist only once in its containing node).
The true values have no specific meaning. They are just needed, since Firebase can't store a key without a value.
Certain characters (such as . and /) cannot be used in keys. If a name contains such characters, you will have to filter them out (or encode them) in the key. For example someone named Jes.sie will have to be stored as Jes.sie (lossy) or e.g. Jes%2Esie (with URL encoding).
In such cases you could store the original unfiltered/unencoded name as the value. So: "Jes%2Esie": "Jes.sie".
A few more general notes about (text) searching in the Firebase Realtime Database:
Firebase can only do prefix matches, it has no support for searching strings that contain or end with a certain substrings. This means that in the original data it can search for everything starting with an B (with orderByKey().startAt("R").endAt("R\uF7FF")), but it can't search for everything ending with ob.
Searches are case-sensitive. If you want to be able to search case-insensitive, consider storing the keys as all-lowercase:
"usernames": {
"bob": "Bob",
"rob": "Rob",
"jes%2esie": "Jes.sie"
}
If you need better support for text-search, consider integrating a third-party search engine. Common recommendations are Elastic-search (self-hosted) or Algolia (cloud-based).
For more information on many of these topics, see:
this article on NoSQL data modeling
the video series Firebase for SQL developers
Cloud Firestore Case Insensitive Sorting Using Query (while written for Firestore, the same applies here)
Assuming I have a list of data I would like to store with Firebase realtime database, and search it later.
What would be the best way to store the data and query it to get the best performance?
My data is a list of names (containing a lot of names).
["Bob", "Rob", ...]
Note that I have multiple clients searching in a given time.
If the names are supposed to be unique and order doesn't matter, you'll want to store them as a mathematical set. In the Firebase Realtime Database you'll model this as:
"usernames": {
"Bob": true,
"Rob": true
}
A few things of note about this model:
We use the names as keys, which means that each name is by definition unique (since each key can exist only once in its containing node).
The true values have no specific meaning. They are just needed, since Firebase can't store a key without a value.
Certain characters (such as . and /) cannot be used in keys. If a name contains such characters, you will have to filter them out (or encode them) in the key. For example someone named Jes.sie will have to be stored as Jes.sie (lossy) or e.g. Jes%2Esie (with URL encoding).
In such cases you could store the original unfiltered/unencoded name as the value. So: "Jes%2Esie": "Jes.sie".
A few more general notes about (text) searching in the Firebase Realtime Database:
Firebase can only do prefix matches, it has no support for searching strings that contain or end with a certain substrings. This means that in the original data it can search for everything starting with an B (with orderByKey().startAt("R").endAt("R\uF7FF")), but it can't search for everything ending with ob.
Searches are case-sensitive. If you want to be able to search case-insensitive, consider storing the keys as all-lowercase:
"usernames": {
"bob": "Bob",
"rob": "Rob",
"jes%2esie": "Jes.sie"
}
If you need better support for text-search, consider integrating a third-party search engine. Common recommendations are Elastic-search (self-hosted) or Algolia (cloud-based).
For more information on many of these topics, see:
this article on NoSQL data modeling
the video series Firebase for SQL developers
Cloud Firestore Case Insensitive Sorting Using Query (while written for Firestore, the same applies here)
Following is one of the examples in Cloud Firestore list query
citiesRef.where("state", ">=", "CA").where("state", "<=", "IN")
What does it mean?
Is this the way I can do a query for searching substrings. For example,
I have a collection of users and all my user documents have first_name and last_name. Now as a user I am trying to search others by typing a portion of the name and searching. I should be able to write a query to get all user who's name contains that string. How do I do that in Firestore?
[Googler here] To answer your first question, state <= "IN" is a query for all documents where the value of the state property sorts less than or equal to the value "IN". So if the state was "IM" that would be true but not if it was "IO" or "IP" as those come later lexicographically.
The use of "IN" may have been confusing here, it's meant to be the abbreviation for the state of Indiana but it's also a reserved word in many other database systems.
To answer the second question, Cloud Firestore does not support any native "contains", operations or other common string queries like "beginsWith", "endsWith", "like", etc. The reason for this is that all Cloud Firestore queries must hit an index and right now we don't index textual fields in a way that would make those queries fast.
Currently we recommend using a third-party search provider like Algolia, and we provide some guidance on doing so here:
https://firebase.google.com/docs/firestore/solutions/search
For an app I build, I want to use the ID generated by Firebase push as an email address local part. Since the dash (-) is not allowed as first character, I would like to replace it with another character.
This has to be reversible though. Therefore I want to know, which characters does the Firebase push ID consist of?
So far I have seen:
alpha (a-z and A-Z and 0-9)
underscore (_)
dash (-)
Sample: -KD3rcGMuucRDjKOTK3O
Are there any other characters which might be contained in the ID?
Do firebase IDs always start with a dash?
There are probably a lot of better ways to generate a unique email address than by using Firebase's push ids and then mangling them. That said, if you want to learn more about how Firebase generates its push ids, read this blog post: The 2^120 Ways to Ensure Unique Identifiers. It also explains why you should not rely on push ids to be unguessable/secure.
An important thing to realize from that post is that the first 8 characters of a push id contain an encoded timestamp, which is also the reason they always start with the same characters if you generate them close to each other.
The post also contains a link to a gist of the JavaScript code to generate a push id.
The set of characters that Firebase selects from is:
-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz;
As you can see the - is just the first character in this dictionary, which is the only reason the push ids (currently) all start with a -. At some point in the future they will start with a 0, then a 1, etc. If you take the code in the gist, you could calculate when each of those roll-overs happen.
Finally: I once wrote an answer on how to get the timestamp back from a push id. Doing so is not recommended, but it can be a fun experiment: Can you get the timestamp from a Firebase realtime database key?
Not strictly a response to the question asked but related: based on #Frank's answer above it seems like a regex that will always match a Firebase push ID will look something like this:
const regex = /[a-zA-Z0-9-_;]*/gm;
This regex assumes that the ID in the string will be delimited by /. The - and ; added to cover the remaining character set. Remove the gm pattern flags if you are only after the first match.
I had a problem where I needed to extract the push ID from an URL. The push ID appeared after another known ID. The regex for such a situation can look like this:
let regex = new RegExp(`(?<=${known_ID}\/)[a-zA-Z0-9-_;]*`);