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

8 android firebase firebase-authentication

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

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

J.D*_*gon 32

这是另一种没有任何创建用户或登录过程的解决方案。

//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!");
                    }

                }
            });
Run Code Online (Sandbox Code Playgroud)


amr*_*rro 22

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

用于创建,读取createUserWithEmailAndPassword文档

createUserWithEmailAndPassword 抛出3个例外:

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

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

你可以在onCompleteListener或处理onFailureListener

下面的例子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());
                                }
                            }
                        }
                    }
            );
Run Code Online (Sandbox Code Playgroud)

对于登录,阅读signInWithEmailAndPassword文档第一.

signInWithEmailAndPassword 抛出两个例外:

  1. FirebaseAuthInvalidUserException:如果电子邮件不存在或已禁用.
  2. FirebaseAuthInvalidCredentialsException:如果密码错误

这是一个例子:

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());
                                }
                            }
                        }
                    }
            );
Run Code Online (Sandbox Code Playgroud)