zerobyte/app/lib/auth-middlewares/only-one-user.ts
Nico 99932a8522
refactor: better-auth (#319)
* refactor: better-auth

* chore: pr feedback

* chore: lower + trim usernames in db
2026-01-07 22:36:20 +01:00

17 lines
512 B
TypeScript

import { db } from "~/server/db/db";
import type { AuthMiddlewareContext } from "../auth";
import { logger } from "~/server/utils/logger";
export const ensureOnlyOneUser = async (ctx: AuthMiddlewareContext) => {
const { path } = ctx;
if (path !== "/sign-up/email") {
return;
}
const existingUser = await db.query.usersTable.findFirst();
if (existingUser) {
logger.error("Attempt to create a second administrator account blocked.");
throw new Error("An administrator account already exists");
}
};