forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits

dev-auto-login.ts32 lines, 1.3 KiBHistoryRawDownload
1import { postLogin, postSelectSchool } from "@/api/auth";
2import type { User } from "@/api/client";
3
4const 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 */
13export 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. */
30export function stopDevAutoLogin(): void {
31 if (import.meta.env.DEV) sessionStorage.setItem(SKIP_KEY, "1");
32}