2017-06-03 40 views
0

我想創建一個Firebase用戶,但是我的代碼失敗,不斷收到身份驗證失敗的消息,請問爲什麼? 此外,我不知道新的Firebase如何連接到我的帳戶,我曾經創建Firebase對象並將鏈接傳遞給它,但現在不同了,我看不到它在哪裏做它(它問在我的網頁瀏覽器登錄,就是這樣,我無法找到它的代碼的引用)爲什麼我不能創建Firebase用戶

@Override 
    public void onClick(View v) { 
     pass.setText(pass.getText().toString().trim()); 
     email.setText(email.getText().toString().trim()); 
     mAuth.createUserWithEmailAndPassword(email.getText().toString(), pass.getText().toString()) 
       .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { 
        @Override 
        public void onComplete(@NonNull Task<AuthResult> task) { 
         if (task.isSuccessful()) { 
          FirebaseUser user = mAuth.getCurrentUser(); 
         } else { 
          Toast.makeText(MainActivityC.this, "Authentication failed.", 
            Toast.LENGTH_SHORT).show(); 
         } 
        } 
       }); 
    } 
+0

你會得到什麼錯誤?您是否看到Firebase控制檯檢查用戶是否已創建 –

回答

2

檢查下面的代碼,供大家參考。它會幫助你。

private FirebaseAuth mAuth; 
    String mUserEmail = "[email protected]"; 
    String mPassword = "password"; 

    mAuth.createUserWithEmailAndPassword(mUserEmail, mPassword) 
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { 
      @Override 
      public void onComplete(@NonNull Task<AuthResult> task) { 

       Log.d(LOG_TAG, getString(R.string.log_message_auth_successful) + " createUserWithEmail:onComplete:" + task.isSuccessful()); 
      // if task is successful then AuthStateListener will get notified you can get user details there. 
       // if task is not successful show error 
       if (!task.isSuccessful()) { 

        try { 
         throw task.getException(); 
        } catch (FirebaseAuthUserCollisionException e) { 
         // log error here        

        } catch (FirebaseNetworkException e) { 
         // log error here 
        } catch (Exception e) { 
        // log error here   
        } 

        } else { 

       // successfully user account created 
      // now the AuthStateListener runs the onAuthStateChanged callback 

       } 
      } 

     }); 
    } 
+0

感謝此工作 –