2016-10-19 119 views
2

我正在使用Firebase中的身份驗證服務,但我不知道如何處理createUserWithEmailAndPassword()的錯誤代碼,如auth/email-already-in-use或auth/invalid-email,在這裏你可以看到錯誤列表https://firebase.google.com/docs/reference/js/firebase.auth.Auth#createUserWithEmailAndPassword如何處理FirebaseAuth例外

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(); 
        } 

       } 
      }); 

    } 

回答

1
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 
       } 
      } 
     } 
    }); 
+0

PS:關於FirebaseAuthUserCollisionException您可以在用戶的​​火力控制檯可以選擇是否可以註冊更多的是通過電子郵件帳戶。認證 - >方法 - >高級 –