如何处理FirebaseAuth异常 [英] How to handle FirebaseAuth exceptions

查看:65
本文介绍了如何处理FirebaseAuth异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Firebase提供的auth服务,但该服务有效,但我不知道如何处理 createUserWithEmailAndPassword()的错误代码,例如auth/email-in-use-in或auth/无效电子邮件,在这里您可以看到错误列表 https://firebase.google.com/docs/reference/js/firebase.auth.Auth#createUserWithEmailAndPassword

I'm using the auth service from Firebase it works but I don't know how to handle the error codes of createUserWithEmailAndPassword() like auth/email-already-in-use or auth/invalid-email ,here you can see the error list https://firebase.google.com/docs/reference/js/firebase.auth.Auth#createUserWithEmailAndPassword

public void register(View target){
        EditText email  = (EditText) findViewById(R.id.editTextName);
        EditText pass  = (EditText) findViewById(R.id.editTextPass);
        Log.d("email",email.getText().toString());
        Log.d("pass",pass.getText().toString());
        auth.createUserWithEmailAndPassword(email.getText().toString(),pass.getText().toString())
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>(){
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if(task.isSuccessful()){
                        Toast.makeText(RegistroActivity.this, "success",
                                Toast.LENGTH_SHORT).show();
                    }else{
                        Toast.makeText(RegistroActivity.this, "fail",
                                Toast.LENGTH_SHORT).show();
                    }

                }
            });

    }

推荐答案

FirebaseAuth.getInstance().createUserWithEmailAndPassword("EMAIL", "PASSWORD")
   .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
       @Override
       public void onComplete(@NonNull Task<AuthResult> task) {
           if (!task.isSuccessful()) {
               if (task.getException() instanceof FirebaseAuthUserCollisionException) {
               // thrown if there already exists an account with the given email address
               } else if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
               // thrown if the email address is malformed
               } else if (task.getException instanceof FirebaseAuthWeakPasswordException) {
               // thrown if the password is not strong enough
               }
           }
       }
   });

这篇关于如何处理FirebaseAuth异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆