skip to Main Content

I have come across different variations when it comes to user authentication and I have seen developers include a userId field AND a username field (both strings) in their database so I wondered if there is an advantage if you have both of these fields.

I would rather have only one field (username) to have a better overview and prevent possible mistakes but I’m not sure if there could be problems that come along with that variation.

I’ve already looked on the internet but only found a Quora answer which stated it would make sense to have a userId as an integer since "Integers are easier/faster/less expensive to sort, search, and compare than strings"

2

Answers


  1. The solution in the linked answer applies to SQL databases and not to NoSQL databases. If you’re using Firebase Authentication, I cannot see any reason why would you store the UID or the user names in the database. Why? Because you can always get those values from the current user object without the need of performing a database call. As a general rule, always store in the database only the data that you need in your application code and nothing more.

    Login or Signup to reply.
  2. The UID never changes and is generally used to reference related data in other collections/tables over username (as users are usually allowed to change their names in most of the applications). You have not provided many details on your applications but these are handy for most of the actions such as:

    1. Fetching usernames when sending emails (better to query from a database rather than fetching data from Firebase Auth)
    2. If your application serves user generated content e.g. articles, you might want to show the author’s name and storing the username in the database is quicker than running an additional query to Firebase. Also, a user can read their own data only using the client SDKs.

    I am not sure about which database you use, but as long as you have proper indexes setup, the queries should be fast. It’s pretty common to use UUIDs or MongoBD’s ObjectIDs as user IDs.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search