🔑 optional auto sign-in for local development
Browse files at this commit13b80a225a4fc69c07c7769b6968a9e2ff35a484Parent: 57a1d1a- chore
- 🔑 optional auto sign-in for local development
4 changed files +44 -1
- README.md +2 -0
- src/lib/dev-auto-login.ts +32 -0
- src/stores/auth-context.tsx +3 -1
- src/vite-env.d.ts +7 -0
README.mdmodified+2 -0
| @@ -53,6 +53,8 @@ Open [http://localhost:5173](http://localhost:5173) in your browser. | |||
| 53 | 53 | ||
| 54 | > **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). | 54 | > **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). |
| 55 | 55 | ||
| 56 | +> **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/`. | ||
| 57 | + | ||
| 56 | --- | 58 | --- |
| 57 | 59 | ||
| 58 | ## 🐳 Run in a container (podman) | 60 | ## 🐳 Run in a container (podman) |
src/lib/dev-auto-login.tsadded+32 -0
| @@ -0,0 +1,32 @@ | |||
| 1 | +import { postLogin, postSelectSchool } from "@/api/auth"; | ||
| 2 | +import type { User } from "@/api/client"; | ||
| 3 | + | ||
| 4 | +const SKIP_KEY = "hezarfen:dev-auto-login-off"; | ||
| 5 | + | ||
| 6 | +/** | ||
| 7 | + * Development-only sign-in with credentials from `.env.local` | ||
| 8 | + * (`VITE_DEV_AUTOLOGIN_USERNAME`, `VITE_DEV_AUTOLOGIN_PASSWORD`, optional | ||
| 9 | + * `VITE_DEV_AUTOLOGIN_SCHOOL`). `import.meta.env.DEV` is a build-time false in | ||
| 10 | + * `vite build`, so the whole body is dropped from production bundles. | ||
| 11 | + * An explicit logout turns it off for the rest of the tab. | ||
| 12 | + */ | ||
| 13 | +export async function devAutoLogin(): Promise<User | null> { | ||
| 14 | + if (!import.meta.env.DEV) return null; | ||
| 15 | + const username = import.meta.env.VITE_DEV_AUTOLOGIN_USERNAME; | ||
| 16 | + const password = import.meta.env.VITE_DEV_AUTOLOGIN_PASSWORD; | ||
| 17 | + if (!username || !password || sessionStorage.getItem(SKIP_KEY)) return null; | ||
| 18 | + try { | ||
| 19 | + const result = await postLogin({ username, password }); | ||
| 20 | + if (!("schools" in result)) return result; | ||
| 21 | + const school = import.meta.env.VITE_DEV_AUTOLOGIN_SCHOOL || result.schools[0]?.slug; | ||
| 22 | + return school ? await postSelectSchool({ school }) : null; | ||
| 23 | + } catch (err) { | ||
| 24 | + console.warn("Dev auto-login failed:", err); | ||
| 25 | + return null; | ||
| 26 | + } | ||
| 27 | +} | ||
| 28 | + | ||
| 29 | +/** Called on logout so the next /auth/me 401 shows the login page. */ | ||
| 30 | +export function stopDevAutoLogin(): void { | ||
| 31 | + if (import.meta.env.DEV) sessionStorage.setItem(SKIP_KEY, "1"); | ||
| 32 | +} | ||
src/stores/auth-context.tsxmodified+3 -1
| @@ -12,6 +12,7 @@ import { getMe } from "@/api/users"; | |||
| 12 | import { postLogout } from "@/api/auth"; | 12 | import { postLogout } from "@/api/auth"; |
| 13 | import type { User } from "@/api/client"; | 13 | import type { User } from "@/api/client"; |
| 14 | import { ApiError } from "@/api/client"; | 14 | import { ApiError } from "@/api/client"; |
| 15 | +import { devAutoLogin, stopDevAutoLogin } from "@/lib/dev-auto-login"; | ||
| 15 | import { usePreferences } from "@/stores/preferences-context"; | 16 | import { usePreferences } from "@/stores/preferences-context"; |
| 16 | 17 | ||
| 17 | type AuthContextValue = { | 18 | type AuthContextValue = { |
| @@ -50,7 +51,7 @@ export function AuthProvider(props: ParentProps) { | |||
| 50 | try { | 51 | try { |
| 51 | return await getMe(); | 52 | return await getMe(); |
| 52 | } catch (err) { | 53 | } catch (err) { |
| 53 | - if (err instanceof ApiError && err.status === 401) return null; | 54 | + if (err instanceof ApiError && err.status === 401) return await devAutoLogin(); |
| 54 | setError(err); | 55 | setError(err); |
| 55 | return null; | 56 | return null; |
| 56 | } | 57 | } |
| @@ -85,6 +86,7 @@ export function AuthProvider(props: ParentProps) { | |||
| 85 | }); | 86 | }); |
| 86 | 87 | ||
| 87 | const logout = async () => { | 88 | const logout = async () => { |
| 89 | + stopDevAutoLogin(); | ||
| 88 | try { | 90 | try { |
| 89 | await postLogout(); | 91 | await postLogout(); |
| 90 | } finally { | 92 | } finally { |
src/vite-env.d.tsmodified+7 -0
| @@ -1 +1,8 @@ | |||
| 1 | /// <reference types="vite/client" /> | 1 | /// <reference types="vite/client" /> |
| 2 | + | ||
| 3 | +interface ImportMetaEnv { | ||
| 4 | + /** Dev-only auto sign-in (see src/lib/dev-auto-login.ts); set in .env.local, never committed. */ | ||
| 5 | + readonly VITE_DEV_AUTOLOGIN_USERNAME?: string; | ||
| 6 | + readonly VITE_DEV_AUTOLOGIN_PASSWORD?: string; | ||
| 7 | + readonly VITE_DEV_AUTOLOGIN_SCHOOL?: string; | ||
| 8 | +} | ||