Last active
July 28, 2021 07:05
-
-
Save JeremyEnglert/0864222351bed7bfe9aa382b553e0efd to your computer and use it in GitHub Desktop.
This file contains 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
var admin = require("firebase-admin"); | |
const firebase = require("firebase"); | |
var serviceAccount = require("./showzone-firebase-service-account.json"); | |
// Initialize Firebase connection | |
admin.initializeApp({ | |
credential: admin.credential.cert(serviceAccount), | |
databaseURL: "https://showzone-cloud.firebaseio.com", | |
}); | |
// Ask user their ShowZone email address | |
let userEmail = "jeremy@showzone.io"; | |
let uid = "nsfoccGeuuQNCeXwsJHPxkYXdcx1"; | |
const checkEmailAddress = () => { | |
admin | |
.auth() | |
.getUserByEmail(userEmail) | |
.then((userRecord) => { | |
console.log(`ShowZone Membership ID: ${userRecord.uid}`); | |
}) | |
// If userEmail doesn't match a record in Firebase | |
.catch((error) => { | |
console.log( | |
"This email could not be found. Try again or contact team@showzone.io if you continue to have issues." | |
); | |
}); | |
}; | |
// checkEmailAddress(); | |
// A user may exist, but we need to make sure they are a Pro/paying member | |
// This function may render the above useless, as you can probably just run this check instead | |
const checkIfPro = () => { | |
admin | |
.auth() | |
.getUserByEmail(userEmail) | |
.then((userRecord) => { | |
if (userRecord.customClaims.stripeRole) { | |
console.log( | |
"This is a pro user. Ask them for their ShowZone membership ID." | |
); | |
console.log(`The answer should be ${userRecord.uid}`); | |
} | |
}) | |
// If userEmail doesn't match a record in Firebase | |
.catch((error) => { | |
console.log( | |
"This email could not be found or you need to upgrade your account (https://showzone.io/pro). Try again or contact team@showzone.io if you continue to have issues." | |
); | |
}); | |
}; | |
// checkIfPro(); | |
// Once we are sure a user is a Pro subscriber, | |
// we add meta that shows they have been verified in Discord. | |
// Note: We need to collect the users Discord username for this (assuming the bot can get thise) | |
const addUserMetaForDiscord = () => { | |
admin | |
.auth() | |
.getUserByEmail(userEmail) | |
.then(({ customClaims: oldClaims }) => { | |
admin.auth().setCustomUserClaims(uid, { ...oldClaims, discord: "DiscordUsername" }); // Replace "DiscordUsername" with users actual Discord username | |
}) | |
// If userEmail doesn't match a record in Firebase | |
.catch((error) => { | |
console.log(error); | |
}); | |
}; | |
// addUserMetaForDiscord(); | |
// This runs through all of the users in Firebase | |
// But we slim that list down a bit by only checking users with claimRoles | |
// If a user doesn't have a stripe customClaim, but does have a Discord customClaim, we need to remove the "Pro" role from Discord | |
let cancelledUsers = []; | |
const listAllUsers = (nextPageToken) => { | |
// List batch of users, 1000 at a time. | |
admin | |
.auth() | |
.listUsers(1000, nextPageToken) | |
.then((listUsersResult) => { | |
listUsersResult.users.forEach((userRecord) => { | |
// We only need to check users that have customClaims | |
if (userRecord.customClaims) { | |
// If a user has a stripeRole of null, they've cancelled their account | |
if ((userRecord.customClaims.stripeRole == null || userRecord.customClaims.stripeRole == undefined) && userRecord.customClaims.discord) { | |
cancelledUsers.push(userRecord.customClaims.discord); | |
} | |
} | |
}); | |
if (listUsersResult.pageToken) { | |
// List next batch of users. | |
listAllUsers(listUsersResult.pageToken); | |
} | |
}) | |
.then (() => { | |
console.log("Remove 'Pro' role from these users:", cancelledUsers); | |
}) | |
.catch((error) => { | |
console.log("Error listing users:", error); | |
}); | |
}; | |
listAllUsers(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment