Firebase中为何使用UID?用户均用Google ID时是否仍需使用?
Great question—this is a common point of confusion when working with Firebase Auth and third-party providers like Google. Let me break down why sticking with Firebase's UID is still a smart move, even if Google IDs are unique:
Decouple your app from a single identity provider
Right now you only use Google sign-in, but what if later you want to add Facebook, email/password, or Apple sign-in? If you've built your database around Google IDs, you'll have to rewrite logic to handle multiple identifier types. Firebase UIDs act as a universal "bridge"—no matter how a user logs in, they get the same UID tied to their account. This keeps your data structure clean and future-proof.Better security and control
Firebase's security rules (for Firestore, Realtime Database, Storage, etc.) are natively built around the Firebase UID. Using expressions likerequest.auth.uid === resource.data.uidto restrict access is straightforward and officially supported. While Google IDs are unique, they're managed by Google—not you. If you need to disable a user or manage account permissions, doing it through Firebase's UID system is more direct and doesn't depend on external services.Avoid risks of third-party identifier changes
While Google IDs are currently stable, there's no guarantee Google won't modify their format or policies down the line. Firebase UIDs are guaranteed to remain consistent for the lifetime of a user's account in your project. Relying on Firebase's own identifier eliminates the risk of breaking your data associations if Google makes changes.Seamless Firebase ecosystem integration
Most Firebase services are designed to work hand-in-hand with Firebase Auth's UID. For example, Cloud Functions triggers for auth events (like user creation) use the UID, and analytics tools tie user behavior to their UID. Using Google IDs would mean extra work to map them to Firebase's ecosystem features.
Quick example
Instead of structuring your Firestore collection like:
users/{googleId}
Go with:
users/{firebaseUid}
You can still store the user's Google ID in their document if you need it (for things like fetching their Google profile), but use the UID as the primary key for all your app's data relationships.
In short: Even with Google-only sign-in, using Firebase UID is the more flexible, secure, and future-proof choice.
内容的提问来源于stack exchange,提问作者dontknowhy

