import { Button, Divider } from "@mui/joy"; import { useEffect, useState } from "react"; import { toast } from "react-hot-toast"; import { useTranslation } from "react-i18next"; import { useGlobalStore, useUserStore } from "@/store/module"; import * as api from "@/helpers/api"; import { absolutifyLink } from "@/helpers/utils"; import useLoading from "@/hooks/useLoading"; import Icon from "@/components/Icon"; import AppearanceSelect from "@/components/AppearanceSelect"; import LocaleSelect from "@/components/LocaleSelect"; const Auth = () => { const { t } = useTranslation(); 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(""); const [password, setPassword] = useState(""); const [identityProviderList, setIdentityProviderList] = useState([]); useEffect(() => { userStore.doSignOut().catch(); const fetchIdentityProviderList = async () => { const { data: identityProviderList } = await api.getIdentityProviderList(); setIdentityProviderList(identityProviderList); }; fetchIdentityProviderList(); }, []); useEffect(() => { if (mode === "demo") { setUsername("demohero"); setPassword("secret"); } }, [mode]); 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 handleFormSubmit = (e: React.FormEvent) => { e.preventDefault(); if (systemStatus?.host) { handleSignInButtonClick(); } else { handleSignUpButtonClick(); } }; const handleSignInButtonClick = async () => { if (username === "" || password === "") { return; } if (actionBtnLoadingState.isLoading) { return; } try { actionBtnLoadingState.setLoading(); await api.signin(username, password); const user = await userStore.doSignIn(); if (user) { window.location.href = "/"; } else { toast.error(t("message.login-failed")); } } catch (error: any) { console.error(error); toast.error(error.response.data.message || t("message.login-failed")); } actionBtnLoadingState.setFinish(); }; const handleSignUpButtonClick = async () => { if (username === "" || password === "") { return; } if (actionBtnLoadingState.isLoading) { return; } try { actionBtnLoadingState.setLoading(); await api.signup(username, password); const user = await userStore.doSignIn(); if (user) { window.location.href = "/"; } else { toast.error(t("common.signup-failed")); } } catch (error: any) { console.error(error); toast.error(error.response.data.message || error.message || t("common.signup-failed")); } actionBtnLoadingState.setFinish(); }; const handleSignInWithIdentityProvider = async (identityProvider: IdentityProvider) => { const stateQueryParameter = `auth.signin.${identityProvider.name}-${identityProvider.id}`; if (identityProvider.type === "OAUTH2") { const redirectUri = absolutifyLink("/auth/callback"); const oauth2Config = identityProvider.config.oauth2Config; const authUrl = `${oauth2Config.authUrl}?client_id=${ oauth2Config.clientId }&redirect_uri=${redirectUri}&state=${stateQueryParameter}&response_type=code&scope=${encodeURIComponent( oauth2Config.scopes.join(" ") )}`; window.location.href = authUrl; } }; return (

{systemStatus.customizedProfile.name}

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

{t("common.username")}
{t("common.password")}
{actionBtnLoadingState.isLoading && } {systemStatus?.allowSignUp && ( <> / )}
{identityProviderList.length > 0 && ( <> {t("common.or")}
{identityProviderList.map((identityProvider) => ( ))}
)} {!systemStatus?.host && (

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

)}
); }; export default Auth;