Firebase push Id: What characters can be inside? - firebase

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-_;]*`);

Related

What is the Firebase Id Format called?

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.

Javascript generate unique short code from an existing unique code

Given that I already have a guaranteed unique string, is it possible to generate a shorter version of this string while keeping the newly generated strings also unique?
The existing unique code is the id generated by firebase firestore, the ID is too long and I prefer not to use it as referral code. I was hoping to limit the length of the string around 8-10 chars.
EX of firebase id: NIzTFZUC64Voc4ttiiNsBF0GNWF3
I need a much shorter version of this string
I also don't want the codes to be case sensitive

how to sort firebase auto generated key?

I am working with firebase real time database. Every time I push the data into the firebase using push method it auto generates a key like this
"-L1GgaMStpwEV4N3sQad" to my best knowledge it's generated based on time also.
So my question is there any way to sort the key asce or in desc?.
I have attached image of firebase auto generated.
Thanks in advance.
I new to firebase but here's my observation :
The unique keys that are generated are already sorted in Lexicographical order.
Have a closer look at your objects:
-L1GgaM is common for all keys (except last which ends with 'N')
Also, remember that capital letters have lower ASCII value than their counterpart.

Invalid characters in Firebase Auth uid?

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.

MVC3 routes - replace id with object name

I'm looking for a fast & elegant way of converting my object IDs with descriptive names, so that my autogenerated routes look like:
/products/oak-table-25x25-3-1
instead of
/products/5bd8c59c-fc37-40c3-bf79-dd30e79b55a5
In this sample:
uid = "5bd8c59c-fc37-40c3-bf79-dd30e79b55a5"
name = "Oak table (25x25) 3/1"
I don't even know how that feature could be named, so that I might google for it.
The problem that I see so far is the uniqueness of that "url-object-name", for example if I have two oak tables 25x35 in the db, and their names differ too little to be uniquely url-named but enough to fool the unique constraint in the db.
I'm thinking of writing that function for name-transform in SQL as an UDF, then adding a calculated field that returns it, then unique-constraining that field.
Is there some more mainstream way of achieving that?
One method is that employed by stackoverflow.com which in your case would be:
/products/5bd8c59c-fc37-40c3-bf79-dd30e79b55a5/oak-table-25x25-3-1
This ensures uniqueness, however the length of the UUID may be a deterrent. You may consider adding a sequential int or bigint identity value to the products table in addition to the uniqueidentifier field. This however would require an additional index on that column for lookup, though a similar index would be required for a Url having only a descritive string. Yet another method would be to use a hash value, seeded by date for instance, which you can compose with the descriptive name. It is simpler to rely on a sequential ID value generated by a database, but if you envision use NoSQL storage mechanisms in the future you may consider using an externally generated hash value to append.
Identity should have 2 properties: it should be unique and unchangable. If you can guarantee, that /products/oak-table-25x25-3-1 will never change to /products/oak-table-25x25-3-1-1 (remember, user can have bookmarks, that shouldn't return 404 statuscode)- you can use name as url parameter and get record by this parameter.
If you can't guarantee uniqueness or want to select record more faster - use next:
/products/123/oak-table-25x25-3-1 - get record by id (123)
/products/123/blablabla - should redirect to first, because blabla no exists or have anoher id
/products/123 - should redirect to first
And try to use more short identities - remember, that at web 2.0 url is a part of UI, and UI should be friendly.
MVC routing (actions) will handle spaces and slashes in a name. It will encode them as %20, and then decode them correctly.
Thus your URL would be /products/oak%20table%2025x25-3%2F1
I have done something very similar in an eCommerce platform I am working on.
The idea is that the URL without the unique ID is better for SEO but we didn't want the unique ID to be the product name that can change often.
The solution was to implement .NET MVC "URL slug only" functionality. The product manager creates "slugs" for every product that are unique and are assigned to products. These link to the product but the product ID and name can be changed whenever.
This allows:
domain.com/oak-table-25x25-3-1
to point to:
/products/5bd8c59c-fc37-40c3-bf79-dd30e79b55a5
(The same functionality can be used on categories too so domain.com/tables can point to domain.com/category/5b38c79c-f837-42c3-bh79-dd405479b15b5)
I have documented how I did this at:
http://makit.net/post/3380143142/dotnet-slug-only-urls

Resources