"use client"; import Link from "next/link"; import { useRouter } from "next/navigation"; import { type FormEvent, useState } from "react"; import { useTranslation } from "react-i18next"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "@/components/ui/card"; import { Field, FieldDescription, FieldLabel } from "@/components/ui/field"; import { Input } from "@/components/ui/input"; import { Tabs, TabsList, TabsTab } from "@/components/ui/tabs"; import { authClient } from "@/lib/auth-client"; import { notify } from "@/lib/toast"; import { cn } from "@/lib/utils"; type Mode = "email" | "username"; export function LoginForm({ className, ...props }: React.ComponentProps<"div">) { const { t } = useTranslation(); const router = useRouter(); // Staff provisioned by an admin sign in with a username; clinic owners sign in // with the email they signed up with. The tab picks which credential to use. const [mode, setMode] = useState("email"); const [identifier, setIdentifier] = useState(""); const [password, setPassword] = useState(""); const [error, setError] = useState(null); const [submitting, setSubmitting] = useState(false); const onSubmit = async (event: FormEvent) => { event.preventDefault(); if (submitting) return; setSubmitting(true); setError(null); const callbackURL = `${window.location.origin}/`; const { error: err } = mode === "email" ? await authClient.signIn.email({ email: identifier.trim(), password, callbackURL, }) : await authClient.signIn.username({ username: identifier.trim(), password, callbackURL, }); if (err) { const message = err.message ?? t("auth.login.error"); setError(message); notify.error(t("auth.login.errorTitle"), message); setSubmitting(false); return; } notify.success(t("auth.login.welcomeBack")); router.push("/"); }; return (
{t("auth.login.title")} {t("auth.login.subtitle")}
{error && (

{error}

)} { setMode(value as Mode); setError(null); }} value={mode} > {t("auth.login.tabEmail")} {t("auth.login.tabUsername")} {mode === "email" ? t("auth.login.emailLabel") : t("auth.login.usernameLabel")} setIdentifier(e.target.value)} placeholder={ mode === "email" ? t("auth.login.emailPlaceholder") : t("auth.login.usernamePlaceholder") } required type={mode === "email" ? "email" : "text"} value={identifier} />
{t("auth.login.passwordLabel")} {t("auth.login.forgotPassword")}
setPassword(e.target.value)} required type="password" value={password} />
{t("auth.login.noAccount")}{" "} {t("auth.login.signUpLink")}
); }