Created
May 26, 2018 14:14
-
-
Save leolabs/94a4d27844dd58dae7e452b7b5185eb4 to your computer and use it in GitHub Desktop.
Update Festify User Claims
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 firebase from 'firebase-admin'; | |
import 'firebase-functions'; // Import to initialize FIREBASE_CONFIG 🙄 | |
import retry from 'promise-retry'; | |
// const config = JSON.parse(process.env.FIREBASE_CONFIG!); | |
// tslint:disable-next-line:no-var-requires | |
const serviceAccount = require('./service-account-2.json'); | |
firebase.initializeApp({ | |
databaseURL: "https://festify-79b08.firebaseio.com", | |
credential: firebase.credential.cert(serviceAccount), | |
}); | |
function listAllUsers(nextPageToken) { | |
// List batch of users, 1000 at a time. | |
firebase.auth().listUsers(1000, nextPageToken) | |
.then(async listUsersResult => { | |
const promises = listUsersResult.users | |
.filter(u => u.uid.indexOf("spotify:user:") !== -1) | |
.filter(u => | |
u.customClaims && | |
u.customClaims['spotify'] && | |
u.customClaims['spotify'].indexOf("%") !== -1); | |
console.log("Processing", promises.length, "|", Date.now()); | |
for (const u of promises) { | |
try { | |
await retry(async retry => { | |
try { | |
await firebase.auth().setCustomUserClaims(u.uid, { | |
spotify: decodeURIComponent(u.customClaims!['spotify']), | |
}); | |
} catch (e) { | |
retry(e); | |
} | |
}, { | |
maxTimeout: 25000, | |
randomize: true, | |
}); | |
console.log("Processed", u.uid); | |
} catch (e) { | |
console.error(e.message); | |
} | |
} | |
if (listUsersResult.pageToken) { | |
// List next batch of users. | |
listAllUsers(listUsersResult.pageToken); | |
} else { | |
console.log("Done"); | |
} | |
}) | |
.catch(error => { | |
console.log("Error listing users:", error); | |
}); | |
} | |
// Start listing users from the beginning, 1000 at a time. | |
listAllUsers(undefined); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment