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

Android App无法注册至Firebase实时数据库及认证错误求助

问题分析与修复方案

注册功能核心错误

你在注册逻辑中误用了Firebase Auth的接口:用登录接口signInWithEmailAndPassword处理注册请求。这个接口是用来验证已存在用户的凭证,新账号尚未创建时调用它,必然返回“凭证不正确、格式错误或已过期”的提示。

注册功能修复点

  • 替换接口:将mAuth.signInWithEmailAndPassword(inputEmail, pass)改为注册专用接口mAuth.createUserWithEmailAndPassword(inputEmail, pass)
  • 完善表单校验:当前仅判断手机号非空就执行逻辑,需改为所有字段(邮箱、用户名、密码、手机号)校验通过后再触发注册
  • 调整数据库写入时机:只有注册成功后,再将用户数据写入Firebase Database,避免注册失败时写入无效数据

重置密码功能错误

重置密码方法中,调用sendPasswordResetEmail(Email)时,全局变量Email从未被赋值,导致传递空邮箱触发失败;同时跳转逻辑存在空Intent调用,会引发崩溃。

重置密码修复点

  • 传递有效邮箱:将用户输入的邮箱直接作为参数传入resetPassword方法,或在点击事件中为Email变量赋值
  • 修正跳转逻辑:正确创建Intent并设置清空栈的标记,避免崩溃且防止用户返回重置页面

修正后的代码

ForgetPassword 修正版

public class ForgetPassword extends AppCompatActivity {
    private EditText emailEditText;
    private ProgressBar progressBar;
    FirebaseAuth mAuth;

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

        emailEditText = findViewById(R.id.inputEmail);
        Button resetPasswordButton = findViewById(R.id.resetPassword);
        progressBar = findViewById(R.id.progressBar);

        resetPasswordButton.setOnClickListener(v -> {
            String email = emailEditText.getText().toString().trim();

            if (email.isEmpty()) {
                Toast.makeText(ForgetPassword.this, "请输入已注册的邮箱", Toast.LENGTH_SHORT).show();
                emailEditText.setError("邮箱不能为空");
                emailEditText.requestFocus();
            } else if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
                emailEditText.setError("请输入有效的邮箱格式");
                emailEditText.requestFocus();
            } else {
                progressBar.setVisibility(View.VISIBLE);
                resetPassword(email);
            }
        });
    }

    private void resetPassword(String email) {
        FirebaseAuth mAuth = FirebaseAuth.getInstance();
        mAuth.sendPasswordResetEmail(email).addOnCompleteListener(task -> {
            progressBar.setVisibility(View.GONE);
            if (task.isSuccessful()) {
                Toast.makeText(ForgetPassword.this, "请检查邮箱获取重置密码链接!", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(ForgetPassword.this, LoginActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                startActivity(intent);
                finish();
            } else {
                Toast.makeText(ForgetPassword.this, "重置失败:" + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }
}

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 -> {
            startActivity(new Intent(RegisterActvity.this, LoginActivity.class));
        });

        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 User = userName.getText().toString().trim();

            boolean isValid = true;
            if (inputEmail.isEmpty() || !Patterns.EMAIL_ADDRESS.matcher(inputEmail).matches()) {
                email.setError(inputEmail.isEmpty() ? "邮箱不能为空" : "请输入有效的邮箱格式");
                isValid = false;
            }
            if (User.isEmpty()) {
                userName.setError("用户名不能为空");
                isValid = false;
            }
            if (pass.isEmpty() || pass.length() < 6) {
                password.setError("密码长度不能少于6位");
                isValid = false;
            }
            if (inputPhone.isEmpty()) {
                phoneNumber.setError("手机号不能为空");
                isValid = false;
            }

            if (isValid) {
                mAuth.createUserWithEmailAndPassword(inputEmail, pass).addOnCompleteListener(task -> {
                    if (task.isSuccessful()) {
                        Toast.makeText(RegisterActvity.this, "注册成功", Toast.LENGTH_SHORT).show();
                        reference.child(inputPhone).child("userName").setValue(User);
                        reference.child(inputPhone).child("email").setValue(inputEmail);
                        reference.child(inputPhone).child("phoneNumber").setValue(inputPhone);
                        startActivity(new Intent(RegisterActvity.this, LoginActivity.class));
                        finish();
                    } else {
                        Toast.makeText(RegisterActvity.this, "注册失败:" + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
    }
}

额外提醒:

  • 禁止在数据库中明文存储用户密码,Firebase Auth已自动加密管理密码,无需自行存储
  • 可添加防重复点击逻辑,避免用户多次触发请求

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 13:55:04