hezarfen_frontend
Clone
https://git.tahinli.com/Hezarfen/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits
| 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`, a school uuid). `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]?.id; |
| 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 | } |