2017-07-21 62 views
1

我想編程我的第一個應用程序,我正在使用Firebase身份驗證電子郵件密碼作爲我的登錄方法。在我的註冊活動中,我想要有電子郵件,密碼和確認密碼。我無法讓應用程序檢查密碼> = 6個字符,並且在創建帳戶之前密碼和確認密碼相等。 該應用程序似乎檢查密碼> = 6,但如果這是真的,它會創建該帳戶,而不檢查密碼=確認密碼。 我也想顯示一條錯誤消息,說用戶名已被使用。試圖設置與firebase身份驗證確認密碼

這裏是我的代碼

private void signUpUser(String email, final String password) { 
    auth.createUserWithEmailAndPassword(email, password) 
      .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { 
       @Override 
       public void onComplete(@NonNull Task<AuthResult> task) { 
        if (!task.isSuccessful()) 
        { 
         if (password.length() < 6) 
          snackbar = Snackbar.make(activity_criar_conta, "Your password must have at least 6 characters.", Snackbar.LENGTH_SHORT); 
            snackbar.show(); 

         String pass2 = etConfirmarSenha.getText().toString(); 

         if (!password.equals(pass2)) 
         snackbar = Snackbar.make(activity_criar_conta, "Both password fields must be identic", Snackbar.LENGTH_SHORT); 
         snackbar.show(); 

        } 
        else{ 
         String emailuser = etCriarEmail.getText().toString(); 
         snackbar = Snackbar.make(activity_criar_conta, "Your account was created with sucess: "+emailuser,Snackbar.LENGTH_SHORT); 
         snackbar.show(); 

回答

1

你想打電話auth.createUserWithEmailAndPassword前檢查你的條件。

你的if語句中還有一個缺失{,所以它沒有按照你的期望執行。應該看起來像這樣:

private void signUpUser(String email, final String password) { 
    if (password.length() < 6) { 
    snackbar = Snackbar.make(activity_criar_conta, "Your password must have at least 6 characters.", Snackbar.LENGTH_SHORT); 
    snackbar.show(); 
    return; 
    } 
    String pass2 = etConfirmarSenha.getText().toString(); 
    if (!password.equals(pass2)) { 
    snackbar = Snackbar.make(activity_criar_conta, "Both password fields must be identical", Snackbar.LENGTH_SHORT); 
    snackbar.show(); 
    return; 
    } 
    auth.createUserWithEmailAndPassword(email, password) 
      .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { 
       @Override 
       public void onComplete(@NonNull Task<AuthResult> task) { 
        if (task.isSuccessful()) { 
         String emailuser = etCriarEmail.getText().toString(); 
         snackbar = Snackbar.make(activity_criar_conta, "Your account was created with sucess: "+emailuser,Snackbar.LENGTH_SHORT); 
         snackbar.show(); 
+1

非常感謝你Ian Barber,真的解決了我的問題。 –