2017-10-12 75 views
0

如何在應用中停用經過身份驗證的用戶?我在YouTube的一些指南中跟隨,但它沒有奏效。它說「無法刪除您的帳戶」。我已經包含獲取當前用戶,但仍未能將該帳戶從Firebase中移除。無法使用android firebase刪除應用中的帳戶

我的java類:

public class GeneralSettingActivity extends AppCompatActivity { 

// private button edit text etc here 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_general_setting); 

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

    fullName.setVisibility(View.GONE); 
    userName.setVisibility(View.GONE); 
    oldEmail.setVisibility(View.GONE); 
    newEmail.setVisibility(View.GONE); 
    password.setVisibility(View.GONE); 
    newPassword.setVisibility(View.GONE); 
    changeFullname.setVisibility(View.GONE); 
    changeUsername.setVisibility(View.GONE); 
    changeEmail.setVisibility(View.GONE); 
    changePassword.setVisibility(View.GONE); 
    sendEmail.setVisibility(View.GONE); 
    remove.setVisibility(View.GONE); 

    progressBar = (ProgressBar) findViewById(R.id.progressBar); 

    if (progressBar != null) { 
     progressBar.setVisibility(View.GONE); 
    } 

    //get firebase auth instance 
    auth = FirebaseAuth.getInstance(); 
    //get current user 
    userID = auth.getCurrentUser().getUid(); 
    mTuteeDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child("Tutees").child(userID); 

    getUserInfo(); 

    //get current user 
    final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); 




    authListener = new FirebaseAuth.AuthStateListener() { 
     @Override 
     public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { 
      FirebaseUser user = firebaseAuth.getCurrentUser(); 
      if (user == null) { 
       // user auth state is changed - user is null 
       // launch login activity 
       startActivity(new Intent(GeneralSettingActivity.this, LoginActivity.class)); 
       finish(); 
      } 
     } 
    }; 

    btnRemoveUser.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 



      progressBar.setVisibility(View.VISIBLE); 
      if (user != null) { 
       user.delete() 
         .addOnCompleteListener(new OnCompleteListener<Void>() { 
          @Override 
          public void onComplete(@NonNull Task<Void> task) { 
           if (task.isSuccessful()) { 
            Toast.makeText(GeneralSettingActivity.this, "Your profile is deleted:(Create a account now!", Toast.LENGTH_SHORT).show(); 
            startActivity(new Intent(GeneralSettingActivity.this, RegisterActivity.class)); 
            finish(); 
            progressBar.setVisibility(View.GONE); 
           } else { 
            Toast.makeText(GeneralSettingActivity.this, "Failed to delete your account!", Toast.LENGTH_SHORT).show(); 
            progressBar.setVisibility(View.GONE); 
           } 
          } 
         }); 
      } 
     } 
    }); 

@Override 
protected void onResume() { 
    super.onResume(); 
    progressBar.setVisibility(View.GONE); 
} 

@Override 
public void onStart() { 
    super.onStart(); 
    auth.addAuthStateListener(authListener); 
} 

@Override 
public void onStop() { 
    super.onStop(); 
    if (authListener != null) { 
     auth.removeAuthStateListener(authListener); 
    } 
} 
} 

不得不刪除一些代碼導致它不會讓我張貼此問題,因爲它的大部分代碼

+0

Firebase指出,用戶必須最近登錄才能成功刪除,因此您可能需要重新進行身份驗證,然後才能刪除。以下是[re-auth]的代碼(https://firebase.google.com/docs/auth/android/manage-users#re-authenticate_a_user)。 –

+0

謝謝,我忘了那件事。我的錯誤嘿嘿 –

回答

1

爲此,您需要爲用戶首先創建憑證,然後再 - 用這些證書認證用戶。在onSuccess()的重新驗證方法中,您可以成功刪除該用戶。您可以按以下步驟操作:

首先獲取當前用戶實例

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

要求用戶的電子郵件地址和密碼,並創建憑據,然後刪除用戶這樣

try { 

     AuthCredential credential = EmailAuthProvider 
       .getCredential(email, password); 
     if(user!=null) { 
      user.reauthenticate(credential).addOnCompleteListener(new OnCompleteListener<Void>() { 
      @Override 
      public void onComplete(@NonNull Task<Void> task) { 
      user.delete().addOnCompleteListener(newOnCompleteListener<Void>() { 
       @Override 
       public void onComplete(@NonNull Task<Void> task) { 
         //user deleted successfully 
        } 
       }); 
      } 
      }); 
     } 
    }catch (Exception e) 
    { 
     //error 
    } 

} 

這對我有效。希望這會對你有所幫助

+0

謝謝!重寫我的整個代碼後,我錯過了那一個。我忘了getcurrentuser/re認證 –