首页 > 解决方案 > 如何检查用户是否已在 Firebase 中注册?

问题描述

我想验证用户是否已经注册。这是我的代码。请帮忙。谢谢。

fAuth           =  FirebaseAuth.getInstance();

    signUpBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if((validateName()||validateLastName()||validateEmail()||validatePassword()||validateRepeatPassword())!= true){
                String email = uEmail.getText().toString().trim();
                String password = uPassword.getText().toString().trim();
                fAuth.createUserWithEmailAndPassword(email,password);
                Toast.makeText(SignUpActivity.this,"Welcome!",Toast.LENGTH_SHORT).show();
                openPhoneActivity();
            }else {
                //here will be toast with something like "You are  already registered"
            }
            }
        });

标签: javaandroidfirebasefirebase-authentication

解决方案


该方法createUserWithEmailAndPassword将检查电子邮件是否已存在于 firebase 身份验证中。从文档:

FirebaseAuthUserCollisionException如果已经存在具有给定电子邮件地址的帐户,则抛出

您可以使用addOnCompleterListener()来了解创建是否成功:

                auth.createUserWithEmailAndPassword(email, password)
                        .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                            @Override
                            public void onComplete(@NonNull Task<AuthResult> task) {
                                if (!task.isSuccessful()) {
                                   Toast.makeText((getApplicationContext(), "Authentication failed: " + task.getException().getMessage(),Toast.LENGTH_SHORT).show();

https://firebase.google.com/docs/reference/android/com/google/firebase/auth/FirebaseAuth#public-taskauthresult-createuserwithemailandpassword-string-email,-string-password


推荐阅读