Created
February 12, 2022 05:59
-
-
Save ddikman/7dea60309a0799a45ebf50ef9e3d6dd9 to your computer and use it in GitHub Desktop.
Firebase function to list provider logins past month
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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