|
| 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 | +const functions = require('firebase-functions/v2'); |
| 19 | +const admin = require('firebase-admin'); |
| 20 | +admin.initializeApp(); |
| 21 | +const PromisePool = require('es6-promise-pool').default; |
| 22 | +// Maximum concurrent account deletions. |
| 23 | +const MAX_CONCURRENT = 3; |
| 24 | + |
| 25 | +/** |
| 26 | + * Run once a day at midnight, to cleanup the users |
| 27 | + * Manually run the task here https://console.cloud.google.com/cloudscheduler |
| 28 | + */ |
| 29 | +exports.accountcleanup = functions.scheduler.onSchedule('every day 00:00', async (event) => { |
| 30 | + // Fetch all user details. |
| 31 | + const inactiveUsers = await getInactiveUsers(); |
| 32 | + // Use a pool so that we delete maximum `MAX_CONCURRENT` users in parallel. |
| 33 | + const promisePool = new PromisePool(() => deleteInactiveUser(inactiveUsers), MAX_CONCURRENT); |
| 34 | + await promisePool.start(); |
| 35 | + functions.logger.log('User cleanup finished'); |
| 36 | +}); |
| 37 | + |
| 38 | +/** |
| 39 | + * Deletes one inactive user from the list. |
| 40 | + */ |
| 41 | +function deleteInactiveUser(inactiveUsers) { |
| 42 | + if (inactiveUsers.length > 0) { |
| 43 | + const userToDelete = inactiveUsers.pop(); |
| 44 | + |
| 45 | + // Delete the inactive user. |
| 46 | + return admin.auth().deleteUser(userToDelete.uid).then(() => { |
| 47 | + return functions.logger.log( |
| 48 | + 'Deleted user account', |
| 49 | + userToDelete.uid, |
| 50 | + 'because of inactivity' |
| 51 | + ); |
| 52 | + }).catch((error) => { |
| 53 | + return functions.logger.error( |
| 54 | + 'Deletion of inactive user account', |
| 55 | + userToDelete.uid, |
| 56 | + 'failed:', |
| 57 | + error |
| 58 | + ); |
| 59 | + }); |
| 60 | + } else { |
| 61 | + return null; |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * Returns the list of all inactive users. |
| 67 | + */ |
| 68 | +async function getInactiveUsers(users = [], nextPageToken) { |
| 69 | + const result = await admin.auth().listUsers(1000, nextPageToken); |
| 70 | + // Find users that have not signed in in the last 30 days. |
| 71 | + const inactiveUsers = result.users.filter( |
| 72 | + user => Date.parse(user.metadata.lastRefreshTime || user.metadata.lastSignInTime) < (Date.now() - 30 * 24 * 60 * 60 * 1000)); |
| 73 | + |
| 74 | + // Concat with list of previously found inactive users if there was more than 1000 users. |
| 75 | + users = users.concat(inactiveUsers); |
| 76 | + |
| 77 | + // If there are more users to fetch we fetch them. |
| 78 | + if (result.pageToken) { |
| 79 | + return getInactiveUsers(users, result.pageToken); |
| 80 | + } |
| 81 | + |
| 82 | + return users; |
| 83 | +} |
0 commit comments