feat: add admin token config and auth helper

This commit is contained in:
William Valentin
2026-02-01 02:45:45 -08:00
parent 4c37115927
commit 50aa6008e3
3 changed files with 28 additions and 1 deletions

View File

@@ -0,0 +1,14 @@
import { test, expect } from "bun:test";
import { isAdminRequest } from "./adminAuth";
test("isAdminRequest returns false when ADMIN_TOKEN unset", () => {
expect(isAdminRequest({ adminToken: undefined }, { headerToken: "x" })).toBe(
false,
);
});
test("isAdminRequest returns true when header token matches", () => {
expect(
isAdminRequest({ adminToken: "secret" }, { headerToken: "secret" }),
).toBe(true);
});

View File

@@ -0,0 +1,7 @@
export function isAdminRequest(
env: { adminToken: string | undefined },
input: { headerToken: string | null | undefined },
) {
if (!env.adminToken) return false;
return input.headerToken === env.adminToken;
}

View File

@@ -2,7 +2,8 @@ import { z } from "zod";
const envSchema = z.object({
APP_NAME: z.string().min(1).default("porthole"),
NEXT_PUBLIC_APP_NAME: z.string().min(1).optional()
NEXT_PUBLIC_APP_NAME: z.string().min(1).optional(),
ADMIN_TOKEN: z.string().min(1).optional(),
});
let cachedEnv: z.infer<typeof envSchema> | undefined;
@@ -23,3 +24,8 @@ export function getAppName() {
const env = getEnv();
return env.NEXT_PUBLIC_APP_NAME ?? env.APP_NAME;
}
export function getAdminToken() {
const env = getEnv();
return env.ADMIN_TOKEN;
}