Makibo
Makibo

Reputation: 1679

Firebase push Id: What characters can be inside?

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:

Sample: -KD3rcGMuucRDjKOTK3O

Upvotes: 8

Views: 3630

Answers (2)

Frank van Puffelen
Frank van Puffelen

Reputation: 599401

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?

Upvotes: 15

M.Calugaru
M.Calugaru

Reputation: 442

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

Upvotes: 0

Related Questions