Skip to content

Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.

🚚 Consolidate auth into features/auth and lib wiring (#2560)

Luke Vella59c0b2c363c8
diff --git a/CLAUDE.md b/CLAUDE.md
index 6d81f9ad0..53edd723e 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -88,7 +88,7 @@ pnpm sherif # Check package dependencies
### Key Features & Structure
- **Polls**: Core scheduling functionality in `apps/web/src/features/poll/` (legacy UI still in `components/poll/`)
- **Spaces**: Workspace/team organization in `apps/web/src/features/space/`
-- **Authentication**: Better-Auth config in `apps/web/src/lib/auth.ts`, helpers in `apps/web/src/auth/`
+- **Authentication**: Better-Auth config in `apps/web/src/lib/auth.ts`, domain logic in `apps/web/src/features/auth/`
- **tRPC API**: Routers in `apps/web/src/trpc/routers/`
- **Feature Flags**: Quick create and other toggles in `apps/web/src/lib/feature-flags/`
diff --git a/apps/web/src/app/[locale]/(auth)/register/components/register-name-form.tsx b/apps/web/src/app/[locale]/(auth)/register/components/register-name-form.tsx
index 0b5b28bda..c30cd6d67 100644
--- a/apps/web/src/app/[locale]/(auth)/register/components/register-name-form.tsx
+++ b/apps/web/src/app/[locale]/(auth)/register/components/register-name-form.tsx
@@ -18,7 +18,7 @@ import React from "react";
import { useForm } from "react-hook-form";
import { setVerificationEmail } from "@/app/[locale]/(auth)/login/actions";
import { PasswordInput } from "@/components/password-input";
-import { PasswordStrengthMeter } from "@/features/password/components/password-strength-meter";
+import { PasswordStrengthMeter } from "@/features/auth/components/password-strength-meter";
import { Trans, useTranslation } from "@/i18n/client";
import { authClient } from "@/lib/auth-client";
import { getBrowserTimeZone } from "@/utils/date-time-utils";
diff --git a/apps/web/src/app/[locale]/(auth)/register/components/schema.ts b/apps/web/src/app/[locale]/(auth)/register/components/schema.ts
index d9bd19ec5..cb1483612 100644
--- a/apps/web/src/app/[locale]/(auth)/register/components/schema.ts
+++ b/apps/web/src/app/[locale]/(auth)/register/components/schema.ts
@@ -1,5 +1,5 @@
import * as z from "zod";
-import { usePasswordValidationSchema } from "@/features/password/schema";
+import { usePasswordValidationSchema } from "@/features/auth/schema";
import { useTranslation } from "@/i18n/client";
import { isValidName } from "@/utils/is-valid-name";
diff --git a/apps/web/src/app/[locale]/(auth)/reset-password/components/reset-password-form.tsx b/apps/web/src/app/[locale]/(auth)/reset-password/components/reset-password-form.tsx
index 447bd2d49..1597ecaad 100644
--- a/apps/web/src/app/[locale]/(auth)/reset-password/components/reset-password-form.tsx
+++ b/apps/web/src/app/[locale]/(auth)/reset-password/components/reset-password-form.tsx
@@ -14,8 +14,8 @@ import { useRouter, useSearchParams } from "next/navigation";
import { useForm } from "react-hook-form";
import * as z from "zod";
import { PasswordInput } from "@/components/password-input";
-import { PasswordStrengthMeter } from "@/features/password/components/password-strength-meter";
-import { usePasswordValidationSchema } from "@/features/password/schema";
+import { PasswordStrengthMeter } from "@/features/auth/components/password-strength-meter";
+import { usePasswordValidationSchema } from "@/features/auth/schema";
import { Trans } from "@/i18n/client";
import { authClient } from "@/lib/auth-client";
import { validateRedirectUrl } from "@/utils/redirect";
diff --git a/apps/web/src/app/[locale]/(space)/settings/security/components/change-password-form.tsx b/apps/web/src/app/[locale]/(space)/settings/security/components/change-password-form.tsx
index 2594fc020..1dda12d1b 100644
--- a/apps/web/src/app/[locale]/(space)/settings/security/components/change-password-form.tsx
+++ b/apps/web/src/app/[locale]/(space)/settings/security/components/change-password-form.tsx
@@ -13,8 +13,8 @@ import { toast } from "@rallly/ui/sonner";
import { useForm } from "react-hook-form";
import * as z from "zod";
import { PasswordInput } from "@/components/password-input";
-import { PasswordStrengthMeter } from "@/features/password/components/password-strength-meter";
-import { usePasswordValidationSchema } from "@/features/password/schema";
+import { PasswordStrengthMeter } from "@/features/auth/components/password-strength-meter";
+import { usePasswordValidationSchema } from "@/features/auth/schema";
import { Trans, useTranslation } from "@/i18n/client";
import { authClient } from "@/lib/auth-client";
diff --git a/apps/web/src/app/[locale]/invite/[urlId]/page.tsx b/apps/web/src/app/[locale]/invite/[urlId]/page.tsx
index d9c18bcac..92c1376ac 100644
--- a/apps/web/src/app/[locale]/invite/[urlId]/page.tsx
+++ b/apps/web/src/app/[locale]/invite/[urlId]/page.tsx
@@ -10,8 +10,8 @@ import { getLocale } from "@/i18n/server/get-locale";
import { DeviceDateTimeProvider } from "@/lib/datetime/device";
import { getDeviceDateTimeConfig } from "@/lib/datetime/server";
import { LocaleSync } from "@/lib/locale/client";
+import { decryptToken } from "@/lib/session";
import { createPublicSSRHelper } from "@/trpc/server/create-ssr-helper";
-import { decryptToken } from "@/utils/session";
import { InvitePageLoader } from "./invite-page-loader";
import Providers from "./providers";
diff --git a/apps/web/src/features/password/components/password-strength-meter.tsx b/apps/web/src/features/auth/components/password-strength-meter.tsx
similarity index 100%
rename from apps/web/src/features/password/components/password-strength-meter.tsx
rename to apps/web/src/features/auth/components/password-strength-meter.tsx
diff --git a/apps/web/src/auth/helpers/temp-email-domains.ts b/apps/web/src/features/auth/constants.ts
similarity index 79%
rename from apps/web/src/auth/helpers/temp-email-domains.ts
rename to apps/web/src/features/auth/constants.ts
index d175b5048..e7c72cba1 100644
--- a/apps/web/src/auth/helpers/temp-email-domains.ts
+++ b/apps/web/src/features/auth/constants.ts
@@ -53,13 +53,3 @@ export const temporaryEmailDomains = [
"mailto.plus",
"ethereal.mail",
];
-
-/**
- * Checks if an email domain is a known temporary/disposable email service
- */
-export const isTemporaryEmail = (email: string): boolean => {
- const domain = email.split("@")[1]?.toLowerCase();
- if (!domain) return false;
-
- return temporaryEmailDomains.includes(domain);
-};
diff --git a/apps/web/src/auth/helpers/is-email-blocked.ts b/apps/web/src/features/auth/data.ts
similarity index 81%
rename from apps/web/src/auth/helpers/is-email-blocked.ts
rename to apps/web/src/features/auth/data.ts
index cbb8a1ef9..92b3f090d 100644
--- a/apps/web/src/auth/helpers/is-email-blocked.ts
+++ b/apps/web/src/features/auth/data.ts
@@ -1,3 +1,4 @@
+import "server-only";
import { prisma } from "@rallly/database";
/**
@@ -88,24 +89,3 @@ export async function isEmailBanned(email: string) {
return bannedUser > 0;
}
-
-export function isEmailBlocked(email: string) {
- if (process.env.ALLOWED_EMAILS) {
- const allowedEmails = process.env.ALLOWED_EMAILS.split(",");
- // Check whether the email matches enough of the patterns specified in ALLOWED_EMAILS
- const isAllowed = allowedEmails.some((allowedEmail) => {
- const regex = new RegExp(
- `^${allowedEmail
- .replace(/[.+?^${}()|[\]\\]/g, "\\$&")
- .replaceAll(/[*]/g, ".*")}$`,
- );
- return regex.test(email);
- });
-
- if (!isAllowed) {
- return true;
- }
- }
-
- return false;
-}
diff --git a/apps/web/src/auth/helpers/merge-user.ts b/apps/web/src/features/auth/mutations.ts
similarity index 99%
rename from apps/web/src/auth/helpers/merge-user.ts
rename to apps/web/src/features/auth/mutations.ts
index a9799b04c..66404a479 100644
--- a/apps/web/src/auth/helpers/merge-user.ts
+++ b/apps/web/src/features/auth/mutations.ts
@@ -1,3 +1,4 @@
+import "server-only";
import { prisma } from "@rallly/database";
import { createLogger } from "@rallly/logger";
import * as Sentry from "@sentry/nextjs";
diff --git a/apps/web/src/features/password/schema.ts b/apps/web/src/features/auth/schema.ts
similarity index 93%
rename from apps/web/src/features/password/schema.ts
rename to apps/web/src/features/auth/schema.ts
index b19612e30..5d9844e86 100644
--- a/apps/web/src/features/password/schema.ts
+++ b/apps/web/src/features/auth/schema.ts
@@ -3,7 +3,7 @@ import * as z from "zod";
import {
calculatePasswordStrength,
passwordQualityThresholds,
-} from "@/features/password/utils";
+} from "@/features/auth/utils";
import { useTranslation } from "@/i18n/client";
export function usePasswordValidationSchema() {
diff --git a/apps/web/src/features/password/types.ts b/apps/web/src/features/auth/types.ts
similarity index 100%
rename from apps/web/src/features/password/types.ts
rename to apps/web/src/features/auth/types.ts
diff --git a/apps/web/src/features/auth/utils.ts b/apps/web/src/features/auth/utils.ts
new file mode 100644
index 000000000..71e330e9d
--- /dev/null
+++ b/apps/web/src/features/auth/utils.ts
@@ -0,0 +1,75 @@
+import zxcvbn from "zxcvbn";
+import { temporaryEmailDomains } from "@/features/auth/constants";
+import type { PasswordQuality } from "@/features/auth/types";
+
+/**
+ * Calculate password strength using zxcvbn
+ * @returns Score from 0-4:
+ * - 0: too guessable (very weak)
+ * - 1: very guessable (weak)
+ * - 2: somewhat guessable (fair)
+ * - 3: safely unguessable (good)
+ * - 4: very unguessable (strong)
+ */
+export function calculatePasswordStrength(password: string): number {
+ if (!password) return 0;
+ const result = zxcvbn(password);
+ return result.score;
+}
+
+export function getPasswordQuality(password: string): PasswordQuality {
+ const score = calculatePasswordStrength(password);
+ switch (score) {
+ case 0:
+ return "veryWeak";
+ case 1:
+ return "weak";
+ case 2:
+ return "fair";
+ case 3:
+ return "good";
+ case 4:
+ return "strong";
+ default:
+ return "veryWeak";
+ }
+}
+
+export const passwordQualityThresholds: Record<PasswordQuality, number> = {
+ veryWeak: 0,
+ weak: 1,
+ fair: 2,
+ good: 3,
+ strong: 4,
+};
+
+export function isEmailBlocked(email: string) {
+ if (process.env.ALLOWED_EMAILS) {
+ const allowedEmails = process.env.ALLOWED_EMAILS.split(",");
+ // Check whether the email matches enough of the patterns specified in ALLOWED_EMAILS
+ const isAllowed = allowedEmails.some((allowedEmail) => {
+ const regex = new RegExp(
+ `^${allowedEmail
+ .replace(/[.+?^${}()|[\]\\]/g, "\\$&")
+ .replaceAll(/[*]/g, ".*")}$`,
+ );
+ return regex.test(email);
+ });
+
+ if (!isAllowed) {
+ return true;
+ }
+ }
+
+ return false;
+}
+
+/**
+ * Checks if an email domain is a known temporary/disposable email service
+ */
+export const isTemporaryEmail = (email: string): boolean => {
+ const domain = email.split("@")[1]?.toLowerCase();
+ if (!domain) return false;
+
+ return temporaryEmailDomains.includes(domain);
+};
diff --git a/apps/web/src/features/password/utils.ts b/apps/web/src/features/password/utils.ts
deleted file mode 100644
index f668d8f7a..000000000
--- a/apps/web/src/features/password/utils.ts
+++ /dev/null
@@ -1,43 +0,0 @@
-import zxcvbn from "zxcvbn";
-import type { PasswordQuality } from "@/features/password/types";
-
-/**
- * Calculate password strength using zxcvbn
- * @returns Score from 0-4:
- * - 0: too guessable (very weak)
- * - 1: very guessable (weak)
- * - 2: somewhat guessable (fair)
- * - 3: safely unguessable (good)
- * - 4: very unguessable (strong)
- */
-export function calculatePasswordStrength(password: string): number {
- if (!password) return 0;
- const result = zxcvbn(password);
- return result.score;
-}
-
-export function getPasswordQuality(password: string): PasswordQuality {
- const score = calculatePasswordStrength(password);
- switch (score) {
- case 0:
- return "veryWeak";
- case 1:
- return "weak";
- case 2:
- return "fair";
- case 3:
- return "good";
- case 4:
- return "strong";
- default:
- return "veryWeak";
- }
-}
-
-export const passwordQualityThresholds: Record<PasswordQuality, number> = {
- veryWeak: 0,
- weak: 1,
- fair: 2,
- good: 3,
- strong: 4,
-};
diff --git a/apps/web/src/auth/plugins/host-only-cookie-cleanup.ts b/apps/web/src/lib/auth-plugins/host-only-cookie-cleanup.ts
similarity index 100%
rename from apps/web/src/auth/plugins/host-only-cookie-cleanup.ts
rename to apps/web/src/lib/auth-plugins/host-only-cookie-cleanup.ts
diff --git a/apps/web/src/lib/auth.ts b/apps/web/src/lib/auth.ts
index 58c13a76a..ff20c5c67 100644
--- a/apps/web/src/lib/auth.ts
+++ b/apps/web/src/lib/auth.ts
@@ -23,17 +23,16 @@ import { headers } from "next/headers";
import { redirect } from "next/navigation";
import { after } from "next/server";
import { cache } from "react";
-import { isEmailBlocked } from "@/auth/helpers/is-email-blocked";
-import { linkAnonymousUser } from "@/auth/helpers/merge-user";
-import { isTemporaryEmail } from "@/auth/helpers/temp-email-domains";
-import { hostOnlyCookieCleanup } from "@/auth/plugins/host-only-cookie-cleanup";
import { getInstanceBranding } from "@/emails/branding";
import { env } from "@/env";
import { posthog } from "@/features/analytics/posthog";
+import { linkAnonymousUser } from "@/features/auth/mutations";
+import { isEmailBlocked, isTemporaryEmail } from "@/features/auth/utils";
import { createSpace } from "@/features/space/mutations";
import type { UserDTO } from "@/features/user/schema";
import { getTranslation } from "@/i18n/server";
import { getLocale } from "@/i18n/server/get-locale";
+import { hostOnlyCookieCleanup } from "@/lib/auth-plugins/host-only-cookie-cleanup";
import { redis } from "@/lib/kv";
import { getValueByPath } from "@/utils/get-value-by-path";
diff --git a/apps/web/src/utils/session/session-config.ts b/apps/web/src/lib/session-config.ts
similarity index 100%
rename from apps/web/src/utils/session/session-config.ts
rename to apps/web/src/lib/session-config.ts
diff --git a/apps/web/src/utils/session/index.ts b/apps/web/src/lib/session.ts
similarity index 100%
rename from apps/web/src/utils/session/index.ts
rename to apps/web/src/lib/session.ts
diff --git a/apps/web/src/trpc/routers/auth.ts b/apps/web/src/trpc/routers/auth.ts
index 608051f02..2ed455c14 100644
--- a/apps/web/src/trpc/routers/auth.ts
+++ b/apps/web/src/trpc/routers/auth.ts
@@ -1,7 +1,7 @@
import { prisma } from "@rallly/database";
import * as z from "zod";
-import { decryptToken } from "@/utils/session";
+import { decryptToken } from "@/lib/session";
import { createRateLimitMiddleware, publicProcedure, router } from "../trpc";
export const auth = router({
diff --git a/apps/web/src/trpc/routers/polls/utils.ts b/apps/web/src/trpc/routers/polls/utils.ts
index f86aa4359..b7c6129ff 100644
--- a/apps/web/src/trpc/routers/polls/utils.ts
+++ b/apps/web/src/trpc/routers/polls/utils.ts
@@ -1,5 +1,5 @@
import { TRPCError } from "@trpc/server";
-import { createToken, decryptToken } from "@/utils/session";
+import { createToken, decryptToken } from "@/lib/session";
export async function getUserIdFromToken(
token: string | undefined,