Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.
TypeScript i18nextnext-authnextjspostgresqlprismareact-emailt3-stacktailwindcsstrpcturborepotypescriptzod
🔒️ Enforce the registration setting where every account is created (#3239)
Luke Vella10a3b6f1b057
diff --git a/apps/web/src/features/instance-settings/data.ts b/apps/web/src/features/instance-settings/data.tsindex 9268df023..1cfafdb16 100644--- a/apps/web/src/features/instance-settings/data.ts+++ b/apps/web/src/features/instance-settings/data.ts@@ -73,3 +73,18 @@ export async function getRegistrationEnabled() {return !instanceSettings.disableUserRegistration;}++// Uncached twin of getRegistrationEnabled for the auth hooks in lib/auth.ts:+// an authorization decision must see the control panel toggle the moment it+// is written, not whenever the page cache revalidates.+export async function isRegistrationOpen() {+ if (!isFeatureEnabled("registration")) {+ return false;+ }+ const instanceSettings = await prisma.instanceSettings.findUnique({+ where: { id: 1 },+ select: { disableUserRegistration: true },+ });++ return !instanceSettings?.disableUserRegistration;+}diff --git a/apps/web/src/lib/auth.ts b/apps/web/src/lib/auth.tsindex 3a989fab3..46e618977 100644--- a/apps/web/src/lib/auth.ts+++ b/apps/web/src/lib/auth.ts@@ -31,6 +31,7 @@ import {isTemporaryEmail,} from "@/features/auth/utils";import { getStripe } from "@/features/billing/service";+import { isRegistrationOpen } from "@/features/instance-settings/data";import type { UserDTO } from "@/features/user/schema";import { jobTitleFieldSchema } from "@/features/user/schema";import { getTranslation } from "@/i18n/server";@@ -199,10 +200,10 @@ export const authLib = betterAuth({storeInDatabase: true,}),emailOTP({- // Sign-up via OTP is allowed so that registering for an event can either- // log a guest into their existing account or create one for a new email.- // The login page is unaffected: it sends "email-verification" OTPs and- // verifies with verifyEmail, neither of which is gated by this flag.+ // The "sign-in" OTP type creates an account for an unknown address; the+ // login page offers it only while registration is on. The control panel+ // toggle changes at runtime, so the registration setting is enforced in+ // the user.create hook rather than by this boot-time flag.disableSignUp: false,expiresIn: 15 * 60,resendStrategy: "reuse",@@ -527,14 +528,24 @@ export const authLib = betterAuth({// a later release. Single-tenant Microsoft is the instance's own// directory; OIDC likewise; Google always asserts email_verified.before: async (user, ctx) => {+ if (user.isAnonymous) {+ return;+ }+ // Registration policy: every path that mints an account (OTP+ // sign-in, social and OIDC callbacks, the ID-token variant of+ // /sign-in/social) converges here, so this is where "registration+ // is off" is enforced. Same shape better-auth documents for+ // SIGNUP_DISABLED.+ if (!(await isRegistrationOpen())) {+ throw new APIError("BAD_REQUEST", {+ code: "SIGNUP_DISABLED",+ message: "Signup is disabled",+ });+ }// The redirect callback names the provider in the route; the// ID-token variant of /sign-in/social names it in the body.const provider = ctx?.params?.id ?? ctx?.body?.provider;- if (- user.isAnonymous ||- user.emailVerified ||- provider !== "microsoft"- ) {+ if (user.emailVerified || provider !== "microsoft") {return;}// better-auth derives emailVerified from the same claims, so andiff --git a/apps/web/tests/otp-sign-up.spec.ts b/apps/web/tests/otp-sign-up.spec.tsnew file mode 100644index 000000000..423ab2553--- /dev/null+++ b/apps/web/tests/otp-sign-up.spec.ts@@ -0,0 +1,81 @@+import { expect, test } from "@playwright/test";+import { prisma } from "@rallly/database";+import { deleteAllMessages, getCode } from "@rallly/test-helpers";+import { createUserInDb } from "./test-utils";++const unknownEmail = "[email protected]";+const existingEmail = "[email protected]";++// The "sign-in" OTP type creates an account for an unknown address. The+// registration setting is enforced where the account would be created, so a+// valid code for an unknown address must still end without a user.+test.describe.serial(() => {+ test.beforeAll(async () => {+ await prisma.instanceSettings.update({+ where: { id: 1 },+ data: { disableUserRegistration: true },+ });+ await prisma.verification.deleteMany({+ where: { identifier: { endsWith: unknownEmail } },+ });+ });++ test.beforeEach(async () => {+ await deleteAllMessages();+ });++ test.afterAll(async () => {+ await prisma.instanceSettings.update({+ where: { id: 1 },+ data: { disableUserRegistration: false },+ });+ await prisma.user.deleteMany({+ where: { email: { in: [unknownEmail, existingEmail] } },+ });+ await prisma.verification.deleteMany({+ where: { identifier: { endsWith: unknownEmail } },+ });+ });++ test("a verified code for an unknown address creates no account", async ({+ request,+ }) => {+ const send = await request.post(+ "/api/better-auth/email-otp/send-verification-otp",+ { data: { email: unknownEmail, type: "sign-in" } },+ );+ expect(send.status()).toBe(200);+ const otp = await getCode(unknownEmail);++ const verify = await request.post("/api/better-auth/sign-in/email-otp", {+ data: { email: unknownEmail, otp },+ });+ expect(verify.status(), await verify.text()).toBe(400);+ expect(await verify.json()).toMatchObject({ code: "SIGNUP_DISABLED" });++ const user = await prisma.user.findUnique({+ where: { email: unknownEmail },+ });+ expect(user).toBeNull();+ });++ test("an existing user still signs in with a code", async ({ request }) => {+ await createUserInDb({+ email: existingEmail,+ name: "Existing User",+ role: "user",+ });++ const send = await request.post(+ "/api/better-auth/email-otp/send-verification-otp",+ { data: { email: existingEmail, type: "sign-in" } },+ );+ expect(send.status()).toBe(200);++ const otp = await getCode(existingEmail);+ const verify = await request.post("/api/better-auth/sign-in/email-otp", {+ data: { email: existingEmail, otp },+ });+ expect(verify.status()).toBe(200);+ });+});