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.

13.5 KB
Raw Blame
import { expo } from '@better-auth/expo';
import { passkey } from '@better-auth/passkey';
import { BRANDING_NAME } from '@lobechat/business-const';
import { createNanoId, idGenerator, serverDB } from '@lobechat/database';
import * as schema from '@lobechat/database/schemas';
import bcrypt from 'bcryptjs';
import { drizzleAdapter } from 'better-auth/adapters/drizzle';
import { verifyPassword as defaultVerifyPassword } from 'better-auth/crypto';
import { type BetterAuthOptions } from 'better-auth/minimal';
import { betterAuth } from 'better-auth/minimal';
import { admin, emailOTP, genericOAuth, magicLink } from 'better-auth/plugins';
import { type BetterAuthPlugin } from 'better-auth/types';
import { EnvHttpProxyAgent, setGlobalDispatcher } from 'undici';

import { appEnv } from '@/envs/app';
import { authEnv } from '@/envs/auth';
import {
  getChangeEmailVerificationTemplate,
  getMagicLinkEmailTemplate,
  getResetPasswordEmailTemplate,
  getVerificationEmailTemplate,
  getVerificationOTPEmailTemplate,
} from '@/libs/better-auth/email-templates';
import { changeEmailUniqueness } from '@/libs/better-auth/plugins/change-email-uniqueness';
import { deferredSessionRefresh } from '@/libs/better-auth/plugins/deferred-session-refresh';
import { emailWhitelist } from '@/libs/better-auth/plugins/email-whitelist';
import { sessionOptions } from '@/libs/better-auth/session-options';
import { initBetterAuthSSOProviders } from '@/libs/better-auth/sso';
import {
  createSecondaryStorage,
  getPasskeyOrigins,
  getTrustedOrigins,
} from '@/libs/better-auth/utils/config';
import { expireLegacyHostOnlyCookies } from '@/libs/better-auth/utils/host-only-cookies';
import { parseSSOProviders } from '@/libs/better-auth/utils/server';
import { clearMismatchedOIDCSession } from '@/libs/oidc-provider/session-cleanup';
import { EmailService } from '@/server/services/email';
import { UserService } from '@/server/services/user';

const LOCAL_NO_PROXY_HOSTS = ['localhost', '127.0.0.1', '[::1]'];

export const mergeLocalNoProxy = (noProxy?: string): string => {
  const entries = new Set(
    (noProxy || '')
      .split(/[,\s]+/)
      .map((entry) => entry.trim())
      .filter(Boolean),
  );

  if (entries.has('*')) return '*';

  for (const host of LOCAL_NO_PROXY_HOSTS) {
    entries.add(host);
  }

  return [...entries].join(',');
};

// Configure HTTP proxy for OAuth provider requests in development (e.g., Google token exchange).
// Node.js native fetch doesn't respect system proxy settings. Keep localhost direct so Next can
// fetch local Vite templates such as /index.auth.html without depending on the system proxy.
// Ref: https://github.com/better-auth/better-auth/issues/7396
if (process.env.NODE_ENV === 'development') {
  const httpProxy = process.env.HTTP_PROXY || process.env.http_proxy;
  const httpsProxy = process.env.HTTPS_PROXY || process.env.https_proxy || httpProxy;

  if (httpProxy || httpsProxy) {
    const proxyAgent = new EnvHttpProxyAgent({
      ...(httpProxy && { httpProxy }),
      ...(httpsProxy && { httpsProxy }),
      noProxy: mergeLocalNoProxy(process.env.NO_PROXY || process.env.no_proxy),
    });
    setGlobalDispatcher(proxyAgent);
  }
}

// Email verification link expiration time (in seconds)
// Default is 1 hour (3600 seconds) as per Better Auth documentation
const VERIFICATION_LINK_EXPIRES_IN = 3600;

/**
 * Safely extract hostname from APP_URL for passkey rpID.
 * Returns undefined if APP_URL is not set (e.g., in e2e tests).
 */
const getPasskeyRpID = (): string | undefined => {
  if (!appEnv.APP_URL) return undefined;
  try {
    return new URL(appEnv.APP_URL).hostname;
  } catch {
    return undefined;
  }
};

/**
 * Browsers silently drop a cookie whose `Domain` the current host is not a member of.
 * Applying a production domain on a preview deployment (`*.vercel.app`) or localhost would
 * therefore erase every auth cookie instead of widening it, so fall back to host-only there.
 */
const resolveCookieDomain = (cookieDomain?: string): string | undefined => {
  if (!cookieDomain) return undefined;

  const base = cookieDomain.replace(/^\./, '');
  try {
    const { hostname } = new URL(appEnv.APP_URL);
    if (hostname !== base && !hostname.endsWith(`.${base}`)) return undefined;
  } catch {
    return undefined;
  }

  return cookieDomain;
};

const MAGIC_LINK_EXPIRES_IN = 900;
// OTP expiration time (in seconds) - 5 minutes for mobile OTP verification
const OTP_EXPIRES_IN = 300;
const enableMagicLink = authEnv.AUTH_ENABLE_MAGIC_LINK;
const enabledSSOProviders = parseSSOProviders(authEnv.AUTH_SSO_PROVIDERS);

const { socialProviders, genericOAuthProviders } = initBetterAuthSSOProviders();

interface CustomBetterAuthOptions {
  /**
   * Share auth cookies across every subdomain of this domain (e.g. `.example.com`).
   * Omit to keep cookies host-only.
   */
  cookieDomain?: string;
  /** Namespace every Better Auth cookie so colocated deployments cannot overwrite each other. */
  cookiePrefix?: string;
  plugins: BetterAuthPlugin[];
}

export function defineConfig(customOptions: CustomBetterAuthOptions) {
  const cookieDomain = resolveCookieDomain(customOptions.cookieDomain);

  const options = {
    account: {
      accountLinking: {
        allowDifferentEmails: true,
        enabled: true,
        trustedProviders: enabledSSOProviders,
      },
    },

    baseURL: appEnv.APP_URL,
    secret: authEnv.AUTH_SECRET,
    trustedOrigins: getTrustedOrigins(enabledSSOProviders),

    emailAndPassword: {
      autoSignIn: true,
      disableSignUp: authEnv.AUTH_DISABLE_EMAIL_PASSWORD,
      enabled: !authEnv.AUTH_DISABLE_EMAIL_PASSWORD,
      maxPasswordLength: 64,
      minPasswordLength: 8,
      requireEmailVerification: authEnv.AUTH_EMAIL_VERIFICATION,
      revokeSessionsOnPasswordReset: true,

      // Compatible with bcrypt password hashes migrated from Clerk; after login, you can re-hash in the backend using BetterAuth's default scrypt.
      password: {
        // New passwords continue to use BetterAuth's default hash to stay consistent with the official configuration.
        async verify({ hash, password }: { hash: string; password: string }): Promise<boolean> {
          if (!hash) return false;

          // Compatible with bcrypt hashes exported from Clerk (starting with $2a$ or $2b$)
          if (hash.startsWith('$2a$') || hash.startsWith('$2b$')) {
            return bcrypt.compare(password, hash);
          }

          // For all other cases, use BetterAuth's default verification
          return defaultVerifyPassword({ hash, password });
        },
      },

      sendResetPassword: async ({ user, url }) => {
        const template = getResetPasswordEmailTemplate({ url });

        const emailService = new EmailService();
        await emailService.sendMail({
          to: user.email,
          ...template,
        });
      },
    },
    emailVerification: {
      autoSignInAfterVerification: true,
      expiresIn: VERIFICATION_LINK_EXPIRES_IN,
      sendVerificationEmail: async ({ user, url }, request) => {
        // Skip sending verification link email for mobile clients (Expo/React Native)
        // Mobile clients use OTP verification instead, triggered manually via emailOTP plugin
        if (request?.headers?.get?.('x-client-type') === 'mobile') {
          return;
        }

        // Use different template for change-email vs signup verification
        const isChangeEmail = request?.url?.includes('/change-email');
        const template = isChangeEmail
          ? getChangeEmailVerificationTemplate({
              expiresInSeconds: VERIFICATION_LINK_EXPIRES_IN,
              url,
              userName: user.name,
            })
          : getVerificationEmailTemplate({
              expiresInSeconds: VERIFICATION_LINK_EXPIRES_IN,
              url,
              userName: user.name,
            });

        const emailService = new EmailService();
        await emailService.sendMail({
          to: user.email,
          ...template,
        });
      },
    },
    onAPIError: {
      errorURL: '/auth-error',
    },
    session: sessionOptions,
    database: drizzleAdapter(serverDB, {
      provider: 'pg',
      // Joined account/session queries need the complete relation schema.
      schema,
    }),
    secondaryStorage: createSecondaryStorage(),
    /**
     * Run user bootstrap for every newly created account (email, magic link, OAuth/social, etc.).
     * Using Better Auth database hooks ensures we catch social flows that bypass /sign-up/* routes.
     * Ref: https://www.better-auth.com/docs/reference/options#databasehooks
     */
    databaseHooks: {
      session: {
        create: {
          before: async (session, context) => {
            try {
              await clearMismatchedOIDCSession(serverDB, session.userId, context);
            } catch (error) {
              /**
               * OIDC cleanup is a provider-specific recovery guard. Its failure must not prevent
               * Better Auth from creating the primary application session.
               */
              console.error('[Better Auth] Failed to clear a stale OIDC session:', error);
            }
          },
        },
      },
      user: {
        create: {
          after: async (user) => {
            const userService = new UserService(serverDB);
            await userService.initUser({
              email: user.email,
              id: user.id,
              username: user.username as string | null,
              createdAt: user.createdAt,
              // TODO: if add phone plugin, we should fill phone here
            });
          },
        },
      },
    },
    user: {
      changeEmail: {
        enabled: true,
      },
      additionalFields: {
        username: {
          required: false,
          type: 'string',
        },
      },
      fields: {
        image: 'avatar',
        // NOTE: use drizzle filed instead of db field, so use fullName instead of full_name
        name: 'fullName',
      },
      modelName: 'users',
    },

    socialProviders,
    advanced: {
      ...(cookieDomain && {
        crossSubDomainCookies: { domain: cookieDomain, enabled: true },
      }),
      ...(customOptions.cookiePrefix && { cookiePrefix: customOptions.cookiePrefix }),
      database: {
        joins: true,
        /**
         * Align Better Auth user IDs with our shared idGenerator for consistency.
         * Other models use the shared nanoid generator (12 chars) to keep IDs consistent project-wide.
         */
        generateId: ({ model }) => {
          // Better Auth passes the model name; handle both singular and plural for safety.
          if (model === 'user' || model === 'users') {
            // clerk id length is 32
            return idGenerator('user', 32 - 'user_'.length);
          }

          // Other models: use shared nanoid generator (12 chars) to keep consistency.
          return createNanoId(12)();
        },
      },
    },
    rateLimit: {
      customRules: {
        '/request-password-reset': { max: 3, window: 60 },
        '/send-verification-email': { max: 3, window: 60 },
      },
    },
    plugins: [
      ...customOptions.plugins,
      deferredSessionRefresh(),
      changeEmailUniqueness(),
      emailWhitelist(),
      expo(),
      admin(),
      // Email OTP plugin for mobile verification
      emailOTP({
        expiresIn: OTP_EXPIRES_IN,
        otpLength: 6,
        allowedAttempts: 3,
        // Don't automatically send OTP on sign up - let mobile client manually trigger it
        sendVerificationOnSignUp: false,
        async sendVerificationOTP({ email, otp }) {
          const emailService = new EmailService();

          // For all OTP types, use the same template
          // userName is optional and will be null since we don't have user context here
          const template = getVerificationOTPEmailTemplate({
            expiresInSeconds: OTP_EXPIRES_IN,
            otp,
            userName: null,
          });

          await emailService.sendMail({
            to: email,
            ...template,
          });
        },
      }),
      passkey({
        // Surfaced by the browser/OS passkey prompt ("Create a passkey for …"),
        // so a hardcoded value leaks the upstream brand into native UI that no
        // amount of in-app theming can reach.
        rpName: BRANDING_NAME,
        // Extract rpID from auth URL (e.g., 'lobehub.com' from 'https://lobehub.com')
        // Returns undefined if AUTH_URL is not set (e.g., in e2e tests)
        rpID: getPasskeyRpID(),
        // Keep Android APK origins aligned with the public Digital Asset Links declaration.
        origin: getPasskeyOrigins(),
      }),
      ...(genericOAuthProviders.length > 0
        ? [
            genericOAuth({
              config: genericOAuthProviders,
            }),
          ]
        : []),
      ...(enableMagicLink
        ? [
            magicLink({
              expiresIn: MAGIC_LINK_EXPIRES_IN,
              sendMagicLink: async ({ email, url }) => {
                const template = getMagicLinkEmailTemplate({
                  expiresInSeconds: MAGIC_LINK_EXPIRES_IN,
                  url,
                });

                const emailService = new EmailService();
                await emailService.sendMail({
                  to: email,
                  ...template,
                });
              },
            }),
          ]
        : []),
    ],
  } satisfies BetterAuthOptions;

  const instance = betterAuth(options);
  if (!cookieDomain) return instance;

  const handleRequest = instance.handler;
  instance.handler = async (request) =>
    expireLegacyHostOnlyCookies(request, await handleRequest(request), cookieDomain);

  return instance;
}