有什么方法可以从Firebase数据库身份验证和Firebase数据库UID中删除匿名用户?

时间:2019-03-22 03:54:13

标签: android firebase firebase-realtime-database firebase-authentication

当前发生的情况:当用户使用onBackPressedcase R.id.logoutMenuOption作为匿名用户注销时,其Firebase身份验证和Firebase数据库Uid将被正确删除。除非用户轻扫关闭应用程序,否则会调用onDestroy()方法,但其Firebase Auth和Firebase数据库Uid仍保留在数据库中。当用户滑动关闭应用程序时,我使用onDestroy()

我需要做什么:当用户滑动关闭应用程序时,将调用onDestroy()removeAnonymousVendorUser()方法来删除匿名用户身份验证以及其用户实时数据库中的Uid。

到目前为止,我已经做过的事情:我创建了removeAnonymousVendorUser()方法并将其放置在onDestroy()方法中。我还将展示如何在其他注销方法中使用它,并且在那些方法中它确实可以工作,但是对于onDestroy()

来说却不起作用
  @Override
protected void onDestroy() {
    super.onDestroy();
    //TODO: Figure out how to delete UID and user when they sign out
    removeAnonymousVendorUser();
    FirebaseAuth.getInstance().signOut();
    finish();
}

用于删除匿名用户Firebase Auth数据和Firebase数据库数据的方法:

  private void removeAnonymousVendorUser() {

    if (FirebaseAuth.getInstance().getCurrentUser() != null) {

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

        user.delete();
    }

    DatabaseReference vendorIdReference = FirebaseDatabase.getInstance().getReference("Vendor");
    vendorIdReference.removeValue();

}

用户单击“注销”菜单选项按钮时的注销方法:

           //Logout Menu Option Button
              @Override
            public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {

             // If Logout Button is clicked within 2 Seconds log user out
            case R.id.logoutMenuOption:
            if (logoutAppPressAgain + 2000 > System.currentTimeMillis()) {
                logoutToast.cancel();

                removeAnonymousVendorUser();
                FirebaseAuth.getInstance().signOut();
                removeAnonymousVendorUser();
                Intent logoutIntent = new Intent(VendorMapsActivity.this, WelcomeActivity.class);
                startActivity(logoutIntent);
                finish();
            } else {
                logoutToast = Toast.makeText(getBaseContext(), R.string.press_button_again_to_logout, Toast.LENGTH_SHORT);
                logoutToast.show();
            }

            logoutAppPressAgain = System.currentTimeMillis();
    }

    return super.onOptionsItemSelected(item);
     }

当用户按下两次时退出方法:

        public void onBackPressed() {
    if (exitAppPressBack + 2000 > System.currentTimeMillis()) {
        exitAppBackToast.cancel();
        super.onBackPressed();

        removeAnonymousVendorUser();
        FirebaseAuth.getInstance().signOut();
        Intent logoutIntent = new Intent(VendorMapsActivity.this, 
        WelcomeActivity.class);
        startActivity(logoutIntent);
        finish();
        } else {
        exitAppBackToast = Toast.makeText(getBaseContext(), 
        R.string.press_back_one_more_time_to_exit, Toast.LENGTH_SHORT);
        exitAppBackToast.show();
        }

    exitAppPressBack = System.currentTimeMillis();
}

这是在用户匿名登录时创建用户的代码:

     public void sellAnonymously(View view) {

    circProgressBar.setVisibility(View.VISIBLE);

    firebaseAuth.signInAnonymously().addOnSuccessListener(VendorLoginActivity.this, new OnSuccessListener<AuthResult>() {
        @Override
        public void onSuccess(AuthResult authResult) {

            Toast.makeText(VendorLoginActivity.this, "Success!  Enjoy the app!", Toast.LENGTH_LONG).show();
            String client_id = firebaseAuth.getCurrentUser().getUid();
            DatabaseReference client_db = FirebaseDatabase.getInstance().getReference().child("Vendor").child(client_id);
            client_db.setValue(true);
            circProgressBar.setVisibility(View.INVISIBLE);

            // Sends anonymous user to VendorMapsActivity
            startActivity(new Intent(VendorLoginActivity.this, VendorMapsActivity.class));

        }
    });
}

1 个答案:

答案 0 :(得分:1)

要删除用户,请按deleting a user上的Firebase文档中所示呼叫user.delete()

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

user.delete()

要从当前用户的数据库中删除数据,请在对数据的引用上调用removeValue(),如deleting data上的Firebase文档所示:

ref.removeValue()

您可以使用Cloud Functions for Firebase将两者结合起来。在这种情况下,您通过从数据库中删除其数据来响应被删除的用户。有关此内容的简短说明,请参见triggering Cloud Functions when a user is deleted上的Firebase文档:

exports.deleteUserData = functions.auth.user().onDelete((user) => {
  // ...
})