Skip to content

Instantly share code, notes, and snippets.

@beardsleym
Created October 8, 2022 20:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beardsleym/e11b69793af87fa0b321bb5eb01287ce to your computer and use it in GitHub Desktop.
Save beardsleym/e11b69793af87fa0b321bb5eb01287ce to your computer and use it in GitHub Desktop.
Disable firebase accounts that use a disposable email
const functions = require("firebase-functions");
const admin = require("firebase-admin");
const got = require("got");
const {
dnsLookupIpVersionToFamily,
} = require("got/dist/source/core/utils/dns-ip-version");
const runtimeOpts = {
timeoutSeconds: 10,
memory: "256MB",
};
const checkDisposableEmail = async (email) => {
try {
const response = await got(
`https://disposable.debounce.io/?email=${email}`,
{
headers: {
"user-agent": "website.com", //I think debounce.io requires this
},
retry: 2,
timeout: 3000,
}
).json();
const boolValue = response.disposable == "true"; //result contains boolean string
return boolValue; // returns true (if disposable email) or false
} catch (error) {
functions.logger.error("checkDisposableEmail error", error);
return false; // return false if error, so no false positives
}
};
//Create new user firestore document - cloud functions
exports.newUserCreated = functions
.runWith(runtimeOpts)
.auth.user()
.onCreate(async (event) => {
// If user signed up with mobile number
if (!event.email) {
// Skip else block
} else {
// Check if disposable email
const disposable = await checkDisposableEmail(event.email);
if (disposable) {
functions.logger.log(
"newUserCreated: disposable email used. Disabled ",
event.email,
{ uid: event.uid }
);
// Set user Auth to disabled.
await admin.auth().updateUser(event.uid, {
disabled: true,
});
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment