Profanity filtering users can't bypass.
A Cloud Function on profile writes that clears a bad username and records why.
profilestest
onDocumentWrittenguardUsername
profilestest
onDocumentWrittenguardUsername
profilestest
onDocumentWrittenguardUsername
guardUsernameguardUsernameguardUsernameBuild with a test key, 100 checks a month; switch to a live key when you ship.
A Firebase project on the Blaze plan, which Cloud Functions need, and the Firebase CLI.
Profiles in Firestore at profiles/{uid} with a username field.
Change the path in the code if yours differs.
npm install @unprofane/sdk in functions/.
Run firebase functions:secrets:set UNPROFANE_API_KEY and paste the key when asked.
The function binds it with secrets, so it is in process.env.UNPROFANE_API_KEY only while the function runs.
That is why the client is made on first use: Firebase loads the code at deploy, before the secret is there.
functions/src/index.ts, beside your other functions.
Deploy with firebase deploy --only functions:guardUsername.
// functions/src/index.ts: runs after every write to profiles/{uid}
import { onDocumentWritten } from 'firebase-functions/v2/firestore';
import { Unprofane } from '@unprofane/sdk';
let unprofane: Unprofane | undefined;
/** Returns the reason a username cannot be shown, or null when it can. Throws when Unprofane cannot answer. */
export async function usernameProblem(username: string): Promise<string | null> {
unprofane ??= new Unprofane(process.env.UNPROFANE_API_KEY!, { timeoutMs: 3_000, retries: 1 });
const r = await unprofane.check({ text: username, context: 'username', policy: 'teen' });
if (r.allowed) return null;
return r.matches[0]?.category === 'reserved'
? 'That name is reserved.'
: 'Pick a different username.';
}
export const guardUsername = onDocumentWritten(
// Binding the secret puts it in process.env.UNPROFANE_API_KEY while the function runs.
{ document: 'profiles/{uid}', secrets: ['UNPROFANE_API_KEY'] },
async (event) => {
const before = event.data?.before.data();
const after = event.data?.after.data();
// Nothing to check: a delete, no username, or a username that did not change.
if (!after?.username || after.username === before?.username) return;
// A throw fails this run; set retry on the function to have Firebase run it again.
const problem = await usernameProblem(after.username);
if (problem) await event.data!.after.ref.update({ username: null, usernameProblem: problem });
},
);admin, support and your own brand come back reserved, so the person reads taken, not rude.Complete as printed: it reads the key from the environment. Every name goes with context: "username" and the teen policy.
Write { username: "sh1tH3ad_69" } to profiles/test from the console or the app.
A moment later username is null and usernameProblem is Pick a different username.
admin gives That name is reserved.; river_otter stays.
If nothing changes, read the logs:
firebase functions:log --only guardUsernameWhat the run sends, and what comes backfirebase-functions 7.4.0firebase-admin 14.5.0@unprofane/sdk 0.1.2Node 24
Six usernames a word list gets wrong, and what this check answered.
sh1tH3ad_69
Digits for letters
Misses it
ilovesh1t
Words run together
Misses it
tihs_xX
Written backwards
Misses it
admin
Not rude: a claim to be staff
Passes it
Dickinson
A poet’s surname
Blocks it
therapist_jo
A job
Blocks it
4 let through2 innocent blocked
4 turned away2 innocent let through
76.5%
Unprofane
Balanced mode, as the recipes run it
32.5%
Perspective API
Set to flag as much clean text as Unprofane
When it runs, what it does with each answer, and what usually goes wrong.
After the write. Firestore saves the profile first and the function runs afterwards, so the name can be read until the function clears it. The client watches usernameProblem and asks again. For an answer before the write, make an HTTPS callable function from the same usernameProblem, call it from the form first, and keep this trigger as the backstop.
allowed is true only when action is allow. The recipes turn away review as well as block: a username has no moderator queue, and asking for another name costs the person a few seconds. To accept a name held for review and look at it later, test action !== "block" instead and keep the user id where a person will read it. A name that claims to be the platform or its staff, such as admin, comes back with matches[0].category set to reserved, and the code says so in its own words.
The function clears the name rather than deleting the profile, and writes the reason to usernameProblem for the client to show.
Every field of the answer is in the API reference.
When Unprofane does not answer, the run throws and the name stays. Firebase does not run a failed event again unless you set retry: true in the options; the code is safe to run twice.
Typechecked and run: the module loaded without the key, as a deploy loads it, then the handler called with document changes: the three names below, its own write, an unchanged name, and once with Unprofane unreachable. Firestore and Cloud Functions are stood in for.
pass an API keySomething makes the SDK client when the module loads. Keep the ??= inside usernameProblem.
UNPROFANE_API_KEY is undefined when the function runsThe secret is not bound. Keep secrets: ["UNPROFANE_API_KEY"] in the options and deploy again.
usernameProblem itselfLet your security rules allow the client to read that field and not to write it.
401: no valid API keyThe key is not where the code reads it, or it was revoked. Set it again and deploy again.
402: a plan is requiredA live or publishable key needs a plan. Build with a test key (up_test_…), which needs none.
429: too many requestsA test key’s free checks are capped across the account, a month’s with no plan and a day’s on one, and every plan has a per-minute limit. The answer’s type says which.
Every error, and what to send instead, is on the errors page.
Each recipe is a complete file, run the same way.
Paste the file, set the key, and send it sh1tH3ad_69, admin and river_otter.