fix: magic link login while invite only

This commit is contained in:
Tristan Yang
2022-06-22 22:21:30 +08:00
parent 3e23c72b6d
commit cce4778c3a
5 changed files with 37 additions and 13 deletions
+27 -1
View File
@@ -1,6 +1,11 @@
import { useEffect } from "react";
import IconGithub from "../../assets/icons/github.svg";
import styled from "styled-components";
import { KEY_LOCAL_MAGIC_TOKEN } from "../../app/config";
import Button from "./styled/Button";
import { useLoginMutation } from "../../app/services/auth";
import toast from "react-hot-toast";
const StyledSocialButton = styled(Button)`
width: 100%;
@@ -24,13 +29,34 @@ type Props = {
};
export default function GithubLoginButton({ type = "login", client_id }: Props) {
//拿本地存的magic token
const magic_token = localStorage.getItem(KEY_LOCAL_MAGIC_TOKEN);
const [login, { isLoading, isSuccess }] = useLoginMutation();
useEffect(() => {
const query = new URLSearchParams(location.search);
const isGithub = query.get("oauth") === "github";
const code = query.get("code");
if (isGithub && code) {
login({
magic_token,
code: code,
type: "github"
});
}
}, []);
useEffect(() => {
if (isSuccess) {
toast.success("Login Successfully");
// navigateTo("/");
}
}, [isSuccess]);
const handleGithubLogin = () => {
location.href = `http://github.com/login/oauth/authorize?client_id=${client_id}`;
// console.log("github login");
};
// console.log("google login ", loaded);
return (
<StyledSocialButton onClick={handleGithubLogin}>
<StyledSocialButton onClick={handleGithubLogin} disabled={isLoading}>
<IconGithub className="icon" />
{` ${type === "login" ? "Sign in" : "Sign up"} with Github`}
</StyledSocialButton>
+6 -3
View File
@@ -2,7 +2,8 @@ import { FC, useEffect } from "react";
import { useGoogleLogin } from "react-google-login";
import toast from "react-hot-toast";
import styled from "styled-components";
import googleSvg from "../../assets/icons/google.svg?url";
import { KEY_LOCAL_MAGIC_TOKEN } from "../../app/config";
import IconGoogle from "../../assets/icons/google.svg";
import Button from "./styled/Button";
import { useLoginMutation } from "../../app/services/auth";
@@ -29,7 +30,8 @@ interface Props {
const GoogleLoginButton: FC<Props> = ({ type = "login", clientId }) => {
const [login, { isSuccess, isLoading }] = useLoginMutation();
//拿本地存的magic token
const magic_token = localStorage.getItem(KEY_LOCAL_MAGIC_TOKEN);
const { signIn, loaded } = useGoogleLogin({
onScriptLoadFailure: (wtf) => {
console.error("google login script load failure", wtf);
@@ -38,6 +40,7 @@ const GoogleLoginButton: FC<Props> = ({ type = "login", clientId }) => {
onSuccess: ({ tokenId, ...rest }) => {
console.info("google oauth success", tokenId, rest);
login({
magic_token,
id_token: tokenId,
type: "google"
});
@@ -60,7 +63,7 @@ const GoogleLoginButton: FC<Props> = ({ type = "login", clientId }) => {
// console.log("google login ", loaded);
return (
<StyledSocialButton disabled={!loaded || isLoading} onClick={handleGoogleLogin}>
<img className="icon" src={googleSvg} alt="google icon" />
<IconGoogle className="icon" alt="google icon" />
{loaded ? `${type === "login" ? "Sign in" : "Sign up"} with Google` : `Initializing`}
</StyledSocialButton>
);