如何从Firebase的实时数据库中删除具有UID的用户? [英] How to delete a user with UID from Real Time Database in Firebase?
问题描述
数据库结构如下
-LGw89Lx5CA9mOe1fSRQ {
uid: "FzobH6xDhHhtjbfqxlHR5nTobL62"
image: "https://pbs.twimg.com/profile_images/8950378298..."
location: "Lorem ipsum, lorem ipsum"
name: "Lorem ipsum"
provider: "twitter.com"
}
如何以编程方式删除所有内容,包括-LGw89Lx5CA9mOe1fSRQ
键?
How can I delete everything, including the -LGw89Lx5CA9mOe1fSRQ
key programmatically?
I looked at this, but it's outdated and deprecated Firebase: removeUser() but need to remove data stored under that uid
我还查看了此,但是这要求用户不断登录(我将用户ID保存在localStorage
中),并且如果我编写firebase.auth().currentUser
,则刷新后将返回null
.数据记录和用户帐户是通过社交网络提供商创建的,我可以在Firebase控制台的 Authentication 和 Database 标签上看到数据.
I've also looked at this, but this requires for user to constantly sign in (I'm saving the user ID in localStorage
) and it returns null
on refresh if I write firebase.auth().currentUser
. Data records and user accounts are created through social network providers and I can see the data both on Authentication and Database tab in the Firebase console.
我已经尝试过使用这些代码,但是它什么也没做.
I've tried with these piece of code but it does nothing.
// currentUser has a value of UID from Firebase
// The value is stored in localStorage
databaseChild.child(currentUser).remove()
.then(res => {
// res returns 'undefined'
console.log('Deleted', res);
})
.catch(err => console.error(err));
最重要的是,我需要一键式同时从 Authentication 标签和 Database 中删除具有特定UID的用户.
The bottom line is, I need to delete the user (with a specific UID) from the Authentication tab and from the Database at the same time with one click.
我知道有一个 Firebase管理SDK 但是我正在创建单页应用程序,并且没有任何后端代码.一切都在前端完成.
I know that there is a Firebase Admin SDK but I'm creating a Single Page Application and I don't have any back end code. Everything is being done on the front end.
感谢您提供任何帮助.
推荐答案
要删除客户端上的用户登录帐户的链接,已经找到的链接是您要在客户端上执行的操作的唯一选择.通常出于安全原因,您希望将大多数操作保留在服务器上,例如用于帐户创建/删除之类的操作,而Firebase会强制执行此操作.仅当您最近登录时才能删除帐户,客户端无法开始删除旧的/随机的帐户.
The link you already found for deleting the user-login-account client-side is your only option if you want to keep the action on the client. Usually you want to keep most of the actions for things like account creation/deletion on the server for security reasons, and Firebase forces the issue. You can only delete your account if you were recently logged in, you can't have client-side start deleting old/random accounts.
更好的选择是创建自己的Cloud Function,以处理与删除用户有关的所有事情.您必须使用已经找到的管理SDK 为此...但是您可以让Cloud Function执行任意数量的操作-它必须从身份验证"标签中删除用户,并删除数据库中的匹配数据.
The better option is to create your own Cloud Function to handle everything related to deleting a user. You would have to use the Admin SDK that you already found for this... but you could have that Cloud Function perform as many actions as you want - it will have to delete the user from the Auth tab, and delete the matching data in the Database.
这篇关于如何从Firebase的实时数据库中删除具有UID的用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!