Firebase Auth · TypeScript
Block disposable email in Firebase Auth
Use a Firebase Cloud Function with the beforeUserCreated blocking trigger. Throw HttpsError to reject before Firebase persists the user.
The code
// functions/src/index.ts
import { beforeUserCreated, HttpsError } from "firebase-functions/v2/identity";
import { defineSecret } from "firebase-functions/params";
const piisendKey = defineSecret("PIISEND_API_KEY");
const CHECK_URL = "https://api.piisend.com/api/v1/check/email";
export const blockDisposable = beforeUserCreated(
{ secrets: [piisendKey] },
async (event) => {
const email = event.data.email;
if (!email) return;
try {
const r = await fetch(`${CHECK_URL}/${encodeURIComponent(email)}`, {
headers: {
Authorization: `Bearer ${piisendKey.value()}`,
},
});
// reject on 'block' — catches disposable, spam, no-MX and typo'd addresses
if (r.ok && (await r.json()).recommendation === 'block') {
throw new HttpsError("invalid-argument", "Please use a permanent email address.");
}
} catch (e) {
if (e instanceof HttpsError) throw e;
// fail open on network errors
}
},
);API key
Store `PIISEND_API_KEY` in Firebase Secret Manager via `defineSecret`.
Fail-open vs fail-closed
Only re-throw HttpsError for confirmed disposable hits.
Pair with verification
Enable blocking functions in Firebase console and deploy to your auth region.
FAQ
Where does the Piisend API key go in Firebase Auth?
Store `PIISEND_API_KEY` in Firebase Secret Manager via `defineSecret`.
What happens if Piisend is slow or down?
Only re-throw HttpsError for confirmed disposable hits.
How does this fit with the rest of Firebase Auth signup?
Enable blocking functions in Firebase console and deploy to your auth region.
What does this snippet block?
Addresses on disposable domains: about 10,900 listed domains, plus new ones whose mail servers belong to a known throwaway service. Free providers such as Gmail and forwarding aliases such as duck.com pass.
Can I get a full allow / review / block verdict instead?
Yes, and we recommend it. Call GET /api/v1/check/email/{email} instead. Both endpoints need an API key with the check:read scope and use one check from your monthly allowance per call, but the check endpoint also returns forwarding relays, phishing lists, missing MX records, new domains, typo suggestions and an allow / review / block verdict.
Send verification email with Piisend
After blocking disposable addresses, send OTP and verification mail through the same Piisend account. One API key for intelligence checks and transactional sends.