import { useEffect, useState } from "react"; import { useTranslation } from "react-i18next"; import { useNavigate } from "react-router-dom"; import { useGlobalStore, useUserStore } from "../store/module"; import * as api from "../helpers/api"; import { validate, ValidatorConfig } from "../helpers/validator"; import useLoading from "../hooks/useLoading"; import Icon from "../components/Icon"; import toastHelper from "../components/Toast"; import AppearanceSelect from "../components/AppearanceSelect"; import LocaleSelect from "../components/LocaleSelect"; import "../less/auth.less"; const validateConfig: ValidatorConfig = { minLength: 4, maxLength: 320, noSpace: true, noChinese: true, }; const Auth = () => { const { t } = useTranslation(); const navigate = useNavigate(); const globalStore = useGlobalStore(); const userStore = useUserStore(); const actionBtnLoadingState = useLoading(false); const { appearance, locale, systemStatus } = globalStore.state; const mode = systemStatus.profile.mode; const [username, setUsername] = useState(mode === "dev" ? "demohero" : ""); const [password, setPassword] = useState(mode === "dev" ? "secret" : ""); useEffect(() => { userStore.doSignOut().catch(); }, []); 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 handleLocaleSelectChange = (locale: Locale) => { globalStore.setLocale(locale); }; const handleAppearanceSelectChange = (appearance: Appearance) => { globalStore.setAppearance(appearance); }; const handleSignInBtnClick = async () => { if (actionBtnLoadingState.isLoading) { return; } const usernameValidResult = validate(username, validateConfig); if (!usernameValidResult.result) { toastHelper.error(t("common.username") + ": " + t(usernameValidResult.reason as string)); return; } const passwordValidResult = validate(password, validateConfig); if (!passwordValidResult.result) { toastHelper.error(t("common.password") + ": " + t(passwordValidResult.reason as string)); return; } try { actionBtnLoadingState.setLoading(); await api.signin(username, password); const user = await userStore.doSignIn(); if (user) { navigate("/"); } else { toastHelper.error(t("message.login-failed")); } } catch (error: any) { console.error(error); toastHelper.error(error.response.data.error); } actionBtnLoadingState.setFinish(); }; const handleSignUpBtnsClick = async (role: UserRole) => { if (actionBtnLoadingState.isLoading) { return; } const usernameValidResult = validate(username, validateConfig); if (!usernameValidResult.result) { toastHelper.error(t("common.username") + ": " + t(usernameValidResult.reason as string)); return; } const passwordValidResult = validate(password, validateConfig); if (!passwordValidResult.result) { toastHelper.error(t("common.password") + ": " + t(passwordValidResult.reason as string)); return; } try { actionBtnLoadingState.setLoading(); await api.signup(username, password, role); const user = await userStore.doSignIn(); if (user) { navigate("/"); } else { toastHelper.error(t("common.singup-failed")); } } catch (error: any) { console.error(error); toastHelper.error(error.response.data.error); } actionBtnLoadingState.setFinish(); }; const handleSignInKeyUp = (e: React.KeyboardEvent) => { if (e.key === "Enter") { handleSignInBtnClick(); } }; return (

{systemStatus.customizedProfile.name}

{systemStatus.customizedProfile.description || t("slogan")}

{t("common.username")}
{t("common.password")}
{systemStatus?.host ? ( <> {actionBtnLoadingState.isLoading && } {systemStatus?.allowSignUp && ( <> / )} ) : ( <> )}
{!systemStatus?.host &&

{t("auth.host-tip")}

}
); }; export default Auth;