import { Button, Input } from "@usememos/mui"; import { LoaderIcon } from "lucide-react"; import { observer } from "mobx-react-lite"; import { ClientError } from "nice-grpc-web"; import { useState } from "react"; import { toast } from "react-hot-toast"; import { authServiceClient } from "@/grpcweb"; import useLoading from "@/hooks/useLoading"; import useNavigateTo from "@/hooks/useNavigateTo"; import { workspaceStore } from "@/store/v2"; import { initialUserStore } from "@/store/v2/user"; import { useTranslate } from "@/utils/i18n"; const PasswordSignInForm = observer(() => { const t = useTranslate(); const navigateTo = useNavigateTo(); const actionBtnLoadingState = useLoading(false); const [username, setUsername] = useState(workspaceStore.state.profile.mode === "demo" ? "yourselfhosted" : ""); const [password, setPassword] = useState(workspaceStore.state.profile.mode === "demo" ? "yourselfhosted" : ""); const handleUsernameInputChanged = (e: React.ChangeEvent) => { const text = e.target.value as string; setUsername(text); }; const handlePasswordInputChanged = (e: React.ChangeEvent) => { const text = e.target.value as string; setPassword(text); }; const handleFormSubmit = (e: React.FormEvent) => { e.preventDefault(); handleSignInButtonClick(); }; const handleSignInButtonClick = async () => { if (username === "" || password === "") { return; } if (actionBtnLoadingState.isLoading) { return; } try { actionBtnLoadingState.setLoading(); await authServiceClient.createSession({ passwordCredentials: { username, password }, }); await initialUserStore(); navigateTo("/"); } catch (error: any) { console.error(error); toast.error((error as ClientError).details || "Failed to sign in."); } actionBtnLoadingState.setFinish(); }; return (
{t("common.username")}
{t("common.password")}
); }); export default PasswordSignInForm;