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; +}