110 lines · 4.5 KB
import { createServerFn } from "@tanstack/react-start";
import { env } from "cloudflare:workers";
import { unreadNotifications } from "~/server/user-inbox";
import { enforceRateLimit, requestIp } from "~/server/limits";
import { z } from "zod";
import { viewerMiddleware } from "./middleware";
import { createSession, destroySession, disabledSessionNotice, loadUserByLogin } from "~/server/auth";
import { verifyPassword, verifyPasswordAgainstNobody } from "~/server/crypto";
import { createUser, githubOAuthEnabled, signupsEnabled } from "~/server/accounts";
import { badRequest } from "~/server/errors";
import { rehashIfStale, sendEmailVerification } from "~/server/account-security";
import { requiresSecondFactor, startPendingLogin } from "~/server/two-factor";
import { accountName, email, password } from "~/lib/validation";
import { requireTurnstile, turnstileSiteKey } from "~/server/turnstile";
import { clientAnalyticsConfig } from "~/server/analytics";
import { recordTermsAcceptance } from "~/server/legal";
import { assertAccountActive } from "~/server/account-disable";
import { isSupporter } from "~/server/supporter";

const turnstileToken = z.string().max(2048).optional();

export const getSession = createServerFn({ method: "GET" })
  .middleware([viewerMiddleware])
  .handler(async ({ context }) => {
    const [unread, supporter] = context.viewer
      ? await Promise.all([unreadNotifications(context.viewer.id), isSupporter(context.viewer.id)])
      : [0, false];
    const disabled = context.viewer ? null : await disabledSessionNotice();
    return {
      viewer: context.viewer,
      supporter,
      unreadNotifications: unread,
      analytics: clientAnalyticsConfig(),
      disabled,
      publicUrl: env.PUBLIC_URL,
    };
  });

export const getAuthOptions = createServerFn({ method: "GET" }).handler(async () => {
  return {