有没有一种简单的方法可以从 firebase 控制台中删除所有注册用户?例如,我从我的开发环境中创建了一百个用户,现在我想删除所有这些用户。
28 回答
正如在更新的答案中一样,您现在可能可以使用 firebase 管理工具,但如果您不想 - 这里有一些更可靠的 javascript 来删除网络中的用户:
var intervalId;
var clearFunction = function() {
var size = $('[aria-label="Delete account"]').size()
if (size == 0) {
console.log("interval cleared")
clearInterval(intervalId)
return
}
var index = Math.floor(Math.random() * size)
$('[aria-label="Delete account"]')[index].click();
setTimeout(function () {
$(".md-raised:contains(Delete)").click()
}, 1000);
};
intervalId = setInterval(clearFunction, 300)
只需在开发人员工具中运行
因为我很懒惰点击 UI 中的按钮和元素,所以我设置了一个小的客户端脚本:
$('[aria-label="Delete account"]').click()
setTimeout(function () {
$(".md-raised:contains(Delete)").click()
}, 1000);
您可能需要多次运行它,但这比浪费时间手动单击屏幕上的东西要好得多。
对于新的 Firebase 更新
试试这个代码以获得最新的 Firebase 更新。打开控制台,粘贴这段代码并回车!!!
setInterval(() => {
document.getElementsByClassName('edit-account-button mat-focus-indicator mat-menu-trigger mat-icon-button mat-button-base')[0].click()
let deleteButtonPosition = document.getElementsByClassName('mat-focus-indicator mat-menu-item ng-star-inserted').length - 1
document.getElementsByClassName('mat-focus-indicator mat-menu-item ng-star-inserted')[deleteButtonPosition].click()
document.getElementsByClassName('confirm-button mat-focus-indicator mat-raised-button mat-button-base mat-warn')[0].click()
}, 1000)
这是我的自行车:
setInterval(() => {
$('[aria-label="Delete account"]').first().click()
setTimeout(()=>{
$(".md-raised:contains(Delete)").click()
}, 100)
}, 2000);
旨在避免delete经常调用端点,因为谷歌因404错误而失败。
使用 Firebase Admin SDK 的完整解决方案
使用 Firebase Admin SDK非常简单,并且是对 Firebase 数据执行此类任务的推荐方法。该解决方案不同于使用开发者控制台的其他临时解决方案。
我只是整理了一个 Node.js 脚本来删除您的 Firebase 身份验证中的所有用户。我已经通过删除约 10000 个用户对其进行了测试。我只是运行了以下 Node.js 代码。
设置 Firebase Admin SDK
新建一个文件夹。在终端中运行以下命令
npm init
sudo npm install firebase-admin --save
index.js现在在这个文件夹中创建一个文件。
要遵循的步骤:
- 转到您的 Firebase 项目 -> 项目设置 -> 服务帐户。
- 单击
Generate new Private Key以下载 JSON 文件。复制 JSON 文件的路径并将其替换为服务帐户私钥 json 文件路径中的以下代码。 databaseURL另外,从设置页面复制。在代码中替换它。- 将代码复制并粘贴到
index.js. - 在终端中运行
node index.js。观看混乱!
var admin = require('firebase-admin');
var serviceAccount = require("/path/to/service/accounts/private/key/json/file");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "/url/to/your/database"
});
function deleteUser(uid) {
admin.auth().deleteUser(uid)
.then(function() {
console.log('Successfully deleted user', uid);
})
.catch(function(error) {
console.log('Error deleting user:', error);
});
}
function getAllUsers(nextPageToken) {
admin.auth().listUsers(100, nextPageToken)
.then(function(listUsersResult) {
listUsersResult.users.forEach(function(userRecord) {
uid = userRecord.toJSON().uid;
deleteUser(uid);
});
if (listUsersResult.pageToken) {
getAllUsers(listUsersResult.pageToken);
}
})
.catch(function(error) {
console.log('Error listing users:', error);
});
}
getAllUsers();
火力基地在这里
在下面更新 2016-11-08原始答案
我们刚刚发布了Firebase Admin SDK,它支持管理用例,例如无需用户先登录即可删除用户帐户。
原始答案
Firebase 身份验证中目前没有 API 可以在不要求用户登录的情况下删除用户。我们知道这会限制我们 API 的可用性,并正在努力在未来的版本中添加此类功能。但像往常一样,我们没有提供该功能何时可用的具体时间表。
目前,您唯一的解决方法是:
- 以应用程序中的每个测试用户身份登录并从那里删除用户
- 从 Firebase 控制台依次删除每个用户
稍微增加了你的帮助脚本。
德国 firebase 网站版本:
$('[aria-label="Nutzermenü öffnen"]').click();
$('[aria-label="Konto löschen"]').click();
for (i = 0; i < 20; i++) {
setTimeout(() => {
$('.md-raised:contains(Löschen)').click();
}, i * 200);
}
对于英文版,只需替换文本。这样,您可以在执行后删除 20 个或更多用户。
2020 年 11 月 16 日成功使用此代码:
setInterval(function () {
$('[aria-label="View more options"]')[0].click()
document.querySelectorAll('.mat-menu-item')[2].click()
document.querySelector('.confirm-button').click()
}, 1000);
setInterval(() => {
if ($('[aria-label="Delete account"]').length > 0) {
$('[aria-label="Delete account"]').first().click()
setTimeout(()=>{
$(".md-raised:contains(Delete)").click()
}, 100)
} else {
$('[aria-label="Reload"]').first().click()
}
}, 2000);
试试这个。这是对上面@www.eugenehp.tk 答案的更新,考虑到如果您有超过一页的条目,则需要刷新页面。
好吧,我使用此脚本在 Firebase 控制台中一次删除所有用户:
$('[aria-label="Delete account"]').each(function() {
$(this).click();
$('[ng-click="controller.submit()"]').click()
})
https://console.firebase.google.com/project/YOUR_PROJECT_NAME/authentication/users
于 2021 年 10 月 10 日测试
这将单击上下文菜单中的最后一个按钮,该按钮始终是“删除”按钮,因此它适用于所有帐户类型setInterval(() => {
document.getElementsByClassName('edit-account-button mat-focus-indicator mat-menu-trigger mat-icon-button mat-button-base')[0].click()
var buttons = document.getElementsByClassName('mat-focus-indicator mat-menu-item ng-star-inserted');
buttons.item(buttons.length - 1).click();
document.getElementsByClassName('confirm-button mat-focus-indicator mat-raised-button mat-button-base mat-warn')[0].click()
}, 1000);
俄语版
var intervalId;
var clearFunction = function() {
if ($('[aria-label="Удаление аккаунта"]').size() == 0) {
console.log("interval cleared")
clearInterval(intervalId)
return
}
$('[aria-label="Удаление аккаунта"]')[0].click();
setTimeout(function () {
$('[ng-click="controller.submit()"]').click()
}, 1000);
};
intervalId = setInterval(clearFunction, 3000)
一个对我有用的解决方案是创建一个单独的文件并导入我的 firebase-admin 并简单地运行以下命令:
const admin = require('./firebase_admin');
const listAllUsers = () => {
console.log('list all users');
// List batch of users, 1000 at a time.
admin.auth().listUsers(1000)
.then((listUsersResult) => {
listUsersResult.users.forEach((userRecord) => {
const user = userRecord.toJSON();
admin
.auth()
.deleteUser(user.uid)
.then(() => {
console.log('successfully deleted user');
})
.catch((err) => {
console.error('error deleting user: ', err);
});
});
if (listUsersResult.pageToken) {
// List next batch of users.
listAllUsers(listUsersResult.pageToken);
}
})
.catch((error) => {
console.log('Error listing users:', error);
});
};
// Start listing users from the beginning, 1000 at a time.
listAllUsers();
这里的概念是我们想从我们的用户身份验证表中检索所有用户,然后使用 deleteUser 管理员身份验证方法一次循环抛出并删除它们。
在终端中,我只是简单地使用 node 来调用文件中的函数(假设文件名是delete_users.js,我刚刚调用node delete_users.js并调用了 listUsers 函数。
希望这可以帮助。
法语版,
var intervalId;
var clearFunction = function() {
if ($('[aria-label="Supprimer le compte"]').size() == 0) {
console.log("interval cleared")
clearInterval(intervalId)
return
}
$('[aria-label="Supprimer le compte"]')[0].click();
setTimeout(function () {
$(".md-raised:contains(Supprimer)").click()
}, 1000);
};
intervalId = setInterval(clearFunction, 3000)
PS:如果您不是 Web 开发人员并且“在工具开发人员中执行”对您来说毫无意义,这里是程序。
- 使用 Chrome 打开您的 firebase 身份验证/用户页面
- 按下 control + Shift+ J
在控制台选项卡中,粘贴此代码并按 Enter。
如果语言与您粘贴的代码匹配,则帐户将开始被删除。
如果你有匿名账户
setInterval(() => {
document.getElementsByClassName('edit-account-button mat-focus-indicator mat-menu-trigger mat-icon-button mat-button-base')[0].click()
document.getElementsByClassName('mat-focus-indicator mat-menu-item ng-star-inserted')[document.getElementsByClassName('mat-focus-indicator mat-menu-item ng-star-inserted').length === 3 ? 2 : 1].click()
document.getElementsByClassName('confirm-button mat-focus-indicator mat-raised-button mat-button-base mat-warn')[0].click()
}, 1000)
只需在控制台上执行以下脚本,它将立即删除所有用户。
$('.edit-account-button').click();
$('.mat-menu-content button:last-child').click()
$('.fire-dialog-actions .confirm-button').click()
更新的 2021 年工作答案
const interval = setInterval(() => {
if ($('.edit-account-button').length === 0) {
clearInterval(interval)
} else {
$('.edit-account-button').first().click()
$('button:contains("Delete account")').click()
$('.confirm-button').click()
}
}, 1000)
https://gist.github.com/cupcakearmy/8c314b891e6958f26374c0205bac85e9
这可能对某些人有帮助。如果您有权访问 firebase 用户控制台 - 只需将页面保存为 html 并使用以下命令删除带有 node.js 的用户。需要设置 firebase-admin
let fs = require('fs'),
admin = require('firebase-admin'),
cheerio = require('cheerio');
// initialize firebase admin here
admin.initializeApp({
credential: admin.credential.cert('path/to/serviceAccountKey.json'),
databaseURL: 'https://<DATABASE_NAME>.firebaseio.com'
});
// use cheerio to load the html file you downloaded
$ = cheerio.load(fs.readFileSync('./yourfirebaseconsole.html'));
$('.a12n-user-uid .fb-table-cell-wrapper').each(function() {
admin.auth().deleteUser($(this).text());
}).then(() => {
console.log('successfully delete user');
}).catch((error) => {
console.log('error occurred ', error);
});
我建议使用浏览器在页面上运行一次 html 解析逻辑,只需运行它并确认结果中只显示用户 ID。就我而言,这返回了所有 UID
$('.a12n-user-uid .fb-table-cell-wrapper').each(function() {
console.log($(this).text());
});
我用过
var openMenuItemForFirstUser = function () {
const menuItem = $('[ng-click="controller.deleteUser()"]')
if (menuItem.size()) {
menuItem[0].classList.add("deletingThisUser")
menuItem[0].click();
setTimeout(deleteUser, 10, 0)
} else {
console.log("No users found...")
}
};
var deleteUser = function (t) {
const confirmButton = $('[ng-click="controller.submit()"]')
if (confirmButton.size()) {
console.log("deleting user")
confirmButton[0].click()
setTimeout(waitForDeletion, 10, 0)
} else {
if (t > 500) console.log("fail trying delete user")
else setTimeout(deleteUser, 10, parseInt(t) + 1)
}
}
var waitForDeletion = function (t) {
const deletingThisUser = $('.deletingThisUser')
if (deletingThisUser.size()) {
if (t > 500) console.log("fail wait for deletion")
else setTimeout(waitForDeletion, 10, parseInt(t) + 1)
} else {
setTimeout(openMenuItemForFirstUser, 10)
}
}
setTimeout(openMenuItemForFirstUser, 1000)
console.log("Deleting all users... Press F5 to cancel it")
我尝试了所有以前的方法,都没有奏效,所以我根据需要创建了一个:
setInterval(() => {
if ($('[aria-label="View more options"]').length > 0) {
$('[aria-label="View more options"]')[0].click()
if ($('[role="menuitem"]').length > 0) {
$('[role="menuitem"]')[1].click()
if ($('[class="confirm-button mat-focus-indicator mat-raised-button mat-button-base mat-warn"]').length > 0) {
$('[class="confirm-button mat-focus-indicator mat-raised-button mat-button-base mat-warn"]').click()
}
}
}
}, 500);
这是另一个可以用作书签的脚本。简单地
- 创建一个新书签 (Ctrl-D)
- 选择“更多”'
- 将脚本粘贴到 URL 输入中
然后,您只需单击书签即可删除所有用户。当没有更多用户或您离开时,脚本将停止。如果您不使用英文版,请*Text使用相应的文本更新变量。
javascript: (function () {
const deleteText = 'Delete account';
const deleteConfirmationText = 'Delete';
const editSelector = '.edit-account-button';
const deleteSelector = `button:contains(${deleteText})`;
const deleteConfirmationSelector = `button span:contains(${deleteConfirmationText})`;
const waitForAnimation = 350;
const deleteUsers = () => {
const editAccountButton = $(editSelector).first();
if (!editAccountButton.size()) {
console.log('Delete complete.');
return;
}
editAccountButton.first().click();
setTimeout(() => {
$(deleteSelector).first().click();
setTimeout(() => {
$(deleteConfirmationSelector).first().click();
setTimeout(() => {
deleteUsers();
}, waitForAnimation * 1.5);
}, waitForAnimation);
}, waitForAnimation);
};
deleteUsers();
})();
刚在这里工作 (2021)
var times = 0;
setInterval(() => {
$('.edit-account-button').first().click()
setTimeout(()=>{
$(".mat-button-wrapper:contains(Excluir)").click()
}, 200)
setTimeout(()=>{
$(".mat-menu-item:contains(Excluir)").click()
}, 200)
setTimeout(()=>{
$(".mat-button-wrapper:contains(Excluir)").click()
}, 200)
times++;
console.log(times)
if(times == 150) {
console.log("ATUALIZANDO!!!")
setTimeout(()=>{
$('[aria-label="Atualizar"]').click();
times = 0 ;
}, 200)
}
}, 1000);
每150次重新加载,你只需要放入250
在我的项目中,我将 firebaseAdmin 连接到模拟器进行测试。为了在每次测试之前清除数据库,我将listUsers函数与deleteUser函数结合起来。注意你可能不想为真实的数据库创建这种类型的功能。
我的代码看起来像这样。
import * as Bluebird from "bluebird"
function clearAllUsers() {
return Promise.resolve()
.then(() => admin.auth().listUsers())
.then((response) => response.users)
.then((users) =>
Bluebird.map(users, (user: { uid: string }) =>
admin.auth().deleteUser(user.uid),
),
);
}
但是你也可以使用 async await 并完成同样的事情
import * as Bluebird from "bluebird"
async function clearAllUsers() {
const usersResponse = await admin.auth().listUsers();
const users = usersResponse.users;
return await Bluebird.map(users, (user: { uid: string }) =>
admin.auth().deleteUser(user.uid),
);
}
此外,如果您碰巧使用模拟器进行测试,那么它的 UI 会附带一个全部删除按钮。见(这里)[https://imgur.com/3Xil86I]
简单删除所有用户 - ESM
// delete.ts or delete.mjs
import admin from 'firebase-admin';
import cred from './credentials.json';
let firebaseApp = admin.initializeApp({
credential: admin.credential.cert(cred as admin.ServiceAccount)
});
const listAllUsers = (nextPageToken?: string) => {
// List batch of users, 1000 at a time.
admin
.auth()
.listUsers(1000, nextPageToken)
.then(async (listUsersResult) => {
await admin.auth().deleteUsers(listUsersResult.users.map((u) => u.uid));
if (listUsersResult.pageToken) {
// List next batch of users.
listAllUsers(listUsersResult.pageToken);
}
})
.catch((error) => {
console.log('Error listing users:', error);
});
};
listAllUsers();
感谢 @abbas-ali 分享此脚本,它与最新的 firebase 版本完美配合(2021 年 7 月测试,有 600 多条记录 - 匿名和多联邦登录电子邮件地址)
setInterval(() => {
document.getElementsByClassName('edit-account-button mat-focus-indicator mat-menu-trigger mat-icon-button mat-button-base')[0].click()
if(document.getElementsByClassName('mat-focus-indicator mat-menu-item ng-star-inserted').length===3){
document.getElementsByClassName('mat-focus-indicator mat-menu-item ng-star-inserted')[2].click()
}else{
document.getElementsByClassName('mat-focus-indicator mat-menu-item ng-star-inserted')[1].click()
}
document.getElementsByClassName('confirm-button mat-focus-indicator mat-raised-button mat-button-base mat-warn')[0].click()
}, 1000)
这个作品现在 17.09.2021
转到您的 firebase 控制台,单击检查元素并选择控制台并将此代码粘贴到那里。
setInterval(() => {
document.getElementsByClassName('edit-account-button mat-focus-indicator mat-menu-trigger mat-icon-button mat-button-base')[0].click()
document.getElementsByClassName('mat-focus-indicator mat-menu-item ng-star-inserted')[2].click()
document.getElementsByClassName('confirm-button mat-focus-indicator mat-raised-button mat-button-base mat-warn')[0].click()
}, 1000)
对于身份验证类型:如果您通过邮件 ID 进行身份验证,请在第二行使用 [2],如果您使用移动身份验证,请在第二行使用1
工作于 2022/02/19
setInterval(() => {
$('[mattooltip="View more options"]').first().click()
setTimeout(()=>{
$(".mat-focus-indicator:contains(Delete)").click()
setTimeout(()=>{
$(".mat-warn").click()
}, 100)
}, 100)
}, 2000);
试试这个,在浏览器控制台中。我在 firebase 中找不到批量删除选项。所以我写了这个js。
var elements = [];
$('.a12n-users-table').find('tr').each(function(r){
$(this).find('td.table-row-actions').each(function(tds) {
$(this).find('button').each(function(x){
if($(this).attr('aria-label')=="Delete account"){
elements.push($(this));
}
});
});
});
var index = 0;
function deleteUser(){
index++;
elements[index].click();
$('.fb-dialog-actions').find('.md-warn').click();
setTimeout(deleteUser, 5000);
}
deleteUser();
