如何在Firebase中自动删除用户? [英] How to automatically delete a user in Firebase?

查看:54
本文介绍了如何在Firebase中自动删除用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码:

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);


情况:

我在app.js

什么都没发生.

在"1"和"2"之后,什么都不会打印到控制台.

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

我不能在snapshot上呼叫.remove().

此外,从文档中:

要删除用户,该用户必须最近登录.请参阅重新验证用户."

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

然后如何删除用户?

参考:

删除2小时之前的Firebase数据

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

推荐答案

上没有没有delete方法快照.相反,您应该只在快照的ref上调用remove:

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);
    });
});

这篇关于如何在Firebase中自动删除用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆