检查是否存在给定电子邮件

时间:2016-10-17 19:03:02

标签: android firebase firebase-authentication

有没有办法知道用户输入的电子邮件在Firebase中是否真实?使用电子邮件和密码方式注册的内置功能是否具有此功能?

编辑:抱歉误解了。我不在乎以前是否使用过该电子邮件,我需要知道的是:如果输入的电子邮件是“已制作”或“真实,存在”

5 个答案:

答案 0 :(得分:18)

是的,您可以创建新帐户或登录:

要进行创建,请阅读createUserWithEmailAndPassword' Docs

createUserWithEmailAndPassword抛出3个例外:

  1. FirebaseAuthWeakPasswordException:如果密码不够强大
  2. FirebaseAuthInvalidCredentialsException:如果电子邮件地址格式错误

  3. FirebaseAuthUserCollisionException如果已存在具有给定电子邮件地址的帐户

  4. 您可以在onCompleteListeneronFailureListener

    中处理该问题

    这是mAuthFirebaseAuth实例的示例:

    mAuth.createUserWithEmailAndPassword(email, password)
                .addOnCompleteListener(
                        new OnCompleteListener<AuthResult>()
                        {
                            @Override
                            public void onComplete(@NonNull Task<AuthResult> task)
                            {
                                if (!task.isSuccessful())
                                {
                                    try
                                    {
                                        throw task.getException();
                                    }
                                    // if user enters wrong email.
                                    catch (FirebaseAuthWeakPasswordException weakPassword)
                                    {
                                        Log.d(TAG, "onComplete: weak_password");
    
                                        // TODO: take your actions!
                                    }
                                    // if user enters wrong password.
                                    catch (FirebaseAuthInvalidCredentialsException malformedEmail)
                                    {
                                        Log.d(TAG, "onComplete: malformed_email");
    
                                        // TODO: Take your action
                                    }
                                    catch (FirebaseAuthUserCollisionException existEmail)
                                    {
                                        Log.d(TAG, "onComplete: exist_email");
    
                                        // TODO: Take your action
                                    }
                                    catch (Exception e)
                                    {
                                        Log.d(TAG, "onComplete: " + e.getMessage());
                                    }
                                }
                            }
                        }
                );
    

    要登录,请先阅读signInWithEmailAndPassword&#39} Docs

    signInWithEmailAndPassword抛出了两个例外:

    1. FirebaseAuthInvalidUserException:如果电子邮件不存在或已停用。
    2. FirebaseAuthInvalidCredentialsException:如果密码错误
    3. 以下是一个例子:

      mAuth.signInWithEmailAndPassword(email, password)
                  .addOnCompleteListener(
                          new OnCompleteListener<AuthResult>()
                          {
                              @Override
                              public void onComplete(@NonNull Task<AuthResult> task)
                              {
                                  if (!task.isSuccessful())
                                  {
                                      try
                                      {
                                          throw task.getException();
                                      }
                                      // if user enters wrong email.
                                      catch (FirebaseAuthInvalidUserException invalidEmail)
                                      {
                                          Log.d(TAG, "onComplete: invalid_email");
      
                                          // TODO: take your actions!
                                      }
                                      // if user enters wrong password.
                                      catch (FirebaseAuthInvalidCredentialsException wrongPassword)
                                      {
                                          Log.d(TAG, "onComplete: wrong_password");
      
                                          // TODO: Take your action
                                      }
                                      catch (Exception e)
                                      {
                                          Log.d(TAG, "onComplete: " + e.getMessage());
                                      }
                                  }
                              }
                          }
                  );
      

答案 1 :(得分:0)

这可能是迟到的答案,但只是对于路过的人可以从答案中获益。我认为检查电子邮件是否存在的最佳方法是将验证电子邮件发送到输入的电子邮件。

尝试查看此链接

https://firebase.googleblog.com/2017/02/email-verification-in-firebase-auth.html

Firebase提供了验证用户电子邮件的简便方法。

答案 2 :(得分:0)

检查输入的电子邮件是否存在几乎是不可能的,但您可以检查输入的电子邮件字符串是否符合有效的电子邮件结构:

if(!isValidEmail(etEmail.getText().toString())) {
         Toast.makeText(getActivity(), "Invalid input for email", Toast.LENGTH_LONG).show();

然后像Jin Liu建议的那样,使用sendEmailVerification(),如:

 mFirebaseUser.sendEmailVerification().addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                if(task.isSuccessful())
                Toast.makeText(getContext(), "Email Verfication Sent", Toast.LENGTH_LONG).show();
                else {
                    try{
                        throw task.getException();
                    }catch (Exception e) {

                        Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_LONG).show();
                    }

                }
            }
        });

答案 3 :(得分:0)

还有另一种解决方案,无需任何创建用户或登录过程。

//check email already exist or not.
    firebaseAuth.fetchSignInMethodsForEmail(email)
            .addOnCompleteListener(new OnCompleteListener<SignInMethodQueryResult>() {
                @Override
                public void onComplete(@NonNull Task<SignInMethodQueryResult> task) {

                    boolean isNewUser = task.getResult().getSignInMethods().isEmpty();

                    if (isNewUser) {
                        Log.e("TAG", "Is New User!");
                    } else {
                        Log.e("TAG", "Is Old User!");
                    }

                }
            });

答案 4 :(得分:0)

使用这个: task.getException().getMessage() 获取异常

相关问题