refactor: social login buttons

This commit is contained in:
Tristan Yang
2022-06-22 12:04:14 +08:00
parent b3b6cd5d6f
commit 3e23c72b6d
7 changed files with 25 additions and 18 deletions
+1 -1
View File
@@ -84,7 +84,7 @@ const baseQueryWithTokenCheck = async (args, api, extraOptions) => {
break;
case 500:
{
toast.error(result.error.data || "server error");
toast.error(result.error.data || "Server Error");
}
break;
case 401:
+7 -6
View File
@@ -1,5 +1,3 @@
// import { useState, useEffect } from "react";
// import { useGoogleLogin } from "react-google-login";
import IconGithub from "../../assets/icons/github.svg";
import styled from "styled-components";
import Button from "./styled/Button";
@@ -20,8 +18,12 @@ const StyledSocialButton = styled(Button)`
}
`;
export default function GithubLoginButton({ config = {} }) {
const { client_id } = config;
type Props = {
client_id: string;
type?: "login" | "register";
};
export default function GithubLoginButton({ type = "login", client_id }: Props) {
const handleGithubLogin = () => {
location.href = `http://github.com/login/oauth/authorize?client_id=${client_id}`;
// console.log("github login");
@@ -30,8 +32,7 @@ export default function GithubLoginButton({ config = {} }) {
return (
<StyledSocialButton onClick={handleGithubLogin}>
<IconGithub className="icon" />
Sign in with Github
{/* {loaded ? `Sign in with Github` : `Initailizing`} */}
{` ${type === "login" ? "Sign in" : "Sign up"} with Github`}
</StyledSocialButton>
);
}
+3 -2
View File
@@ -24,9 +24,10 @@ const StyledSocialButton = styled(Button)`
interface Props {
clientId: string;
type?: "login" | "register";
}
const GoogleLoginButton: FC<Props> = ({ clientId }) => {
const GoogleLoginButton: FC<Props> = ({ type = "login", clientId }) => {
const [login, { isSuccess, isLoading }] = useLoginMutation();
const { signIn, loaded } = useGoogleLogin({
@@ -60,7 +61,7 @@ const GoogleLoginButton: FC<Props> = ({ clientId }) => {
return (
<StyledSocialButton disabled={!loaded || isLoading} onClick={handleGoogleLogin}>
<img className="icon" src={googleSvg} alt="google icon" />
{loaded ? `Sign in with Google` : `Initailizing`}
{loaded ? `${type === "login" ? "Sign in" : "Sign up"} with Google` : `Initializing`}
</StyledSocialButton>
);
};
+9 -4
View File
@@ -84,13 +84,18 @@ export default function LoginPage() {
toast.error(error.data);
break;
case 401:
toast.error("username or password incorrect");
toast.error("Username or Password incorrect");
break;
case 404:
toast.error("account not exsit");
toast.error("Account not exist");
break;
case 410:
toast.error(
"No associated account found, please contact admin for an invitation link to join."
);
break;
default:
toast.error("something error");
toast.error("Something Error");
break;
}
return;
@@ -173,7 +178,7 @@ export default function LoginPage() {
{hasDivider && <hr className="or" />}
{enableMagicLink && <MagicLinkLogin />}
{googleLogin && <GoogleLoginButton clientId={clientId} />}
{enableGithubLogin && <GithubLoginButton config={githubAuthConfig} />}
{enableGithubLogin && <GithubLoginButton client_id={githubAuthConfig?.client_id} />}
{enableMetamaskLogin && <MetamaskLoginButton login={login} />}
{oidc.length > 0 && <OidcLoginButton issuers={oidc} />}
{whoCanSignUp === "EveryOne" && <SignUpLink />}
+4 -2
View File
@@ -139,8 +139,10 @@ export default function Reg() {
</Button>
</form>
<hr className="or" />
{googleLogin && <GoogleLoginButton clientId={clientId} />}
{enableGithubLogin && <GithubLoginButton config={githubAuthConfig} />}
{googleLogin && <GoogleLoginButton type="register" clientId={clientId} />}
{enableGithubLogin && (
<GithubLoginButton type="register" client_id={githubAuthConfig?.client_id} />
)}
<SignInLink />
</>
);
-2
View File
@@ -1,6 +1,4 @@
import { Outlet } from "react-router-dom";
// import { useMatch } from "react-router-dom";
import StyledWrapper from "./styled";
export default function RegContainer() {