From c341f8d8759e487492f370d5466c9558362506cc Mon Sep 17 00:00:00 2001 From: Nicolas Meienberger Date: Sat, 14 Feb 2026 11:47:05 +0100 Subject: [PATCH] refactor: add auth to getServerConstants --- .gitignore | 1 + .../components/create-repository-form.tsx | 2 +- .../local-repository-form.tsx | 2 +- app/server/core/constants.ts | 9 --------- app/server/lib/functions/server-constants.ts | 19 +++++++++++++++++++ 5 files changed, 22 insertions(+), 11 deletions(-) create mode 100644 app/server/lib/functions/server-constants.ts diff --git a/.gitignore b/.gitignore index 6d1d89b2..f5ebc411 100644 --- a/.gitignore +++ b/.gitignore @@ -37,3 +37,4 @@ playwright/temp # OpenAPI error logs openapi-ts-error-*.log .output +tmp/ diff --git a/app/client/modules/repositories/components/create-repository-form.tsx b/app/client/modules/repositories/components/create-repository-form.tsx index 93165770..64afcb15 100644 --- a/app/client/modules/repositories/components/create-repository-form.tsx +++ b/app/client/modules/repositories/components/create-repository-form.tsx @@ -34,7 +34,7 @@ import { AdvancedForm, } from "./repository-forms"; import { useServerFn } from "@tanstack/react-start"; -import { getServerConstants } from "~/server/core/constants"; +import { getServerConstants } from "~/server/lib/functions/server-constants"; import { useSuspenseQuery } from "@tanstack/react-query"; export const formSchema = type({ diff --git a/app/client/modules/repositories/components/repository-forms/local-repository-form.tsx b/app/client/modules/repositories/components/repository-forms/local-repository-form.tsx index 5f993550..f9efa2f4 100644 --- a/app/client/modules/repositories/components/repository-forms/local-repository-form.tsx +++ b/app/client/modules/repositories/components/repository-forms/local-repository-form.tsx @@ -16,7 +16,7 @@ import { } from "../../../../components/ui/alert-dialog"; import type { RepositoryFormValues } from "../create-repository-form"; import { useServerFn } from "@tanstack/react-start"; -import { getServerConstants } from "~/server/core/constants"; +import { getServerConstants } from "~/server/lib/functions/server-constants"; import { useSuspenseQuery } from "@tanstack/react-query"; type Props = { diff --git a/app/server/core/constants.ts b/app/server/core/constants.ts index c22e676f..eb12ed4e 100644 --- a/app/server/core/constants.ts +++ b/app/server/core/constants.ts @@ -1,5 +1,3 @@ -import { createServerFn } from "@tanstack/react-start"; - export const OPERATION_TIMEOUT = 5000; export const VOLUME_MOUNT_BASE = process.env.ZEROBYTE_VOLUMES_DIR || "/var/lib/zerobyte/volumes"; @@ -15,10 +13,3 @@ export const RCLONE_CONFIG_DIR = process.env.RCLONE_CONFIG_DIR || "/root/.config export const DEFAULT_EXCLUDES = [DATABASE_URL, RESTIC_PASS_FILE, REPOSITORY_BASE]; export const REGISTRATION_ENABLED_KEY = "registrations_enabled"; - -export const getServerConstants = createServerFn({ method: "GET" }).handler(() => { - return { - REPOSITORY_BASE, - REGISTRATION_ENABLED_KEY, - }; -}); diff --git a/app/server/lib/functions/server-constants.ts b/app/server/lib/functions/server-constants.ts new file mode 100644 index 00000000..c508a6e8 --- /dev/null +++ b/app/server/lib/functions/server-constants.ts @@ -0,0 +1,19 @@ +import { createServerFn } from "@tanstack/react-start"; +import { getRequestHeaders } from "@tanstack/react-start/server"; +import { UnauthorizedError } from "http-errors-enhanced"; +import { auth } from "~/server/lib/auth"; +import { REGISTRATION_ENABLED_KEY, REPOSITORY_BASE } from "~/server/core/constants"; + +export const getServerConstants = createServerFn({ method: "GET" }).handler(async () => { + const headers = getRequestHeaders(); + const session = await auth.api.getSession({ headers }); + + if (!session?.user) { + throw new UnauthorizedError("Invalid or expired session"); + } + + return { + REPOSITORY_BASE, + REGISTRATION_ENABLED_KEY, + }; +});