feat(login): dev-only prefill from VITE_ADMIN_EMAIL/PASSWORD
When import.meta.env.DEV is true, the login form's email and password fields are populated from the corresponding env vars. Shaves the manual re-typing during dev iteration. Production builds get empty strings regardless of build-time env values: the prefill is gated on import.meta.env.DEV (which Vite replaces with literal `false` at build time, so the surrounding ternary tree-shakes and the env values can't bleed into the prod bundle even if accidentally set in the build env). Files: - src/vite-env.d.ts (new): ImportMetaEnv augmentation with the four VITE_* vars we use (admin email/password, dev directus/processor URLs). Gives proper typing under strict mode. - src/ui/pages/login.tsx: devDefaults computed once at module scope from import.meta.env. Form's defaultValues uses it. - .env.example: documents VITE_ADMIN_EMAIL and VITE_ADMIN_PASSWORD with examples; notes the prod-ignore guarantee. - .gitignore: adds *.env (defensive — complements the existing *.local pattern). .env.example stays committable (doesn't end in .env).
This commit is contained in:
+12
-1
@@ -23,6 +23,17 @@ const LoginFormSchema = z.object({
|
||||
|
||||
type LoginForm = z.infer<typeof LoginFormSchema>;
|
||||
|
||||
/**
|
||||
* Dev-only prefill from `VITE_ADMIN_EMAIL` / `VITE_ADMIN_PASSWORD`.
|
||||
* Production builds get empty strings regardless of build-time env values.
|
||||
*/
|
||||
const devDefaults = import.meta.env.DEV
|
||||
? {
|
||||
email: import.meta.env.VITE_ADMIN_EMAIL ?? '',
|
||||
password: import.meta.env.VITE_ADMIN_PASSWORD ?? '',
|
||||
}
|
||||
: { email: '', password: '' };
|
||||
|
||||
export type LoginPageProps = {
|
||||
/** Called once the auth store transitions to `'authenticated'`. */
|
||||
onAuthenticated?: () => void;
|
||||
@@ -35,7 +46,7 @@ export function LoginPage({ onAuthenticated }: LoginPageProps) {
|
||||
|
||||
const form = useForm<LoginForm>({
|
||||
resolver: zodResolver(LoginFormSchema),
|
||||
defaultValues: { email: '', password: '' },
|
||||
defaultValues: devDefaults,
|
||||
});
|
||||
|
||||
// If the auth store flips to authenticated (e.g. login succeeds, or another
|
||||
|
||||
Vendored
+16
@@ -0,0 +1,16 @@
|
||||
/// <reference types="vite/client" />
|
||||
|
||||
interface ImportMetaEnv {
|
||||
/** Local-dev convenience: prefill the login form's email field. Only consumed when import.meta.env.DEV. */
|
||||
readonly VITE_ADMIN_EMAIL?: string;
|
||||
/** Local-dev convenience: prefill the login form's password field. Only consumed when import.meta.env.DEV. */
|
||||
readonly VITE_ADMIN_PASSWORD?: string;
|
||||
/** Override the dev proxy's Directus target. See vite.config.ts. */
|
||||
readonly VITE_DEV_DIRECTUS_URL?: string;
|
||||
/** Override the dev proxy's Processor WS target. See vite.config.ts. */
|
||||
readonly VITE_DEV_PROCESSOR_WS_URL?: string;
|
||||
}
|
||||
|
||||
interface ImportMeta {
|
||||
readonly env: ImportMetaEnv;
|
||||
}
|
||||
Reference in New Issue
Block a user