|
| 1 | +/** |
| 2 | + * Copyright 2022 Google Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +'use strict'; |
| 17 | + |
| 18 | +// [START all] |
| 19 | +// [START import] |
| 20 | +// The Cloud Functions for Firebase SDK to create v2 Cloud Functions and set up triggers. |
| 21 | +const { onSchedule } = require('firebase-functions/v2/scheduler'); |
| 22 | +const { logger } = require('firebase-functions'); |
| 23 | + |
| 24 | +// The Firebase Admin SDK to delete inactive users. |
| 25 | +const admin = require('firebase-admin'); |
| 26 | +admin.initializeApp(); |
| 27 | + |
| 28 | +// The es6-promise-pool to limit the concurrency of promises. |
| 29 | +const PromisePool = require('es6-promise-pool').default; |
| 30 | +// Maximum concurrent account deletions. |
| 31 | +const MAX_CONCURRENT = 3; |
| 32 | +// [END import] |
| 33 | + |
| 34 | +// [START accountcleanup] |
| 35 | +// Run once a day at midnight, to clean up the users |
| 36 | +// Manually run the task here https://console.cloud.google.com/cloudscheduler |
| 37 | +exports.accountcleanup = onSchedule('every day 00:00', async (event) => { |
| 38 | + // Fetch all user details. |
| 39 | + const inactiveUsers = await getInactiveUsers(); |
| 40 | + // Use a pool so that we delete maximum `MAX_CONCURRENT` users in parallel. |
| 41 | + const promisePool = new PromisePool(() => deleteInactiveUser(inactiveUsers), MAX_CONCURRENT); |
| 42 | + await promisePool.start(); |
| 43 | + logger.log('User cleanup finished'); |
| 44 | +}); |
| 45 | +// [END accountcleanup] |
| 46 | + |
| 47 | +// [START deleteInactiveUser] |
| 48 | +// Deletes one inactive user from the list. |
| 49 | +function deleteInactiveUser(inactiveUsers) { |
| 50 | + if (inactiveUsers.length > 0) { |
| 51 | + const userToDelete = inactiveUsers.pop(); |
| 52 | + |
| 53 | + // Delete the inactive user. |
| 54 | + return admin.auth().deleteUser(userToDelete.uid).then(() => { |
| 55 | + return logger.log( |
| 56 | + 'Deleted user account', |
| 57 | + userToDelete.uid, |
| 58 | + 'because of inactivity' |
| 59 | + ); |
| 60 | + }).catch((error) => { |
| 61 | + return logger.error( |
| 62 | + 'Deletion of inactive user account', |
| 63 | + userToDelete.uid, |
| 64 | + 'failed:', |
| 65 | + error |
| 66 | + ); |
| 67 | + }); |
| 68 | + } else { |
| 69 | + return null; |
| 70 | + } |
| 71 | +} |
| 72 | +// [END deleteInactiveUser] |
| 73 | + |
| 74 | +// [START getInactiveUsers] |
| 75 | +// Returns the list of all inactive users. |
| 76 | +async function getInactiveUsers(users = [], nextPageToken) { |
| 77 | + const result = await admin.auth().listUsers(1000, nextPageToken); |
| 78 | + // Find users that have not signed in in the last 30 days. |
| 79 | + const inactiveUsers = result.users.filter( |
| 80 | + user => Date.parse(user.metadata.lastRefreshTime || user.metadata.lastSignInTime) < (Date.now() - 30 * 24 * 60 * 60 * 1000)); |
| 81 | + |
| 82 | + // Concat with list of previously found inactive users if there was more than 1000 users. |
| 83 | + users = users.concat(inactiveUsers); |
| 84 | + |
| 85 | + // If there are more users to fetch we fetch them. |
| 86 | + if (result.pageToken) { |
| 87 | + return getInactiveUsers(users, result.pageToken); |
| 88 | + } |
| 89 | + |
| 90 | + return users; |
| 91 | +} |
| 92 | +// [END getInactiveUsers] |
| 93 | +// [END all] |
0 commit comments