import { Button, Divider } from "@mui/joy"; import { useEffect, useState } from "react"; 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 toastHelper from "../components/Toast"; import AppearanceSelect from "../components/AppearanceSelect"; import LocaleSelect from "../components/LocaleSelect"; import "../less/auth.less"; 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(mode === "demo" ? "demohero" : ""); const [password, setPassword] = useState(mode === "demo" ? "secret" : ""); const [identityProviderList, setIdentityProviderList] = useState([]); useEffect(() => { userStore.doSignOut().catch(); const fetchIdentityProviderList = async () => { const { data: { data: identityProviderList }, } = await api.getIdentityProviderList(); setIdentityProviderList(identityProviderList); }; fetchIdentityProviderList(); }, []); 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; } try { actionBtnLoadingState.setLoading(); await api.signin(username, password); const user = await userStore.doSignIn(); if (user) { window.location.href = "/"; } else { toastHelper.error(t("message.login-failed")); } } catch (error: any) { console.error(error); toastHelper.error(error.response.data.error); } actionBtnLoadingState.setFinish(); }; const handleSignUpBtnsClick = async () => { if (actionBtnLoadingState.isLoading) { return; } try { actionBtnLoadingState.setLoading(); await api.signup(username, password); const user = await userStore.doSignIn(); if (user) { window.location.href = "/"; } else { toastHelper.error(t("common.singup-failed")); } } catch (error: any) { console.error(error); toastHelper.error(error.response.data.error); } 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("slogan")}

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

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

}
); }; export default Auth;