How to automatically delete a user in Firebase?

雨燕双飞 提交于 2019-12-25 18:37:03

问题


CODE:

app.js

setInterval(function() {

    console.log("1");
    var pendingRef = admin.database().ref("pending");
    var now = Date.now();
    var cutoff = now - 1 * 60 * 60 * 1000;
    var old = pendingRef.orderByChild('timestamp').endAt(cutoff).limitToLast(1);
    console.log("2");
    var listener = old.on('child_added', function(snapshot) {
        console.log("3");
        console.log("A VALUE:"+snapshot.val());
        snapshot.delete().then(function() {
            snapshot.ref().remove();
        }, function(error) {
            console.log("USER WAS NOT DELETED:"+ snapshot.val().key);
        });
    });
}, 1000 * 10);

SITUATION:

I call it at the end of my app.js

Nothing happens.

Nothing gets printed to the console after "1" and "2".


EDIT:

I cannot call .remove() on a snapshot.

Also, from the docs:

"To delete a user, the user must have signed in recently. See Re-authenticate a user."

How can I delete a user then ?


REFERENCE:

Delete firebase data older than 2 hours

https://firebase.google.com/docs/auth/web/manage-users#delete_a_user


回答1:


There is no delete method on the snapshot. Instead, you should just call remove on the snapshot's ref:

var listener = old.on('child_added', function (snapshot) {
  console.log("EXECUTING...");
  console.log("A VALUE:" + snapshot.val());
  snapshot.ref
    .remove()
    .catch(function (error) {
      console.log("USER WAS NOT DELETED:" + snapshot.key);
    });
});

To delete the user account - in addition to the user data you have stored - you also need to call the deleteUser method (as you are running this on Node using firebase-admin):

var listener = old.on('child_added', function (snapshot) {
  console.log("EXECUTING...");
  console.log("A VALUE:" + snapshot.val());
  snapshot.ref
    .remove()
    .then(function () {
      return admin.auth().deleteUser(snapshot.key);
    })
    .catch(function (error) {
      console.log("USER WAS NOT DELETED:" + snapshot.key);
    });
});


来源:https://stackoverflow.com/questions/41696708/how-to-automatically-delete-a-user-in-firebase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!