firebase How to use the Firebase Database to keep a list of Firebase Authentication users Handling User Account Data in the Realtime Database

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

The Firebase auth system is the source of a users uid, displayName, photoURL, and maybe email. Password based accounts set these persistent values in the auth system via the .updateProfile method. Storing these values in the Realtime Database, rDB, users node poses the issue of stale data. Display names, for example, may change. To keep these values in synch use local storage in concert with .onAuthStateChange.

on every .onAuthStateChange

  • getItem('displayName') and getItem('photoURL')
  • compare to user.displayName and user.photoURL
  • if different
    • setItem('displayName') and setItem('photoURL')
    • db.ref.child('users').update the values of displayName and/or photoURL

.onAuthStateChange fires on every page load or reload, as well as on every auth state change. It potentially fires often, e.g. multi page apps. However reading and writing to local storage is synchronous and very fast so there will be no noticeable impact on app performance.



Got any firebase Question?