Skip to content

Instantly share code, notes, and snippets.

@ddikman
Created Feb 12, 2022
Embed
What would you like to do?
Firebase function to list provider logins past month
import * as functions from "firebase-functions"
import * as admin from "firebase-admin"
const getUsers = async (pageToken?: string) : Promise<admin.auth.UserRecord[]> => {
const result = await admin.auth().listUsers(1000, pageToken)
if (result.pageToken) {
return [...result.users, ...await getUsers(result.pageToken)]
}
return result.users
}
const publish = functions.https.onRequest(async (req, res) => {
const users = await getUsers()
const logins = users.map(user => {
return { lastSignIn: new Date(Date.parse(user.metadata.lastSignInTime)), providerId: user.providerData[0].providerId }
})
const providers = Array.from(new Set(logins.map(login => login.providerId)))
const oneMonthAgo = new Date()
oneMonthAgo.setMonth(oneMonthAgo.getMonth() - 1)
const providerLogins = providers.map(provider => {
return { provider, pastMonth: logins.filter(login => login.providerId === provider && login.lastSignIn >= oneMonthAgo).length }
});
const data = { providerLogins }
res.send(data)
res.end()
})
export = publish;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment