简体   繁体   English

Firebase 电子邮件/密码注册无效

[英]Firebase Email/Password sign up not working

So I have a sign up function in my app, what it does is ask for an email and password and then creates an account with those parameters.所以我的应用程序中有一个注册功能,它的作用是要求提供电子邮件和密码,然后使用这些参数创建一个帐户。 Then the app logs the user with the newly created account and requests the email and uid that Firestore created, and puts it in a collection.然后应用程序使用新创建的帐户记录用户并请求 Firestore 创建的电子邮件和 uid,并将其放入一个集合中。

For whatever reason, the app crashes when I try to retrieve the account details from Firebase and when I check it, no account was ever created and no collection was made (Tho it never got to that point so I guess yeah).无论出于何种原因,当我尝试从 Firebase 检索帐户详细信息时,应用程序崩溃,当我检查它时,没有创建帐户,也没有创建任何集合(但它从未达到那个点,所以我想是的)。

What am I doing wrong?我究竟做错了什么?

This is the code (And no, for this project I don't need any username):这是代码(不,对于这个项目,我不需要任何用户名):

public class Register extends AppCompatActivity {
    EditText RegisterEditTextEmail, RegisterEditTextPassword, RegisterEditTextPasswordAgain;
    Button RegisterButton;
    FirebaseAuth auth = FirebaseAuth.getInstance();
    FirebaseFirestore db = FirebaseFirestore.getInstance();
    boolean emailGood = false, passwordGood = false, passwordMatch = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        RegisterButton = findViewById(R.id.RegisterButton);
        RegisterEditTextEmail = findViewById(R.id.RegisterEditTextEmail);
        RegisterEditTextPassword = findViewById(R.id.RegisterEditTextPassword);
        RegisterEditTextPasswordAgain = findViewById(R.id.RegisterEditTextPasswordAgain);
        RegisterButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String email = RegisterEditTextEmail.getText().toString();
                String password = RegisterEditTextPassword.getText().toString();
                String passwordAgain = RegisterEditTextPasswordAgain.getText().toString();

               ........
               checking if the email and password are good
               ........

                if (emailGood && passwordGood && passwordMatch) {
                    auth.createUserWithEmailAndPassword(RegisterEditTextEmail.getText().toString(), RegisterEditTextPassword.getText().toString())
                            .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                                @Override
                                public void onComplete(Task<AuthResult> task) {
                                    if(task.isComplete()){
                                        FirebaseUser user = auth.getCurrentUser();
                                        Map<String, Object> map = new HashMap<>();
                                        map.put("email", user.getEmail()); <---- crashes here
                                        map.put("uuid", user.getUid());
                                        db.collection("users")
                                                .document(user.getUid().toString())
                                                .set(map)
                                                .addOnSuccessListener(new OnSuccessListener<Void>() {
                                                    @Override
                                                    public void onSuccess(Void unused) {
                                                        Intent intent = new Intent(Register.this, MainActivity.class);
                                                        startActivity(intent);
                                                    }
                                                }).addOnFailureListener(new OnFailureListener() {
                                            @Override
                                            public void onFailure(@NonNull Exception e) {
                                                Toast.makeText(Register.this, e.getMessage().toString(), Toast.LENGTH_LONG);
                                            }
                                        });
                                    }
                                }
                            });

                }
            }
        });
    }
}

The error I get:我得到的错误:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.clockin, PID: 3607
    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.auth.FirebaseUser.getEmail()' on a null object reference
        at com.example.clockin.Register$1$1.onComplete(Register.java:74)
        at com.google.android.gms.tasks.zzj.run(com.google.android.gms:play-services-tasks@@17.2.0:4)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

Edit: Someone suggested a NullPointer solution, I guess I failed to elaborate further.编辑:有人提出了 NullPointer 解决方案,我想我没有进一步阐述。 My problem isn't that the account is returned as null, my problem is that for whatever reason, FirebaseAuth refuses to create an account in the first place.我的问题不是该帐户返回为 null,我的问题是无论出于何种原因,FirebaseAuth 首先拒绝创建帐户。 And as a result, the account is null.结果,该帐户为空。 Why won't FirebaseAuth make an account?为什么 FirebaseAuth 不创建帐户?

Edit 2: Apparently my API key in Google Cloud is expired, I tried looking around for a fix but didn't manage to find one, anyone knows how to fix it?编辑 2:显然我在 Google Cloud 中的 API 密钥已过期,我尝试四处寻找修复程序,但没有找到,有人知道如何修复吗?

The problem is in this snippet:问题出在这个片段中:

auth.createUserWithEmailAndPassword(RegisterEditTextEmail.getText().toString(), RegisterEditTextPassword.getText().toString())
    .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(Task<AuthResult> task) {
            if(task.isComplete()){
                FirebaseUser user = auth.getCurrentUser();
                Map<String, Object> map = new HashMap<>();
                map.put("email", user.getEmail());

When a Task completes it can either be successful or unsuccessful and you need to handle both case separately.Task完成时,它可以是成功的,也可以是不成功的,您需要分别处理这两种情况。 Only when the task is successful will auth.getCurrentUser() have a value.只有当任务成功时auth.getCurrentUser()才会有值。

So you should only try to access that (and its getEmail ) when the task is successful:所以你应该只在任务成功时尝试访问它(和它的getEmail ):

public void onComplete(Task<AuthResult> task) {
    if(task.isSuccessful()){ // 👈 Change this test
        FirebaseUser user = auth.getCurrentUser();
        Map<String, Object> map = new HashMap<>();
        map.put("email", user.getEmail());

When the task failed, you can learn more about why it failed by logging the exception it contains.当任务失败时,您可以通过记录它包含的异常来详细了解它失败的原因。 So:所以:

public void onComplete(Task<AuthResult> task) {
    if(task.isSuccessful()){ // 👈 Change this test
        FirebaseUser user = auth.getCurrentUser();
        Map<String, Object> map = new HashMap<>();
        map.put("email", user.getEmail());
        ...
    }
    else {
        Log.e("AUTH", "Creating user failed", task.getException()); // 👈
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Android 进行 Firebase 电子邮件和密码身份验证 - 用户注册 - Firebase Email and Password Authentication with android - User sign up Android Firebase使用电子邮件和密码登录 - Android Firebase sign in with email and password 尝试在Android Studio中使用Firebase使用电子邮件和密码进行注册时出错 - Error while trying to sign up using email and password using firebase in android studio 如何使用 email 和密码登录 firebase 管理员 sdk 在 ZE84E30B9390CDB464DB6DB2C9ABZ78 - How to sign in with email and password using firebase admin sdk in Android Android-Firebase身份验证不适用于电子邮件/密码设置 - Android - Firebase Authentication not working with Email/Password setup 注册后存储用户的电子邮件和密码 - Store user's email and password after sign up 为什么我无法将Firebase电子邮件密码登录链接到Google登录? - Why can't I link firebase email-password sign-in to google sign-in? 是否可以在我们的应用程序中使用电子邮件和密码查看 firebase 登录用户的密码 - Is it possible to view password of firebase sign-in users with email and password in our app 是Firebase登录方法signInWithEmailAndPassword(email,password); 在 flutter 中使用发送散列密码? - Is the Firebase sign in method signInWithEmailAndPassword(email, password); use in flutter sends the password hashed? Firebase无法接收电子邮件/密码身份验证 - Firebase doesn't pick up email/password authentication
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM