8058
Ir
irvc / files
irvc / web / src / views / Login.tsx
1.3 KB
1import { useState, type FormEvent } from "react"; 2import { Button, TextInput } from "@/ui"; 3import AuthShell from "../components/AuthShell"; 4import { api } from "../lib/api"; 5import type { User } from "../lib/types"; 6 7export default function Login({ onLogin }: { onLogin: (u: User) => void }) { 8 const [error, setError] = useState(""); 9 10 async function submit(e: FormEvent<HTMLFormElement>) { 11 e.preventDefault(); 12 const form = new FormData(e.currentTarget); 13 setError(""); 14 try { 15 onLogin( 16 await api<User>("/login", { 17 method: "POST", 18 body: JSON.stringify({ 19 username: form.get("username"), 20 password: form.get("password"), 21 }), 22 }), 23 ); 24 } catch (err) { 25 setError((err as Error).message); 26 } 27 } 28 29 return ( 30 <AuthShell subtitle="sign in" onSubmit={submit}> 31 <TextInput name="username" placeholder="username" autoFocus autoComplete="username" /> 32 <TextInput 33 name="password" 34 type="password" 35 placeholder="password" 36 autoComplete="current-password" 37 /> 38 {error && <p className="px-1 text-xs text-danger">{error}</p>} 39 <Button variant="primary" size="lg" type="submit" className="w-full"> 40 enter 41 </Button> 42 </AuthShell> 43 ); 44}