firebasegoogle-cloud-firestorefirebase-authenticationsocial-networking

how to manage user display names in firebase cleanly


I have a firebase app that allows users to send one another messages. I'm using Firebase Authentication for login and to store the display name. The app data is in Firestore and the app shows lists of messages with display names, so I'm also storing each display name in a "user" record in Firestore (since afaict there's no way for one user to get another user's display name from Firebase Auth). Duplicating the display name between Auth and Firestore seems fragile so I'm considering not using Auth for this at all and managing the display name only in Firestore, although Auth is nice because I can get my own display name synchronously which I do a lot in my app. But wait, it gets worse...

I'm also storing both userID and display name in many of my Firebase docs to avoid having to "join" to populate display name each time I retrieve a list of docs (I know join is not the correct term for a doc db). Consequently, each time a user changes his display name I have to update every doc in which it's stored. I did this because I think display name updates will be relatively rare compared to retrievals, so I'm optimizing for reads at the expense of the occasional big write. But this also seems fragile.

TL;DR My whole approach feels messy and anyone who's written a social app on top of a doc db will have faced a similar problem, so I'm looking for advice on how to implement this... nicely.

Thanks!


Solution

  • Consider duplicating the display name.

    In large and popular chat rooms like discord, when you send a message your display name is attached to the message, and when you do update your name the messages that you already sent holds to the old display name. Ofc you should also attach the userId to the message, as you probably are already, in order for you to programmatically identify the user and for others to keep sending messages to said userId.

    If you have a strong requirement to avoid "duplicating the display name between Auth and Firestore" then i will advice to stop using the firebase auth displayname and create your own system for displaying custom names.

    Other kind of applications like twitter that do update your display name on older messages are using relational databases to avoid duplication. I heard time and time again that duplicating data is OK in non-relational databases.

    Sadly i do not have any code snippet to share on this topic.