Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

♻️ refactor: expose email harmony options slot (#15477)

YuTengjingb7e266307947
diff --git a/src/business/server/better-auth.ts b/src/business/server/better-auth.ts
index e4864559dd9..090325f2af4 100644
--- a/src/business/server/better-auth.ts
+++ b/src/business/server/better-auth.ts
@@ -1,4 +1,7 @@
-// eslint-disable-next-line unused-imports/no-unused-vars
-export async function businessEmailValidator(email: string): Promise<boolean> {
- return true;
-}
+import type { emailHarmony } from 'better-auth-harmony';
+
+export type BusinessEmailHarmonyOptions = NonNullable<Parameters<typeof emailHarmony>[0]>;
+
+export const businessEmailHarmonyOptions = {
+ allowNormalizedSignin: false,
+} satisfies BusinessEmailHarmonyOptions;
diff --git a/src/libs/better-auth/define-config.test.ts b/src/libs/better-auth/define-config.test.ts
index 69c0518cbd0..e51c112162d 100644
--- a/src/libs/better-auth/define-config.test.ts
+++ b/src/libs/better-auth/define-config.test.ts
@@ -2,6 +2,8 @@ import { describe, expect, it, vi } from 'vitest';
const mocks = vi.hoisted(() => ({
betterAuth: vi.fn((options) => options),
+ businessEmailHarmonyOptions: { allowNormalizedSignin: false },
+ emailHarmony: vi.fn((options) => ({ id: 'email-harmony', options })),
}));
vi.mock('@better-auth/expo', () => ({
@@ -12,10 +14,6 @@ vi.mock('@better-auth/passkey', () => ({
passkey: vi.fn(() => ({ id: 'passkey' })),
}));
-vi.mock('@lobechat/business-const', () => ({
- ENABLE_BUSINESS_FEATURES: false,
-}));
-
vi.mock('@lobechat/database', () => ({
createNanoId: vi.fn(() => vi.fn(() => 'generated-id')),
idGenerator: vi.fn(() => 'generated-user-id'),
@@ -50,11 +48,7 @@ vi.mock('better-auth/plugins', () => ({
}));
vi.mock('better-auth-harmony', () => ({
- emailHarmony: vi.fn(() => ({ id: 'email-harmony' })),
-}));
-
-vi.mock('better-auth-harmony/email', () => ({
- validateEmail: vi.fn(),
+ emailHarmony: mocks.emailHarmony,
}));
vi.mock('undici', () => ({
@@ -63,7 +57,7 @@ vi.mock('undici', () => ({
}));
vi.mock('@/business/server/better-auth', () => ({
- businessEmailValidator: vi.fn(),
+ businessEmailHarmonyOptions: mocks.businessEmailHarmonyOptions,
}));
vi.mock('@/envs/app', () => ({
@@ -132,4 +126,12 @@ describe('defineConfig', () => {
}),
);
});
+
+ it('should delegate emailHarmony options to the business slot', async () => {
+ const { defineConfig } = await import('./define-config');
+
+ defineConfig({ plugins: [] });
+
+ expect(mocks.emailHarmony).toHaveBeenCalledWith(mocks.businessEmailHarmonyOptions);
+ });
});
diff --git a/src/libs/better-auth/define-config.ts b/src/libs/better-auth/define-config.ts
index 775e6b60753..a1ff1a5d432 100644
--- a/src/libs/better-auth/define-config.ts
+++ b/src/libs/better-auth/define-config.ts
@@ -1,6 +1,5 @@
import { expo } from '@better-auth/expo';
import { passkey } from '@better-auth/passkey';
-import { ENABLE_BUSINESS_FEATURES } from '@lobechat/business-const';
import { createNanoId, idGenerator, serverDB } from '@lobechat/database';
import * as schema from '@lobechat/database/schemas';
import bcrypt from 'bcryptjs';
@@ -11,10 +10,9 @@ import { betterAuth } from 'better-auth/minimal';
import { admin, emailOTP, genericOAuth, magicLink } from 'better-auth/plugins';
import { type BetterAuthPlugin } from 'better-auth/types';
import { emailHarmony } from 'better-auth-harmony';
-import { validateEmail } from 'better-auth-harmony/email';
import { ProxyAgent, setGlobalDispatcher } from 'undici';
-import { businessEmailValidator } from '@/business/server/better-auth';
+import { businessEmailHarmonyOptions } from '@/business/server/better-auth';
import { appEnv } from '@/envs/app';
import { authEnv } from '@/envs/auth';
import {
@@ -84,10 +82,6 @@ const enabledSSOProviders = parseSSOProviders(authEnv.AUTH_SSO_PROVIDERS);
const { socialProviders, genericOAuthProviders } = initBetterAuthSSOProviders();
-async function customEmailValidator(email: string): Promise<boolean> {
- return ENABLE_BUSINESS_FEATURES ? businessEmailValidator(email) : validateEmail(email);
-}
-
interface CustomBetterAuthOptions {
plugins: BetterAuthPlugin[];
}
@@ -264,7 +258,7 @@ export function defineConfig(customOptions: CustomBetterAuthOptions) {
...customOptions.plugins,
emailWhitelist(),
expo(),
- emailHarmony({ allowNormalizedSignin: false, validator: customEmailValidator }),
+ emailHarmony(businessEmailHarmonyOptions),
admin(),
// Email OTP plugin for mobile verification
emailOTP({