refactor: social buttons
This commit is contained in:
@@ -1,12 +1,19 @@
|
|||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import { useNavigate } from "react-router-dom";
|
import { useNavigate } from "react-router-dom";
|
||||||
|
|
||||||
|
type Props = {
|
||||||
export default function MagicLinkLogin() {
|
smtp?: boolean
|
||||||
|
}
|
||||||
|
export default function MagicLinkLogin({ smtp = false }: Props) {
|
||||||
const { t } = useTranslation("auth");
|
const { t } = useTranslation("auth");
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const handleSignUp = () => {
|
const handleSignUp = () => {
|
||||||
|
if (smtp) {
|
||||||
|
// 配置了SMTP,走magic link 流程
|
||||||
|
navigate("/send_magic_link");
|
||||||
|
} else {
|
||||||
navigate("/register");
|
navigate("/register");
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -0,0 +1,47 @@
|
|||||||
|
import { useEffect } from 'react';
|
||||||
|
import toast from 'react-hot-toast';
|
||||||
|
import { useGetLoginConfigQuery } from "../../app/services/server";
|
||||||
|
import useGithubAuthConfig from "../../common/hook/useGithubAuthConfig";
|
||||||
|
import useGoogleAuthConfig from "../../common/hook/useGoogleAuthConfig";
|
||||||
|
import GoogleLoginButton from "../../common/component/GoogleLoginButton";
|
||||||
|
import GithubLoginButton from "../../common/component/GithubLoginButton";
|
||||||
|
import MetamaskLoginButton from "./MetamaskLoginButton";
|
||||||
|
import OidcLoginButton from "./OidcLoginButton";
|
||||||
|
import { useLoginMutation } from '../../app/services/auth';
|
||||||
|
import { LoginConfig } from '../../types/server';
|
||||||
|
|
||||||
|
// type Props = {}
|
||||||
|
|
||||||
|
const SocialLoginButtons = () => {
|
||||||
|
const [login, { isSuccess }] = useLoginMutation();
|
||||||
|
const { config: githubAuthConfig } = useGithubAuthConfig();
|
||||||
|
const { data: loginConfig, isSuccess: loginConfigSuccess } = useGetLoginConfigQuery();
|
||||||
|
const { clientId } = useGoogleAuthConfig();
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (isSuccess) {
|
||||||
|
toast.success("Login Successfully");
|
||||||
|
// navigateTo("/");
|
||||||
|
}
|
||||||
|
}, [isSuccess]);
|
||||||
|
if (!loginConfigSuccess) return null;
|
||||||
|
const {
|
||||||
|
github: enableGithubLogin,
|
||||||
|
google: enableGoogleLogin,
|
||||||
|
metamask: enableMetamaskLogin,
|
||||||
|
oidc = [],
|
||||||
|
} = loginConfig as LoginConfig;
|
||||||
|
const googleLogin = enableGoogleLogin && !!clientId;
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
{googleLogin && <GoogleLoginButton type="register" clientId={clientId} />}
|
||||||
|
{enableGithubLogin && (
|
||||||
|
<GithubLoginButton type="register" client_id={githubAuthConfig?.client_id} />
|
||||||
|
)}
|
||||||
|
{enableMetamaskLogin && <MetamaskLoginButton login={login} />}
|
||||||
|
{oidc.length > 0 && <OidcLoginButton issuers={oidc} />}
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default SocialLoginButtons;
|
||||||
@@ -4,8 +4,7 @@ import toast from "react-hot-toast";
|
|||||||
import BASE_URL from "../../app/config";
|
import BASE_URL from "../../app/config";
|
||||||
// import web3 from "web3";
|
// import web3 from "web3";
|
||||||
import StyledWrapper from "./styled";
|
import StyledWrapper from "./styled";
|
||||||
import MetamaskLoginButton from "./MetamaskLoginButton";
|
|
||||||
import OidcLoginButton from "./OidcLoginButton";
|
|
||||||
import Input from "../../common/component/styled/Input";
|
import Input from "../../common/component/styled/Input";
|
||||||
import Button from "../../common/component/styled/Button";
|
import Button from "../../common/component/styled/Button";
|
||||||
import MagicLinkLogin from "./MagicLinkLogin";
|
import MagicLinkLogin from "./MagicLinkLogin";
|
||||||
@@ -13,18 +12,15 @@ import SignUpLink from "./SignUpLink";
|
|||||||
import { useLoginMutation } from "../../app/services/auth";
|
import { useLoginMutation } from "../../app/services/auth";
|
||||||
import { useGetLoginConfigQuery, useGetSMTPStatusQuery } from "../../app/services/server";
|
import { useGetLoginConfigQuery, useGetSMTPStatusQuery } from "../../app/services/server";
|
||||||
import useGoogleAuthConfig from "../../common/hook/useGoogleAuthConfig";
|
import useGoogleAuthConfig from "../../common/hook/useGoogleAuthConfig";
|
||||||
import useGithubAuthConfig from "../../common/hook/useGithubAuthConfig";
|
|
||||||
import GoogleLoginButton from "../../common/component/GoogleLoginButton";
|
|
||||||
import GithubLoginButton from "../../common/component/GithubLoginButton";
|
|
||||||
import { FetchBaseQueryError } from "@reduxjs/toolkit/dist/query";
|
import { FetchBaseQueryError } from "@reduxjs/toolkit/dist/query";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
|
import SocialLoginButtons from "./SocialLoginButtons";
|
||||||
|
|
||||||
export default function LoginPage() {
|
export default function LoginPage() {
|
||||||
const { t } = useTranslation("auth");
|
const { t } = useTranslation("auth");
|
||||||
const { data: enableSMTP } = useGetSMTPStatusQuery();
|
const { data: enableSMTP } = useGetSMTPStatusQuery();
|
||||||
const [login, { isSuccess, isLoading, error }] = useLoginMutation();
|
const [login, { isSuccess, isLoading, error }] = useLoginMutation();
|
||||||
const { clientId } = useGoogleAuthConfig();
|
const { clientId } = useGoogleAuthConfig();
|
||||||
const { config: githubAuthConfig } = useGithubAuthConfig();
|
|
||||||
const { data: loginConfig, isSuccess: loginConfigSuccess } = useGetLoginConfigQuery();
|
const { data: loginConfig, isSuccess: loginConfigSuccess } = useGetLoginConfigQuery();
|
||||||
const [input, setInput] = useState({
|
const [input, setInput] = useState({
|
||||||
email: "",
|
email: "",
|
||||||
@@ -162,12 +158,10 @@ export default function LoginPage() {
|
|||||||
<div className="btns">
|
<div className="btns">
|
||||||
|
|
||||||
{enableMagicLink && <MagicLinkLogin />}
|
{enableMagicLink && <MagicLinkLogin />}
|
||||||
{googleLogin && <GoogleLoginButton clientId={clientId} />}
|
|
||||||
{enableGithubLogin && <GithubLoginButton client_id={githubAuthConfig?.client_id} />}
|
<SocialLoginButtons />
|
||||||
{enableMetamaskLogin && <MetamaskLoginButton login={login} />}
|
|
||||||
{oidc.length > 0 && <OidcLoginButton issuers={oidc} />}
|
|
||||||
</div>
|
</div>
|
||||||
{whoCanSignUp === "EveryOne" && <SignUpLink />}
|
{whoCanSignUp === "EveryOne" && <SignUpLink smtp={enableSMTP} />}
|
||||||
</div>
|
</div>
|
||||||
</StyledWrapper>
|
</StyledWrapper>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import Button from "../../common/component/styled/Button";
|
|||||||
|
|
||||||
export const StyledSocialButton = styled(Button)`
|
export const StyledSocialButton = styled(Button)`
|
||||||
width: 100%;
|
width: 100%;
|
||||||
margin-bottom: 16px;
|
/* margin-bottom: 16px; */
|
||||||
display: flex;
|
display: flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
justify-content: center;
|
justify-content: center;
|
||||||
@@ -63,6 +63,7 @@ const StyledWrapper = styled.div`
|
|||||||
height: 1px;
|
height: 1px;
|
||||||
background-color: #e4e7ec;
|
background-color: #e4e7ec;
|
||||||
margin: 26px 0;
|
margin: 26px 0;
|
||||||
|
overflow: visible;
|
||||||
&:after {
|
&:after {
|
||||||
padding: 4px;
|
padding: 4px;
|
||||||
background-color: #fff;
|
background-color: #fff;
|
||||||
|
|||||||
@@ -6,12 +6,10 @@ import Button from "../../common/component/styled/Button";
|
|||||||
import { useLazyCheckEmailQuery, useSendRegMagicLinkMutation } from "../../app/services/auth";
|
import { useLazyCheckEmailQuery, useSendRegMagicLinkMutation } from "../../app/services/auth";
|
||||||
import EmailNextTip from "./EmailNextStepTip";
|
import EmailNextTip from "./EmailNextStepTip";
|
||||||
import SignInLink from "./SignInLink";
|
import SignInLink from "./SignInLink";
|
||||||
import { useGetLoginConfigQuery } from "../../app/services/server";
|
|
||||||
import useGithubAuthConfig from "../../common/hook/useGithubAuthConfig";
|
|
||||||
import useGoogleAuthConfig from "../../common/hook/useGoogleAuthConfig";
|
|
||||||
import GoogleLoginButton from "../../common/component/GoogleLoginButton";
|
|
||||||
import GithubLoginButton from "../../common/component/GithubLoginButton";
|
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
|
import { useGetLoginConfigQuery } from "../../app/services/server";
|
||||||
|
import SocialLoginButtons from "../login/SocialLoginButtons";
|
||||||
|
|
||||||
interface AuthForm {
|
interface AuthForm {
|
||||||
email: string;
|
email: string;
|
||||||
@@ -24,6 +22,8 @@ export default function Reg() {
|
|||||||
const [sendRegMagicLink, { isLoading: signingUp, data, isSuccess }] =
|
const [sendRegMagicLink, { isLoading: signingUp, data, isSuccess }] =
|
||||||
useSendRegMagicLinkMutation();
|
useSendRegMagicLinkMutation();
|
||||||
const [checkEmail, { isLoading: checkingEmail }] = useLazyCheckEmailQuery();
|
const [checkEmail, { isLoading: checkingEmail }] = useLazyCheckEmailQuery();
|
||||||
|
const { data: loginConfig, isSuccess: loginConfigSuccess } = useGetLoginConfigQuery();
|
||||||
|
|
||||||
// const navigateTo = useNavigate();
|
// const navigateTo = useNavigate();
|
||||||
const [magicToken, setMagicToken] = useState("");
|
const [magicToken, setMagicToken] = useState("");
|
||||||
const [input, setInput] = useState<AuthForm>({
|
const [input, setInput] = useState<AuthForm>({
|
||||||
@@ -87,17 +87,10 @@ export default function Reg() {
|
|||||||
return { ...prev };
|
return { ...prev };
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const { clientId } = useGoogleAuthConfig();
|
|
||||||
const { config: githubAuthConfig } = useGithubAuthConfig();
|
|
||||||
const { data: loginConfig, isSuccess: loginConfigSuccess } = useGetLoginConfigQuery();
|
|
||||||
if (!loginConfigSuccess) return null;
|
if (!loginConfigSuccess) return null;
|
||||||
const {
|
const {
|
||||||
github: enableGithubLogin,
|
|
||||||
google: enableGoogleLogin,
|
|
||||||
who_can_sign_up: whoCanSignUp
|
who_can_sign_up: whoCanSignUp
|
||||||
} = loginConfig;
|
} = loginConfig;
|
||||||
const googleLogin = enableGoogleLogin && clientId;
|
|
||||||
// magic token 没有并且没有开放注册
|
// magic token 没有并且没有开放注册
|
||||||
if (whoCanSignUp !== "EveryOne" && !magicToken)
|
if (whoCanSignUp !== "EveryOne" && !magicToken)
|
||||||
// todo: i18n
|
// todo: i18n
|
||||||
@@ -153,11 +146,8 @@ export default function Reg() {
|
|||||||
</Button>
|
</Button>
|
||||||
</form>
|
</form>
|
||||||
<hr className="or" />
|
<hr className="or" />
|
||||||
<div className="flex flex-col gap-3">
|
<div className="flex flex-col gap-3 py-3">
|
||||||
{googleLogin && <GoogleLoginButton type="register" clientId={clientId} />}
|
<SocialLoginButtons />
|
||||||
{enableGithubLogin && (
|
|
||||||
<GithubLoginButton type="register" client_id={githubAuthConfig?.client_id} />
|
|
||||||
)}
|
|
||||||
</div>
|
</div>
|
||||||
<SignInLink token={magicToken} />
|
<SignInLink token={magicToken} />
|
||||||
</>
|
</>
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import Button from "../../common/component/styled/Button";
|
|||||||
import { useSendLoginMagicLinkMutation } from "../../app/services/auth";
|
import { useSendLoginMagicLinkMutation } from "../../app/services/auth";
|
||||||
import SentTip from "./SentTip";
|
import SentTip from "./SentTip";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
|
import SocialLoginButtons from "../login/SocialLoginButtons";
|
||||||
|
|
||||||
export default function SendMagicLinkPage() {
|
export default function SendMagicLinkPage() {
|
||||||
const { t } = useTranslation("auth");
|
const { t } = useTranslation("auth");
|
||||||
@@ -91,6 +92,9 @@ export default function SendMagicLinkPage() {
|
|||||||
</Button>
|
</Button>
|
||||||
</form>
|
</form>
|
||||||
<hr className="or" />
|
<hr className="or" />
|
||||||
|
<div className="flex flex-col gap-3 py-3">
|
||||||
|
<SocialLoginButtons />
|
||||||
|
</div>
|
||||||
<Button onClick={handlePwdPath}>{t("login.password")}</Button>
|
<Button onClick={handlePwdPath}>{t("login.password")}</Button>
|
||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
|
|||||||
Reference in New Issue
Block a user