Profanity filtering users can't bypass.
A before-create and before-update hook on the user table.
Create your account
you@example.com
Name
sh1tH3ad_69
Pick a different username.
Create your account
you@example.com
Name
admin
That name is reserved.
Create your account
you@example.com
Name
river_otter
Account created
Build with a test key, 100 checks a month; switch to a live key when you ship.
A Better Auth server with its database set up.
npm install @unprofane/sdkPut UNPROFANE_API_KEY in the server’s environment, beside BETTER_AUTH_SECRET.
auth.ts, the file that calls betterAuth.
Add databaseHooks to your options rather than replacing them.
// auth.ts: Better Auth database hooks on the user table
import { betterAuth } from 'better-auth';
import { APIError } from 'better-auth/api';
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.';
}
/** Refuses the write with a 400 whose message the client shows. */
async function guard(name: unknown): Promise<void> {
if (typeof name !== 'string' || !name) return;
const problem = await usernameProblem(name);
if (problem) throw new APIError('BAD_REQUEST', { message: problem });
}
export const auth = betterAuth({
// ...your database, email and social settings
databaseHooks: {
user: {
create: {
before: async (user) => {
await guard(user.name);
return { data: user };
},
},
update: {
before: async (data) => {
await guard(data.name);
return { data };
},
},
},
},
});admin, support and your own brand come back reserved, so the person reads taken, not rude.400 whose message Better Auth hands to the client as it is.Complete as printed: it reads the key from the environment. Every name goes with context: "username" and the teen policy.
Sign up with the name sh1tH3ad_69: the client receives a 400 with Pick a different username., and no user row is written.
Change a user’s name to admin: That name is reserved. river_otter goes through.
What the run sends, and what comes backbetter-auth 1.7.7@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.
Before the write, in the same request: the hooks run before Better Auth creates a user or changes one, and a refused name is never saved. The code checks name; with the username plugin, call guard on username too.
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.
A refusal throws APIError with status 400 and the reason as its message, which Better Auth returns to the client.
Every field of the answer is in the API reference.
When Unprofane does not answer, the hook throws an error that is not an APIError, so the sign-up or the change fails and nothing is written. To let it through instead, catch the error in guard and return.
Typechecked and run: both hooks called as Better Auth calls them, with the three names below, an update that leaves the name alone, and once with Unprofane unreachable. No database or HTTP request is involved.
Throw APIError from better-auth/api with the reason as message, as the code does.
Call guard(user.username) in the create hook and guard(data.username) in the update hook.
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.