如何在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."
然后如何删除用户?
参考:
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);
});
});