繁体   English   中英

如何检查用户是否已经在firebase中注册?

[英]How to check if user is already registered in 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"
            }
            }
        });

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

 auth.createUserWithEmailAndPassword(email, password) .addOnCompleteListener(CurrentActivity.this, new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { Toast.makeText(SignupActivity.this, "createUserWithEmail:onComplete:" + task.isSuccessful(), Toast.LENGTH_SHORT).show(); // If sign in fails, display a message to the user. If sign in succeeds // the auth state listener will be notified and logic to handle the // signed in user can be handled in the listener. if (!task.isSuccessful()) { Toast.makeText(SignupActivity.this, "Authentication failed." + task.getException(), Toast.LENGTH_SHORT).show(); } else { //Do something here startActivity(new Intent(CurrentActivity.this, RedirectActivity.class)); finish(); } } });

如果已存在具有给定电子邮件地址的帐户,则addOnCompleterListener()将抛出FirebaseAuthUserCollisionException 您可以通过以下代码检查电子邮件是否已注册。

mAuth.createUserWithEmailAndPassword(signup_email, signup_password)
                            .addOnCompleteListener(SignupActivity.this, new OnCompleteListener<AuthResult>() {
                                @Override
                                public void onComplete(@NonNull Task<AuthResult> task) {
                                    if (task.isSuccessful()) {
                                        // Signup success
                                      
                                        Toast.makeText(SignupActivity.this,"Signup Successful!", Toast.LENGTH_SHORT).show();
                                        
                                    } else {
                                    
                                        // If signup fails, and user emial is already registered.
                                         if (task.getException() instanceof FirebaseAuthUserCollisionException) {
                                         
                                            Toast.makeText(SignupActivity.this,"Provided Email Is Already Registered!", Toast.LENGTH_SHORT).show();
                                            
                                        }

                                    }
                                }
                            });

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM