zerobyte/app/server/lib/auth-middlewares/only-one-user.ts
Nico 1017f1a38b
feat: edit repository form (#507)
* feat: edit repository form

* refactor: local repo path concat as a code migration

* refactor: server constants

* chore: fix lint issue in test file

* refactor: add auth to getServerConstants
2026-02-14 11:49:33 +01:00

23 lines
836 B
TypeScript

import { db } from "~/server/db/db";
import type { AuthMiddlewareContext } from "../auth";
import { logger } from "~/server/utils/logger";
import { ForbiddenError } from "http-errors-enhanced";
import { REGISTRATION_ENABLED_KEY } from "~/server/core/constants";
export const ensureOnlyOneUser = async (ctx: AuthMiddlewareContext) => {
const { path } = ctx;
const existingUser = await db.query.usersTable.findFirst();
if (path !== "/sign-up/email") {
return;
}
const result = await db.query.appMetadataTable.findFirst({
where: { key: REGISTRATION_ENABLED_KEY },
});
if (result?.value !== "true" && existingUser) {
logger.info("User registration attempt blocked: registrations are not enabled.");
throw new ForbiddenError("User registrations are currently disabled. Please contact an administrator for access.");
}
};