-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from uwblueprint/sync-firebase-mongodb-script
add script to delete users that are in only 1 of firebase or mongodb
- Loading branch information
Showing
2 changed files
with
103 additions
and
1 deletion.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import * as firebaseAdmin from "firebase-admin"; | ||
import dotenv from "dotenv"; | ||
import MgUser, { User } from "../models/user.mgmodel"; | ||
import logger from "../utilities/logger"; | ||
import { getErrorMessage } from "../utilities/errorUtils"; | ||
import { mongo } from "../models"; | ||
|
||
dotenv.config(); | ||
|
||
const Logger = logger(__filename); | ||
|
||
firebaseAdmin.initializeApp({ | ||
credential: firebaseAdmin.credential.cert({ | ||
projectId: process.env.FIREBASE_PROJECT_ID, | ||
privateKey: process.env.FIREBASE_SVC_ACCOUNT_PRIVATE_KEY?.replace( | ||
/\\n/g, | ||
"\n", | ||
), | ||
clientEmail: process.env.FIREBASE_SVC_ACCOUNT_CLIENT_EMAIL, | ||
}), | ||
}); | ||
|
||
mongo.connect(); | ||
|
||
async function getAllFirebaseUsers( | ||
nextPageToken?: string, | ||
): Promise<firebaseAdmin.auth.UserRecord[]> { | ||
const listUsersResult = await firebaseAdmin | ||
.auth() | ||
.listUsers(1000, nextPageToken); | ||
const { users } = listUsersResult; | ||
|
||
// If there are more users to fetch, recursively fetch them. | ||
if (listUsersResult.pageToken) { | ||
const moreUsers = await getAllFirebaseUsers(listUsersResult.pageToken); | ||
return users.concat(moreUsers); | ||
} | ||
return users; | ||
} | ||
|
||
async function getUsersNotInMongoDB( | ||
allFirebaseUsers: firebaseAdmin.auth.UserRecord[], | ||
allMongoDBUsers: User[], | ||
): Promise<string[]> { | ||
// We are checking Firebase for all the MongoDB users not in there | ||
|
||
const allFirebaseUserIds = allFirebaseUsers.map((user) => user.uid); | ||
const allMongoDBUserIds = new Set(allMongoDBUsers.map((user) => user.authId)); | ||
|
||
// put in the ! to not delete the wrong people | ||
return allFirebaseUserIds.filter((id) => !allMongoDBUserIds.has(id)); | ||
} | ||
|
||
async function getUsersNotInFirebase( | ||
allFirebaseUsers: firebaseAdmin.auth.UserRecord[], | ||
allMongoDBUsers: User[], | ||
) { | ||
// We are checking MongoDB for all the Firebase users not in there | ||
const allFirebaseUserIds = new Set(allFirebaseUsers.map((user) => user.uid)); | ||
const allMongoDBUserIds = allMongoDBUsers.map((user) => user.authId); | ||
// put in the ! to not delete the wrong people | ||
return allMongoDBUserIds.filter((id) => !allFirebaseUserIds.has(id)); | ||
} | ||
|
||
async function main() { | ||
try { | ||
const allFirebaseUsers = await getAllFirebaseUsers(); | ||
Logger.info(`Found ${allFirebaseUsers.length} firebase users`); | ||
|
||
const allMongoDBUsers = await MgUser.find(); | ||
Logger.info(`Found ${allMongoDBUsers.length} MongoDB users`); | ||
|
||
const removeMongoDBUsers = await getUsersNotInMongoDB( | ||
allFirebaseUsers, | ||
allMongoDBUsers, | ||
); | ||
const removeFirebaseUsers = await getUsersNotInFirebase( | ||
allFirebaseUsers, | ||
allMongoDBUsers, | ||
); | ||
|
||
// Comment if you dont actually want to delete | ||
|
||
removeMongoDBUsers.forEach(async (id) => { | ||
await MgUser.deleteOne({ authId: id }); | ||
}); | ||
|
||
removeFirebaseUsers.forEach(async (id) => { | ||
await firebaseAdmin.auth().deleteUser(id); | ||
}); | ||
Logger.info( | ||
`Deleted ${removeMongoDBUsers.length} MongoDB users and ${removeFirebaseUsers.length} Firebase users`, | ||
); | ||
process.exit(); | ||
} catch (error: unknown) { | ||
Logger.error(getErrorMessage(error)); | ||
throw error; | ||
} | ||
} | ||
|
||
main(); |