@@ -40,32 +40,32 @@ exports.accountcleanup = functions.https().onRequest((req, res) => {
4040 if ( key !== functions . env . cron . key ) {
4141 console . log ( 'The key provided in the request does not match the key set in the environment. Check that' , key ,
4242 'matches the cron.key attribute in `firebase env:get`' ) ;
43- return res . status ( 403 ) . send ( 'Security key does not match. Make sure your "key" URL query parameter matches the ' +
43+ res . status ( 403 ) . send ( 'Security key does not match. Make sure your "key" URL query parameter matches the ' +
4444 'cron.key environment variable.' ) ;
45+ return ;
4546 }
4647
47- // We'll fetch all user details.
48+ // Fetch all user details.
4849 getUsers ( ) . then ( users => {
49- // We'll use a pool so that we delete maximum `MAX_CONCURRENT` users in parallel.
50+ // Find users that have not signed in in the last 30 days.
51+ const inactiveUsers = users . filter (
52+ user => parseInt ( user . lastLoginAt , 10 ) > Date . now ( ) - 30 * 24 * 60 * 60 * 1000 ) ;
53+
54+ // Use a pool so that we delete maximum `MAX_CONCURRENT` users in parallel.
5055 const promisePool = new PromisePool ( ( ) => {
51- let user ;
52- // We search for users that have not signed in in the last 30 days.
53- while ( ! user || parseInt ( user . lastLoginAt ) > Date . now ( ) - 30 * 24 * 60 * 60 * 1000 ) {
54- if ( users . length === 0 ) {
55- return null ;
56- }
57- user = users . pop ( ) ;
58- }
56+ if ( inactiveUsers . length > 0 ) {
57+ const userToDelete = inactiveUsers . pop ( ) ;
5958
60- // If an inactive user is found we delete it.
61- return firebaseAdmin . auth ( ) . deleteUser ( user . uid ) . then ( ( ) => {
62- console . log ( 'Deleted user account' , user . uid , 'because of inactivity' ) ;
63- } ) . catch ( error => {
64- console . error ( 'Deletion of inactive user account' , user . uid , 'failed:' , error ) ;
65- } ) ;
59+ // Delete the inactive user.
60+ return firebaseAdmin . auth ( ) . deleteUser ( userToDelete . uid ) . then ( ( ) => {
61+ console . log ( 'Deleted user account' , userToDelete . uid , 'because of inactivity' ) ;
62+ } ) . catch ( error => {
63+ console . error ( 'Deletion of inactive user account' , userToDelete . uid , 'failed:' , error ) ;
64+ } ) ;
65+ }
6666 } , MAX_CONCURRENT ) ;
6767
68- return promisePool . start ( ) . then ( ( ) => {
68+ promisePool . start ( ) . then ( ( ) => {
6969 console . log ( 'User cleanup finished' ) ;
7070 res . send ( 'User cleanup finished' ) ;
7171 } ) ;
0 commit comments