From 13b80a225a4fc69c07c7769b6968a9e2ff35a484 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Burak=20Boduro=C4=9Flu?= <80620802+burakboduroglu@users.noreply.github.com> Date: Sat, 19 Sep 2026 15:11:53 +0300 Subject: [PATCH] =?UTF-8?q?chore:=20=F0=9F=94=91=20optional=20auto=20sign-?= =?UTF-8?q?in=20for=20local=20development=20-=20with=20VITE=5FDEV=5FAUTOLO?= =?UTF-8?q?GIN=5FUSERNAME=20/=20=5FPASSWORD=20(optional=20=5FSCHOOL)=20in?= =?UTF-8?q?=20=20=20.env.local,=20bun=20run=20dev=20signs=20in=20on=20the?= =?UTF-8?q?=20first=20/auth/me=20401,=20picking=20the=20=20=20named=20or?= =?UTF-8?q?=20first=20school=20when=20the=20account=20has=20several=20-=20?= =?UTF-8?q?logout=20turns=20it=20off=20for=20the=20rest=20of=20the=20tab,?= =?UTF-8?q?=20so=20the=20login=20page=20stays=20-=20behind=20import.meta.e?= =?UTF-8?q?nv.DEV:=20checked=20that=20a=20vite=20build=20with=20.env.local?= =?UTF-8?q?=20=20=20present=20carries=20neither=20the=20code=20nor=20the?= =?UTF-8?q?=20credentials=20-=20.env.local=20is=20covered=20by=20the=20exi?= =?UTF-8?q?sting=20*.local=20ignore;=20README=20notes=20it?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 ++ src/lib/dev-auto-login.ts | 32 ++++++++++++++++++++++++++++++++ src/stores/auth-context.tsx | 4 +++- src/vite-env.d.ts | 7 +++++++ 4 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 src/lib/dev-auto-login.ts diff --git a/README.md b/README.md index 1c3f1eb..03b1862 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,8 @@ Open [http://localhost:5173](http://localhost:5173) in your browser. > **Proxy Note**: Vite proxies `/api/*` and WebSocket connections to `BACKEND_ORIGIN`, preserving HttpOnly session cookies across same-origin calls. Local development defaults to `https://hezarfen-backend.dizey.sh`, so `bun run dev` works without a backend process on the laptop; use `BACKEND_ORIGIN=http://127.0.0.1:7656 bun run dev` to target one locally. The production Bun server gets its co-located loopback default from the image and can be overridden through `hezarfen_frontend.env` (see "Run in a container" below). +> **Dev auto sign-in**: for local testing, put `VITE_DEV_AUTOLOGIN_USERNAME` and `VITE_DEV_AUTOLOGIN_PASSWORD` (optionally `VITE_DEV_AUTOLOGIN_SCHOOL`, a school slug) in `.env.local`, which git ignores. `bun run dev` then signs in on the first `401`; logging out turns it off for that tab. It lives in `src/lib/dev-auto-login.ts` behind `import.meta.env.DEV`, so `vite build` drops it and the credentials never reach `dist/`. + --- ## 🐳 Run in a container (podman) diff --git a/src/lib/dev-auto-login.ts b/src/lib/dev-auto-login.ts new file mode 100644 index 0000000..a6a3329 --- /dev/null +++ b/src/lib/dev-auto-login.ts @@ -0,0 +1,32 @@ +import { postLogin, postSelectSchool } from "@/api/auth"; +import type { User } from "@/api/client"; + +const SKIP_KEY = "hezarfen:dev-auto-login-off"; + +/** + * Development-only sign-in with credentials from `.env.local` + * (`VITE_DEV_AUTOLOGIN_USERNAME`, `VITE_DEV_AUTOLOGIN_PASSWORD`, optional + * `VITE_DEV_AUTOLOGIN_SCHOOL`). `import.meta.env.DEV` is a build-time false in + * `vite build`, so the whole body is dropped from production bundles. + * An explicit logout turns it off for the rest of the tab. + */ +export async function devAutoLogin(): Promise { + if (!import.meta.env.DEV) return null; + const username = import.meta.env.VITE_DEV_AUTOLOGIN_USERNAME; + const password = import.meta.env.VITE_DEV_AUTOLOGIN_PASSWORD; + if (!username || !password || sessionStorage.getItem(SKIP_KEY)) return null; + try { + const result = await postLogin({ username, password }); + if (!("schools" in result)) return result; + const school = import.meta.env.VITE_DEV_AUTOLOGIN_SCHOOL || result.schools[0]?.slug; + return school ? await postSelectSchool({ school }) : null; + } catch (err) { + console.warn("Dev auto-login failed:", err); + return null; + } +} + +/** Called on logout so the next /auth/me 401 shows the login page. */ +export function stopDevAutoLogin(): void { + if (import.meta.env.DEV) sessionStorage.setItem(SKIP_KEY, "1"); +} diff --git a/src/stores/auth-context.tsx b/src/stores/auth-context.tsx index c38af77..436a9f5 100644 --- a/src/stores/auth-context.tsx +++ b/src/stores/auth-context.tsx @@ -12,6 +12,7 @@ import { getMe } from "@/api/users"; import { postLogout } from "@/api/auth"; import type { User } from "@/api/client"; import { ApiError } from "@/api/client"; +import { devAutoLogin, stopDevAutoLogin } from "@/lib/dev-auto-login"; import { usePreferences } from "@/stores/preferences-context"; type AuthContextValue = { @@ -50,7 +51,7 @@ export function AuthProvider(props: ParentProps) { try { return await getMe(); } catch (err) { - if (err instanceof ApiError && err.status === 401) return null; + if (err instanceof ApiError && err.status === 401) return await devAutoLogin(); setError(err); return null; } @@ -85,6 +86,7 @@ export function AuthProvider(props: ParentProps) { }); const logout = async () => { + stopDevAutoLogin(); try { await postLogout(); } finally { diff --git a/src/vite-env.d.ts b/src/vite-env.d.ts index 11f02fe..7bf481d 100644 --- a/src/vite-env.d.ts +++ b/src/vite-env.d.ts @@ -1 +1,8 @@ /// + +interface ImportMetaEnv { + /** Dev-only auto sign-in (see src/lib/dev-auto-login.ts); set in .env.local, never committed. */ + readonly VITE_DEV_AUTOLOGIN_USERNAME?: string; + readonly VITE_DEV_AUTOLOGIN_PASSWORD?: string; + readonly VITE_DEV_AUTOLOGIN_SCHOOL?: string; +}