From 4e7adcc9eec647d7290f22ef3671776216ee89de Mon Sep 17 00:00:00 2001 From: Jamison Lahman Date: Fri, 19 Dec 2025 04:07:53 -0800 Subject: [PATCH] chore(devtools): pass debug auth token with server-side requests (#6836) --- .pre-commit-config.yaml | 8 ++++---- web/README.md | 6 ++++-- web/src/lib/userSS.ts | 24 +++++++++++++++++++----- web/src/lib/utilsSS.ts | 13 ++++++++----- 4 files changed, 35 insertions(+), 16 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index ff30d755f3..abf1d75de7 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -8,10 +8,6 @@ repos: # From: https://github.com/astral-sh/uv-pre-commit/pull/53/commits/d30b4298e4fb63ce8609e29acdbcf4c9018a483c rev: d30b4298e4fb63ce8609e29acdbcf4c9018a483c hooks: - - id: uv-run - name: Check lazy imports - args: ["--with=onyx-devtools", "ods", "check-lazy-imports"] - files: ^backend/(?!\.venv/).*\.py$ - id: uv-sync args: ["--locked", "--all-extras"] - id: uv-lock @@ -32,6 +28,10 @@ repos: name: uv-export model_server.txt args: ["--no-emit-project", "--no-default-groups", "--no-hashes", "--extra", "model_server", "-o", "backend/requirements/model_server.txt"] files: ^(pyproject\.toml|uv\.lock|backend/requirements/.*\.txt)$ + - id: uv-run + name: Check lazy imports + args: ["--with=onyx-devtools", "ods", "check-lazy-imports"] + files: ^backend/(?!\.venv/).*\.py$ # NOTE: This takes ~6s on a single, large module which is prohibitively slow. # - id: uv-run # name: mypy diff --git a/web/README.md b/web/README.md index 058efdcd3b..017afb7088 100644 --- a/web/README.md +++ b/web/README.md @@ -29,8 +29,7 @@ create a `.env.local` file in the `web/` directory with the following configurat ```text # Point local dev server to cloud backend -# This allows testing frontend changes against the production/staging backend -INTERNAL_URL=https://cloud.onyx.app/api +INTERNAL_URL=https://st-dev.onyx.app/api # Debug auth cookie for authenticating against remote backend # This cookie is automatically injected into API requests when in development mode @@ -43,6 +42,9 @@ INTERNAL_URL=https://cloud.onyx.app/api DEBUG_AUTH_COOKIE=your_cookie_value_here ``` +By default, this does _NOT_ override existing cookies, so if you've logged in previously, you +may need to delete the cookies for the `localhost` domain. + **Important notes:** - The `.env.local` file should be created in the `web/` directory (same level as `package.json`) diff --git a/web/src/lib/userSS.ts b/web/src/lib/userSS.ts index 7697779300..3d54e5f769 100644 --- a/web/src/lib/userSS.ts +++ b/web/src/lib/userSS.ts @@ -165,14 +165,13 @@ export const logoutSS = async ( export const getCurrentUserSS = async (): Promise => { try { + const cookieString = processCookies(await cookies()); + const response = await fetch(buildUrl("/me"), { credentials: "include", next: { revalidate: 0 }, headers: { - cookie: (await cookies()) - .getAll() - .map((cookie) => `${cookie.name}=${cookie.value}`) - .join("; "), + cookie: cookieString, }, }); @@ -189,8 +188,23 @@ export const getCurrentUserSS = async (): Promise => { }; export const processCookies = (cookies: ReadonlyRequestCookies): string => { - return cookies + let cookieString = cookies .getAll() .map((cookie) => `${cookie.name}=${cookie.value}`) .join("; "); + + // Inject debug auth cookie for local development against remote backend (only if not already present) + if (process.env.DEBUG_AUTH_COOKIE && process.env.NODE_ENV === "development") { + const hasAuthCookie = cookieString + .split(/;\s*/) + .some((c) => c.startsWith("fastapiusersauth=")); + if (!hasAuthCookie) { + const debugCookie = `fastapiusersauth=${process.env.DEBUG_AUTH_COOKIE}`; + cookieString = cookieString + ? `${cookieString}; ${debugCookie}` + : debugCookie; + } + } + + return cookieString; }; diff --git a/web/src/lib/utilsSS.ts b/web/src/lib/utilsSS.ts index 0eb77b8c80..0276292f0f 100644 --- a/web/src/lib/utilsSS.ts +++ b/web/src/lib/utilsSS.ts @@ -1,5 +1,6 @@ import { cookies } from "next/headers"; import { HOST_URL, INTERNAL_URL } from "./constants"; +import { processCookies } from "@/lib/userSS"; export function buildClientUrl(path: string) { if (path.startsWith("/")) { @@ -57,15 +58,17 @@ export class UrlBuilder { } export async function fetchSS(url: string, options?: RequestInit) { - const init = options || { + const cookieString = processCookies(await cookies()); + + const init: RequestInit = { credentials: "include", cache: "no-store", + ...options, headers: { - cookie: (await cookies()) - .getAll() - .map((cookie) => `${cookie.name}=${cookie.value}`) - .join("; "), + ...options?.headers, + cookie: cookieString, }, }; + return fetch(buildUrl(url), init); }