如何在應用中停用經過身份驗證的用戶?我在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);
}
}
}
不得不刪除一些代碼導致它不會讓我張貼此問題,因爲它的大部分代碼
Firebase指出,用戶必須最近登錄才能成功刪除,因此您可能需要重新進行身份驗證,然後才能刪除。以下是[re-auth]的代碼(https://firebase.google.com/docs/auth/android/manage-users#re-authenticate_a_user)。 –
謝謝,我忘了那件事。我的錯誤嘿嘿 –