0

저는 Firebase를 사용하고 있으며 "createUserWithEmailAndPassword"라고 불리는 사용자 계정을 만드는 방법을 사용하고 있습니다.createUserWithEmailAndPassword 메서드의 예외를 재정의하는 방법은 무엇입니까?

Firebase references에서이 메소드 중 하나가 암호가 6 자 미만일 때 호출되는 "FirebaseAuthWeakPasswordException"이라는 것을 발견했습니다.

나는이 예외를 잡아 내 자신의 단어와 함께 사용자에게 을 메시지를 보여주고 싶어하지만 난 & 캐치를 시도와 방법을 포장 할 때이 오류를 얻을 : "예외 'com.google.firebase.auth.FirebaseAuthWeakPasswordException을 '해당 try 블록 "에서 throw되지 않습니다. 잠시 동안이 문제를 해결하려했지만 행운이 없었습니다. 여기에 코드 조각이, 당신이 내 그림을 도울 수 있기를 바랍니다 : 당신은 당신이 올바른 오류 코드 또는 예외를 얻을 수없는 이유 어떤 FailureListener을 추가하지 않은

mAuth.createUserWithEmailAndPassword(email, pass) 
      .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { 

       @Override 
       public void onComplete(@NonNull Task<AuthResult> task) { 
        // Log.d(TAG, "createUserWithEmail:onComplete:" + task.isSuccessful()); 

        // 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(getApplicationContext(),"Account has created!",Toast.LENGTH_SHORT).show(); 
        } 
        if (!task.isSuccessful()) { 
         Toast.makeText(getApplicationContext(), "failed!", 
           Toast.LENGTH_SHORT).show(); 
        } 
       } 

      }); 

답변

1

.

는 당신을 위해 무엇을 변경하는 경우 알려 주시기 함이

mAuth.createUserWithEmailAndPassword(email, pass) 
     .addOnFailureListener(this, new OnFailureListener() { 
        @Override 
        public void onFailure(@NonNull Exception e) { 
         if (e instanceof FirebaseAuthException) { 
          ((FirebaseAuthException) e).getErrorCode()); 
          //your other logic goes here 
         } 
        } 
       }) 

처럼 에 mAuth를 추가합니다.

+0

너무 감사합니다! 이제 작동 중입니다. 그리고 나는 단지 "e instanceof"를 각 질문에 대한 참조 링크 인 FirebaseAuthWeakPasswordException, FirebaseAuthInvalidCredentialsException 및 FirebaseAuthUserCollisionException과 같은 가능한 예외로 변경합니다. – zb22

+0

다행 :). 행운을 빕니다. –

1

당신은 instanceoftask.getException()를 호출 한 후 사용해야합니다 :

mAuth.createUserWithEmailAndPassword("[email protected]", "only5") 
    .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { 
     @Override 
     public void onComplete(@NonNull Task<AuthResult> task) { 
      Log.i(TAG, "createUserWithEmail:onComplete:" + task.isSuccessful()); 

      if (!task.isSuccessful()) { 
       Log.w(TAG, "onComplete: Failed=" + task.getException().getMessage()); 
       if (task.getException() instanceof FirebaseAuthWeakPasswordException) { 
        Toast.makeText(MainActivity.this, "Weak Password", Toast.LENGTH_SHORT).show(); 
       } 
      } 
     } 
    }); 
+0

나는 또한 당신의 접근 방식을 주셔서 감사합니다. 나에게 유익한 정보. –

+0

예, 해결하기위한 또 다른 좋은 방법입니다. 감사합니다. 또한 "task.getException(). getMessage()"게시 오류 메시지를 제공하기 전에 "instanceof"를 사용하는 방법은 내 마음을 교차하지 않았다. – zb22

관련 문제