import type { FormEvent, ReactNode } from "react"; import { Alert, AlertDescription, AlertIcon, Button, Center, FormControl, FormLabel, HStack, Icon, Input, Stack } from "./chakra-compat"; import { FcGoogle } from "react-icons/fc"; import type { AuthProviderAvailability, AuthProviderKey, AuthSubmitValues, LoginMode } from "./types"; type LoginFormTexts = { nameLabel: string; emailLabel: string; passwordLabel: string; passwordConfirmLabel: string; submitRegisterLabel: string; submitSignInLabel: string; toggleToRegisterLabel: string; toggleToSignInLabel: string; forgotPasswordLabel: string; googleLabel: string; slackLabel: string; }; type LoginFormProps = { mode: LoginMode; texts: LoginFormTexts; onSubmit: (values: AuthSubmitValues) => void | Promise; onModeToggle: () => void; loading?: boolean; oauthLoadingProvider?: AuthProviderKey | null; providers?: AuthProviderAvailability; onOAuthSignIn?: (provider: AuthProviderKey) => void | Promise; errorMessage?: string | null; successMessage?: string | null; footer?: ReactNode; forgotPasswordLink?: ReactNode; emailPlaceholder?: string; namePlaceholder?: string; }; export function LoginForm({ mode, texts, onSubmit, onModeToggle, loading = false, oauthLoadingProvider = null, providers, onOAuthSignIn, errorMessage, successMessage, footer, forgotPasswordLink, emailPlaceholder = "you@example.com", namePlaceholder = "Jane Doe" }: LoginFormProps) { const registerMode = mode === "register"; function handleSubmit(event: FormEvent) { event.preventDefault(); const form = new FormData(event.currentTarget); void onSubmit({ name: String(form.get("name") ?? ""), email: String(form.get("email") ?? ""), password: String(form.get("password") ?? ""), passwordConfirm: String(form.get("passwordConfirm") ?? "") }); } return ( {errorMessage ? ( {errorMessage} ) : null} {successMessage ? ( {successMessage} ) : null}
{registerMode ? ( {texts.nameLabel} ) : null} {texts.emailLabel} {texts.passwordLabel} {!registerMode && forgotPasswordLink ? {forgotPasswordLink} : null} {registerMode ? ( {texts.passwordConfirmLabel} ) : null}
{registerMode || !onOAuthSignIn || (!providers?.google && !providers?.slack) ? null : ( {providers.google ? ( ) : null} {providers.slack ? ( ) : null} )} {footer ?? null}
); }