Files
spa/src/ui/pages/login.tsx
T
julian 8d0bc2bb1e 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).
2026-05-02 18:48:04 +02:00

131 lines
4.2 KiB
TypeScript

import { useEffect, useState } from 'react';
import { useForm } from 'react-hook-form';
import { zodResolver } from '@hookform/resolvers/zod';
import { z } from 'zod';
import { useAuthStore } from '@/auth';
import { Button } from '@/ui/primitives/button';
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/ui/primitives/card';
import { Alert, AlertDescription } from '@/ui/primitives/alert';
import {
Form,
FormControl,
FormField,
FormItem,
FormLabel,
FormMessage,
} from '@/ui/primitives/form';
import { Input } from '@/ui/primitives/input';
const LoginFormSchema = z.object({
email: z.string().email({ message: 'Enter a valid email address.' }),
password: z.string().min(1, { message: 'Password is required.' }),
});
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;
};
export function LoginPage({ onAuthenticated }: LoginPageProps) {
const status = useAuthStore((s) => s.status);
const isSubmitting = status === 'authenticating';
const [submitError, setSubmitError] = useState<string | null>(null);
const form = useForm<LoginForm>({
resolver: zodResolver(LoginFormSchema),
defaultValues: devDefaults,
});
// If the auth store flips to authenticated (e.g. login succeeds, or another
// tab logs in concurrently), notify the surrounding container.
useEffect(() => {
if (status === 'authenticated') {
onAuthenticated?.();
}
}, [status, onAuthenticated]);
async function onSubmit(values: LoginForm) {
setSubmitError(null);
const result = await useAuthStore.getState().login(values.email, values.password);
if (!result.ok) {
setSubmitError(result.error);
}
// Success path is handled by the status effect above.
}
return (
<div className="min-h-screen grid place-items-center bg-muted p-4">
<Card className="w-full max-w-sm">
<CardHeader>
<CardTitle>Sign in to TRM</CardTitle>
<CardDescription>Use your operator credentials.</CardDescription>
</CardHeader>
<CardContent>
{submitError && (
<Alert variant="destructive" className="mb-4">
<AlertDescription>{submitError}</AlertDescription>
</Alert>
)}
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4">
<FormField
control={form.control}
name="email"
render={({ field }) => (
<FormItem>
<FormLabel>Email</FormLabel>
<FormControl>
<Input
type="email"
autoComplete="username"
autoFocus
disabled={isSubmitting}
{...field}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="password"
render={({ field }) => (
<FormItem>
<FormLabel>Password</FormLabel>
<FormControl>
<Input
type="password"
autoComplete="current-password"
disabled={isSubmitting}
{...field}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<Button type="submit" className="w-full" disabled={isSubmitting}>
{isSubmitting ? 'Signing in…' : 'Sign in'}
</Button>
</form>
</Form>
</CardContent>
</Card>
</div>
);
}