You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

同一手机上Firebase数据库每次获取的用户ID为何变化?

Firebase User ID Changes on Same Device - Troubleshooting Tips

Hey there! No worries about your English at all—let's dig into why Firebase is giving you a different user ID every time on the same phone. 😊

First, let's go through the most common causes and fixes for this issue:

1. You're creating a new user every time instead of reusing an existing one

If your code calls a user creation method (like createUserWithEmailAndPassword or signInAnonymously) on every app launch without first checking if a user is already logged in, Firebase will generate a fresh user ID each time.

The fix: Always check for an existing logged-in user first before initiating a new login/signup flow. Here's a quick example of how to do that:

FirebaseAuth mAuth = FirebaseAuth.getInstance();

@Override
protected void onStart() {
    super.onStart();
    // Check if user is already signed in
    FirebaseUser currentUser = mAuth.getCurrentUser();
    if (currentUser != null) {
        // Use the existing user's ID
        String stableUserId = currentUser.getUid();
        // Do something with the ID...
    } else {
        // Only create/sign in a new user if no one is logged in
        // Your login logic here (e.g., signInAnonymously(), createUserWithEmailAndPassword())
    }
}

2. Firebase Auth persistence is disabled

By default, Firebase keeps users logged in across app restarts and device reboots. But if you've manually disabled persistence with setPersistenceEnabled(false), the user session will be lost when the app closes. Next time you open the app, you'll have to sign in again, which might generate a new ID (especially with anonymous login).

Double-check your code for this line:

FirebaseAuth.getInstance().setPersistenceEnabled(false);

If you see it, either remove it or change false to true to keep the user session persistent.

3. Issues with anonymous login (if that's what you're using)

Anonymous user IDs are tied to the app's data on the device. If you're clearing app data, uninstalling/reinstalling the app, or using a device cleaner that wipes app storage, the anonymous user will be deleted, and Firebase will create a new one. But if this is happening without those actions, go back to the first two points—you're likely not persisting the session or checking for existing users properly.

4. Missing key code details

The code snippet you shared only shows UI elements and a DatabaseReference—we don't see the actual Firebase Auth logic you're using to get the user ID. Could you add:

  • Which Firebase Auth method you're using (anonymous, email/password, social login, etc.)?
  • The exact code where you fetch the user ID?
  • Any code that signs out the user or clears auth state?

That extra info will help narrow down the problem even faster!


内容的提问来源于stack exchange,提问作者MIkey014

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:25:02