2016-10-01 57 views
2
private void registerUser(){ 
String emailId = email.getText().toString().trim().toLowerCase(); 
String password = pass.getText().toString().trim(); 

firebaseAuth.createUserWithEmailAndPassword(emailId,password) 
     .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { 
      @Override 
      public void onComplete(@NonNull Task<AuthResult> task) { 
       progressDialog.dismiss(); 
       if(task.isSuccessful()){ 
        Toast.makeText(MainActivity.this,"Registration Successful",Toast.LENGTH_SHORT).show(); 

        //show chatroom 
        finish(); 
        startActivity(new Intent(getApplicationContext(),ProfileActivity.class)); 
       } 
       else{ 
        Toast.makeText(MainActivity.this,"Registration Failed. Please try again",Toast.LENGTH_SHORT).show(); 
       } 
      } 
     }); 
} 

我想添加一個用戶名或顯示名稱,但無法這樣做。我嘗試了一些東西,但仍然沒有結果。請幫助我。本週我需要此功能進行項目提交。如何在Firebase中通過電子郵件+密碼認證添加DisplayName? Android

+0

你不能做到這一點使用FirebaseAuth,如果你要存儲用戶的細節,你需要做手工,使用RealtimeDatabase –

+0

@SakchhamSharma有一種方法可以做到這一點,只是不直接創建新用戶時。看到我的回答如下:) – edant92

+0

@ edant92我在創造時間 –

回答

2

這絕對是可能的,但不在用戶創建方法中。

一旦你(在addOnSuccessListener可能)創建你的用戶,你可以使用類似於下面的代碼的東西來更新用戶顯示名稱:

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); 

UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder().setDisplayName("John Smith").build(); 

user.updateProfile(profileUpdates); 

希望這有助於!

編輯:我之前說過要將代碼添加到AuthStateListener,但是,弗蘭克的建議將其放在addOnSuccessListener中會更好/更有意義,因此我已更新了答案以反映此情況。

+2

好的答案!我可能會在'addOnSuccessListener'中執行此操作,因爲在創建帳戶時通常只需設置名稱。 –

0

我剛剛爲我自己的實現(SDK版本4.4.1)調查了這個問題。我發現,如果你確定使用來自注冊/登錄的完全相同的task.result對象,而不是來自默認實例的對象,它可以完美地工作。

圍繞着另一種工作幫助我是有一個電子郵件的參考表中的FB DB這樣的:

{ "EmailRef": { "username1" : "[email protected] domain .com"}, {"username2" : "[email protected]"} } 

,然後由用戶的電子郵件來查詢用戶名(從auth.CurrentUser.Email)採用的方法是這樣的:

public static void GetCurrentUserName(Firebase.Auth.FirebaseUser user) 
{ 
    string message = ""; 
    DatabaseReference dbRef = FbDbConnection(); 
    FirebaseDatabase.DefaultInstance.GetReference("EmailRef").OrderByValue().EqualTo(user.Email).GetValueAsync().ContinueWith(task => 
    { 
     if (task.IsFaulted) 
     { 
      message = "GetCurrentUserName encountered an error: " + task.Exception; 
      ModalManager.BuildFireBaseDebugModal(message); 
      Debug.LogError(message); 
      return; 
     } 
     if (task.IsCanceled) 
     { 
      message = "GetCurrentUserName was canceled."; 
      Debug.LogError(message); 
      return; 
     } 
     if (task.IsCompleted) 
     { 
      foreach (DataSnapshot ss in task.Result.Children.AsEnumerable()) 
      { 
       try 
       { 
        if (ss.Value != null) 
        { 
         if (ss.Value.ToString() == user.Email) 
         { 
          message = "GetCurrentUserName was successful -- Email: " + user.Email + " Username: " + user.DisplayName; 
          Debug.LogError(message); 
         } 
        } 
        return; 
       } 
       catch (Exception ex) 
       { 
        message = "GetCurrentUserName Exception: " + ex; 
        Debug.LogError(message); 
        return; 
       } 
      } 
     } 

    }); 
} 
相關問題