【发布时间】:2016-12-13 00:27:19
【问题描述】:
有没有一种简单的方法可以从 firebase 控制台中删除所有注册用户? 例如,我从我的开发环境中创建了一百个用户,现在我想将他们全部删除。
【问题讨论】:
标签: firebase firebase-authentication firebase-console
有没有一种简单的方法可以从 firebase 控制台中删除所有注册用户? 例如,我从我的开发环境中创建了一百个用户,现在我想将他们全部删除。
【问题讨论】:
标签: firebase firebase-authentication firebase-console
firebaser 在这里
我们刚刚发布了Firebase Admin SDK,它支持管理用例,例如deleting a user account without requiring that user to sign in first。
原始答案
Firebase 身份验证中目前没有 API 可以在不要求用户登录的情况下删除用户。我们知道这会限制我们 API 的可用性,并正在努力在未来的版本中添加此类功能。但与往常一样,我们没有提供该功能何时可用的具体时间表。
目前你唯一的解决办法是:
【讨论】:
admin.auth().deleteUser(uid);
因为我很懒惰点击 UI 中的按钮和元素,所以我设置了一个小的客户端脚本:
$('[aria-label="Delete account"]').click()
setTimeout(function () {
$(".md-raised:contains(Delete)").click()
}, 1000);
您可能需要多次运行它,但这比浪费时间手动点击屏幕上的东西要好得多。
【讨论】:
Inspect或Inspect Element),然后转到Console标签并粘贴代码并按return / 回车键。
略微增加了您的帮助脚本。
德国 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 个或更多用户。
【讨论】:
好吧,我用这个脚本在 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
【讨论】:
这可能对某些人有所帮助。 如果您有权访问 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());
});
【讨论】:
在更新的答案中,您现在可能可以使用 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)
只需在开发者工具中运行它
【讨论】:
俄文版
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)
【讨论】:
这是我的自行车:?
setInterval(() => {
$('[aria-label="Delete account"]').first().click()
setTimeout(()=>{
$(".md-raised:contains(Delete)").click()
}, 100)
}, 2000);
旨在避免经常调用 delete 端点,因为 google 失败并出现 404 错误。
【讨论】:
我用过
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")
【讨论】:
使用 Firebase Admin SDK 非常简单,并且是推荐对您的 Firebase 数据执行此类任务的方法。此解决方案不同于其他使用开发者控制台的临时解决方案。
我只是整理了一个 Node.js 脚本来删除您的 Firebase 身份验证中的所有用户。我已经通过删除约 10000 个用户对其进行了测试。我只是运行了以下 Node.js 代码。
创建一个新文件夹。在终端中运行以下命令
npm init
sudo npm install firebase-admin --save
现在在这个文件夹中创建一个index.js 文件。
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();
【讨论】:
一个对我有用的解决方案是创建一个单独的文件并导入我的 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函数。
希望这会有所帮助。
【讨论】:
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 中找不到批量删除选项。所以我写了这个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();
【讨论】:
法文版,
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 开发人员,并且“在工具开发人员中执行”对您来说毫无意义,这里是过程。
在控制台选项卡中,粘贴此代码并按 Enter。
如果语言与您粘贴的代码匹配,帐户将开始被删除。
【讨论】:
尝试使用此代码获取最新的 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)
【讨论】:
在 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="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);
【讨论】:
这是另一个可用作书签的脚本。简单的
然后您只需单击书签即可删除所有用户。当没有更多用户或您离开时,脚本将停止。如果您使用的不是英文版,请使用相应的文本更新 *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();
})();
【讨论】:
如果你有匿名账户
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 年)
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
【讨论】:
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
【讨论】:
在我的项目中,我将 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]
【讨论】:
// 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
【讨论】:
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);
【讨论】: