refactor: add auth to getServerConstants
This commit is contained in:
parent
2252543e32
commit
c341f8d875
5 changed files with 22 additions and 11 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -37,3 +37,4 @@ playwright/temp
|
|||
# OpenAPI error logs
|
||||
openapi-ts-error-*.log
|
||||
.output
|
||||
tmp/
|
||||
|
|
|
|||
|
|
@ -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({
|
||||
|
|
|
|||
|
|
@ -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 = {
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
};
|
||||
});
|
||||
|
|
|
|||
19
app/server/lib/functions/server-constants.ts
Normal file
19
app/server/lib/functions/server-constants.ts
Normal file
|
|
@ -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,
|
||||
};
|
||||
});
|
||||
Loading…
Reference in a new issue