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

Firebase多用户注册失败:认证凭证错误问题求助

问题修复:Firebase注册多用户失败报错

核心错误原因

你的注册逻辑完全写错了——点击注册按钮时,调用的是Firebase的登录方法signInWithEmailAndPassword(),而非注册方法createUserWithEmailAndPassword()。这导致:

  • 第一个用户能“注册成功”,大概率是该邮箱已在Firebase控制台手动创建过,登录方法直接验证通过;
  • 后续新用户注册时,因邮箱未被创建,登录方法返回“凭证不正确”的错误,也就是你看到的提示。

其他待修复问题

  • 输入验证逻辑缺陷:用多个独立if判断,会同时弹出多个输入错误提示,应改用else if串联;
  • HelperClass构造参数错误:传入的是EditText控件对象,而非用户输入的字符串;
  • 数据库写入时机错误:不管注册是否成功,都会向Firebase数据库写入数据,应放在auth操作成功的回调里;
  • Firebase依赖重复:模块级build.gradle中重复引入了firebase-database的不同版本,会导致依赖冲突;
  • 数据库路径冗余:reference已经指向"users"节点,后续写入时无需重复写child("users")。

修正后的代码

1. RegisterActivity修正版

public class RegisterActvity extends AppCompatActivity {
    EditText email, phoneNumber, userName, password;
    Button btnRegister;
    TextView textLogin;
    FirebaseAuth mAuth;
    FirebaseDatabase database;
    DatabaseReference reference;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register_actvity);

        btnRegister = findViewById(R.id.button);
        email = findViewById(R.id.inputEmail);
        phoneNumber = findViewById(R.id.inputPhone);
        password = findViewById(R.id.inputPassword);
        textLogin = findViewById(R.id.account);
        userName = findViewById(R.id.userName);
        mAuth = FirebaseAuth.getInstance();

        textLogin.setOnClickListener(view -> {
            Intent intent = new Intent(RegisterActvity.this, LoginActivity.class);
            startActivity(intent);
        });

        btnRegister.setOnClickListener(view -> {
            database = FirebaseDatabase.getInstance();
            reference = database.getReference("users");
            
            String inputEmail = email.getText().toString().trim();
            String pass = password.getText().toString().trim();
            String inputPhone = phoneNumber.getText().toString().trim();
            String userNameStr = userName.getText().toString().trim();

            // 修正输入验证逻辑,避免多错误同时弹出
            if (inputEmail.isEmpty()) {
                email.setError("邮箱不能为空");
            } else if (userNameStr.isEmpty()) {
                userName.setError("用户名不能为空");
            } else if (pass.isEmpty() || pass.length() < 6) {
                password.setError("密码长度不能少于6位");
            } else if (inputPhone.isEmpty()) {
                phoneNumber.setError("手机号不能为空");
            } else {
                // 替换为正确的注册方法
                mAuth.createUserWithEmailAndPassword(inputEmail, pass)
                        .addOnCompleteListener(task -> {
                            if (task.isSuccessful()) {
                                Toast.makeText(RegisterActvity.this, "用户注册成功", Toast.LENGTH_SHORT).show();
                                // 注册成功后再写入数据库
                                reference.child(inputPhone).child("userName").setValue(userNameStr);
                                reference.child(inputPhone).child("email").setValue(inputEmail);
                                reference.child(inputPhone).child("phoneNumber").setValue(inputPhone);
                                // 注意:不建议明文存储密码到数据库,Firebase Auth会管理密码,可删除该字段
                                // reference.child(inputPhone).child("password").setValue(pass);
                                
                                startActivity(new Intent(RegisterActvity.this, LoginActivity.class));
                            } else {
                                Toast.makeText(RegisterActvity.this, "注册失败:" + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
                            }
                        });
            }
        });
    }
}

2. 模块级build.gradle依赖修正(移除重复项)

plugins {
    id 'com.android.application'
    id("com.google.gms.google-services")
}
android {
    compileSdk 33
    defaultConfig {
        applicationId 'com.example.greendiner'
        minSdk 23
        targetSdk 33
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    buildFeatures {
        viewBinding true
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'com.google.android.gms:play-services-auth:20.4.0'
    implementation 'com.google.android.material:material:1.9.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    testImplementation 'junit:junit:4.13.2'
    implementation 'de.hdodenhof:circleimageview:3.1.0'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
    implementation "androidx.biometric:biometric:1.1.0"
    implementation 'com.google.firebase:firebase-core:18.0.0'
    // 保留最新版本的firebase-database,移除重复依赖
    implementation 'com.google.firebase:firebase-database-ktx:20.2.2'
    implementation 'com.google.firebase:firebase-storage:19.2.1'
    implementation 'com.google.firebase:firebase-inappmessaging-display-ktx:20.3.2'
    implementation 'com.google.firebase:firebase-auth:22.3.1'
    // OneSignalSDK
    implementation 'com.onesignal:OneSignal:[4.0.0, 4.99.99]'
    //gif
    implementation 'pl.droidsonroids.gif:android-gif-drawable:1.2.19'
}

额外注意事项

  • 禁止明文存储密码:Firebase Auth会负责用户密码的安全管理,无需将密码写入数据库,否则存在安全风险;
  • Firebase控制台配置:确保已在Firebase控制台开启“邮箱/密码”登录方式;
  • 依赖版本统一:尽量保持Firebase相关依赖版本一致,避免版本差异导致的兼容性问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 05:26:02