如何从NodeJS Admin SDK中的Firebase身份验证中删除上次登录超过特定时间的匿名用户? [英] how to delete anonymous users that last signed in more than certain time from Firebase Authentication in NodeJS Admin SDK?

查看:45
本文介绍了如何从NodeJS Admin SDK中的Firebase身份验证中删除上次登录超过特定时间的匿名用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从Firebase身份验证中,我们有这样的表,提供者可以是电子邮件,谷歌,facebook或匿名

from Firebase Authentication, we have table like this, the providers can be email, google, facebook or anonymous

我需要删除最近一次登录的六个月前的所有匿名帐户.我需要查询那些匿名帐户,然后将其全部删除.

I need to delete all anonymous accounts that last signed in was more than six months ago. I need to query those anonymous accounts and then delete them all.

但是我真的不知道如何使用Node JS admin SDK查询六个月前最后一次登录的所有匿名帐户.是否可以?该怎么做?

but I really have no idea how to query all anonymous account that last signed in was more than six months ago using Node JS admin SDK. is it possible? how to do that?

因为从中的文档中的Firebase(1亿个匿名帐户)存在限制这里.我可能未达到该限制,但是我认为如果可以通过在云功能中使用云调度程序创建cron作业来清理未使用的匿名帐户,那会更好

because there is a limit from firebase (100 million anonymous account) from the documentation in here. I may not hit that limit, but I think it is better If I can clean unused anonymous accounts by creating a cron job using cloud scheduler in cloud function

推荐答案

我想找到了解决方案,建议您阅读此Firebase

I think I find the solution, I suggest you to read this Firebase official documentation first, to know how to get all users from authentication, there is an explanation there that you need to read. there is no something like query to get data we need, at least right now

我将使用Typescript和async/await而不是then/catch和Javascript.但是,如果您使用的是javascript,则只需稍微修改一下函数参数

I will use Typescript and async/await instead of then/catch and Javascript. but if you use javascript, then you just need to modify the function parameter a little bit

import * as moment from "moment";
const auth = admin.auth();

export const deleteUnusedAnonymousUsers = async function(nextPageToken?: string) {

    // this is a recursive function

    try {

        // get accounts in batches, because the maximum number of users allowed to be listed at a time is 1000
        const listUsersResult = await auth.listUsers(1000, nextPageToken);

        const anonymousUsers = listUsersResult.users.filter((userRecord) => {
            return userRecord.providerData.length == 0;
        });

        const sixMonthAgo = moment().subtract(6, "months").toDate();

        const anonymousThatSignedInMoreThanSixMonthAgo = anonymousUsers.filter((userRecord) => {
            const lastSignInDate = new Date(userRecord.metadata.lastSignInTime);
            return moment(lastSignInDate).isBefore(sixMonthAgo);
        });

        const userUIDs = anonymousThatSignedInMoreThanSixMonthAgo.map((userRecord) => userRecord.uid);

        await auth.deleteUsers(userUIDs);

        if (listUsersResult.pageToken) {
            // List next batch of users.
            deleteUnusedAnonymousUsers(listUsersResult.pageToken);
        }

    } catch (error) {
        console.log(error);
    }

};

用法

deleteUnusedAnonymousUsers();

这篇关于如何从NodeJS Admin SDK中的Firebase身份验证中删除上次登录超过特定时间的匿名用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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