如何使用Android Studio在Firebase中检查“用户电子邮件已存在”

时间:2018-08-05 12:43:51

标签: java android firebase firebase-authentication

我正在尝试更改代码。但是,它失败了。我的输出仍然相同。也就是说,如果用户放置的电子邮件已经存在或不存在。验证“此电子邮件已注册。”仍然出来。为什么?有人可以对我的编码有什么毛病吗?

这是我的方法:-

public boolean isCheckEmail(final String email)
{
    mAuth.fetchProvidersForEmail(email).addOnCompleteListener(new OnCompleteListener<ProviderQueryResult>()
    {
        @Override
        public void onComplete(@NonNull Task<ProviderQueryResult> task)
        {
            boolean check = !task.getResult().getProviders().isEmpty();

            if(check)
            {
                Toast.makeText(Signup.this, "This email has been registered.", Toast.LENGTH_SHORT).show();
            }

            else
            {
                Toast.makeText(Signup.this, "This email has been registered.", Toast.LENGTH_SHORT).show();
            }
        }
    });
    return true;
}

在这一半的编码中,我正在尝试回调isCheckEmail方法:-

 private void RegisterAccount(String firstname, String lastname, String email, String password, String confirmpass)
{
    if (TextUtils.isEmpty(email))
    {
        Toast.makeText(Signup.this, "Enter your email address.", Toast.LENGTH_SHORT).show();
    }

    else if (!isValidEmail(email)) {
        Toast.makeText(Signup.this,"Please enter your valid email address.",Toast.LENGTH_SHORT).show();
    }

    else if (!isCheckEmail(email))
    {
        Toast.makeText(Signup.this, "This email has been registered.", Toast.LENGTH_SHORT).show();
    }

4 个答案:

答案 0 :(得分:2)

fetchProvidersForEmail是异步调用,因此您必须将其返回值与回调一起使用。

在主线程上返回true无效。

这是解决方案:

首先使用方法(成功)创建一个接口

public interface OnEmailCheckListener(){
 void onSucess(boolean isRegistered);
} 

您的checkEmail方法应如下所示:

public void isCheckEmail(final String email,final OnEmailCheckListener listener){
mAuth.fetchProvidersForEmail(email).addOnCompleteListener(new OnCompleteListener<ProviderQueryResult>()
{
    @Override
    public void onComplete(@NonNull Task<ProviderQueryResult> task)
    {
        boolean check = !task.getResult().getProviders().isEmpty();

        listener.onSucess(check);

    }
});

}

最后像这样调用您的isCheckEmail:

isCheckEmail("yourEmail@test.com",new OnEmailCheckListener(){
   onSucess(boolean isRegistered){

     if(isRegistered){
           //The email was registered before
    } else {
           //The email not registered before
    }

   }
});

希望这对您有所帮助。

答案 1 :(得分:1)

Firebase 会自动告诉您要创建帐户的电子邮件是否已经存在。 创建帐户时,您应该检查任务是否成功

if(task.isSuccessful()) {} // you have this code in your last bit of code
else{}                     // you have this in your code already

(

您当前拥有代码

Toast.makeText(getApplicationContext(), "registration not performed", Toast.LENGTH_SHORT).show();

(它几乎是您提供的代码中的最后一行)但您应该将其替换为:

try {
    throw task.getException();
} catch(FirebaseAuthUserCollisionException e) {
    // email already in use
Toast.makeText(getApplicationContext(), "Email already taken!", Toast.LENGTH_SHORT).show();
}

因此您不需要自己检查电子邮件是否存在,因为 Firebase 会自动抛出异常,然后您可以例如显示吐司。

答案 2 :(得分:0)

此方法可以检查电子邮件是否存在

 void checkEmailExistsOrNot(){
    firebaseauth.fetchSignInMethodsForEmail(email.getText().toString()).addOnCompleteListener(new OnCompleteListener<SignInMethodQueryResult>() {
        @Override
        public void onComplete(@NonNull Task<SignInMethodQueryResult> task) {
            Log.d(TAG,""+task.getResult().getSignInMethods().size());
            if (task.getResult().getSignInMethods().size() == 0){
                // email not existed
            }else {
                // email existed
            }

        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            e.printStackTrace();
        }
    });
}

答案 3 :(得分:0)

Sai Gopi Me 的回答的 Kotlin 版本

fun checkEmailExistsOrNot() {

        FirebaseAuth
        .getInstance()
        .fetchSignInMethodsForEmail(binding.etEmail.value())
        .addOnCompleteListener { task ->

            Log.d(TAG, "" + task.result?.signInMethods?.size)

            if (task.result?.signInMethods?.size === 0) {
                // email not existed
                Log.d(TAG, "email  not existed")
            } else {
                // email existed
                Log.d(TAG, "checkEmailExistsOrNot: ")

            }
        }.addOnFailureListener {
                e -> e.printStackTrace()
        }

}