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
+1
View File
@@ -35,6 +35,7 @@ export const KEY_DEVICE_KEY = "RUSTCHAT_DEVICE_KEY";
export const KEY_USERS_VERSION = "RUSTCHAT_USERS_VERSION"; export const KEY_USERS_VERSION = "RUSTCHAT_USERS_VERSION";
export const KEY_AFTER_MID = "RUSTCHAT_AFTER_MID"; export const KEY_AFTER_MID = "RUSTCHAT_AFTER_MID";
export const KEY_PWA_INSTALLED = "RUSTCHAT_PWA_INSTALLED"; export const KEY_PWA_INSTALLED = "RUSTCHAT_PWA_INSTALLED";
export const KEY_LOCAL_MAGIC_TOKEN = "RUSTCHAT_LOCAL_MAGIC_TOKEN";
export const Emojis = ["👍", "❤️", "😄", "👀", "👎", "🎉", "🙁", "🚀"]; export const Emojis = ["👍", "❤️", "😄", "👀", "👎", "🎉", "🙁", "🚀"];
export const Views = { export const Views = {
item: "item", item: "item",
+27 -1
View File
@@ -1,6 +1,11 @@
import { useEffect } from "react";
import IconGithub from "../../assets/icons/github.svg"; import IconGithub from "../../assets/icons/github.svg";
import styled from "styled-components"; import styled from "styled-components";
import { KEY_LOCAL_MAGIC_TOKEN } from "../../app/config";
import Button from "./styled/Button"; import Button from "./styled/Button";
import { useLoginMutation } from "../../app/services/auth";
import toast from "react-hot-toast";
const StyledSocialButton = styled(Button)` const StyledSocialButton = styled(Button)`
width: 100%; width: 100%;
@@ -24,13 +29,34 @@ type Props = {
}; };
export default function GithubLoginButton({ type = "login", client_id }: 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 = () => { const handleGithubLogin = () => {
location.href = `http://github.com/login/oauth/authorize?client_id=${client_id}`; location.href = `http://github.com/login/oauth/authorize?client_id=${client_id}`;
// console.log("github login"); // console.log("github login");
}; };
// console.log("google login ", loaded); // console.log("google login ", loaded);
return ( return (
<StyledSocialButton onClick={handleGithubLogin}> <StyledSocialButton onClick={handleGithubLogin} disabled={isLoading}>
<IconGithub className="icon" /> <IconGithub className="icon" />
{` ${type === "login" ? "Sign in" : "Sign up"} with Github`} {` ${type === "login" ? "Sign in" : "Sign up"} with Github`}
</StyledSocialButton> </StyledSocialButton>
+6 -3
View File
@@ -2,7 +2,8 @@ import { FC, useEffect } from "react";
import { useGoogleLogin } from "react-google-login"; import { useGoogleLogin } from "react-google-login";
import toast from "react-hot-toast"; import toast from "react-hot-toast";
import styled from "styled-components"; 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 Button from "./styled/Button";
import { useLoginMutation } from "../../app/services/auth"; import { useLoginMutation } from "../../app/services/auth";
@@ -29,7 +30,8 @@ interface Props {
const GoogleLoginButton: FC<Props> = ({ type = "login", clientId }) => { const GoogleLoginButton: FC<Props> = ({ type = "login", clientId }) => {
const [login, { isSuccess, isLoading }] = useLoginMutation(); const [login, { isSuccess, isLoading }] = useLoginMutation();
//拿本地存的magic token
const magic_token = localStorage.getItem(KEY_LOCAL_MAGIC_TOKEN);
const { signIn, loaded } = useGoogleLogin({ const { signIn, loaded } = useGoogleLogin({
onScriptLoadFailure: (wtf) => { onScriptLoadFailure: (wtf) => {
console.error("google login script load failure", wtf); console.error("google login script load failure", wtf);
@@ -38,6 +40,7 @@ const GoogleLoginButton: FC<Props> = ({ type = "login", clientId }) => {
onSuccess: ({ tokenId, ...rest }) => { onSuccess: ({ tokenId, ...rest }) => {
console.info("google oauth success", tokenId, rest); console.info("google oauth success", tokenId, rest);
login({ login({
magic_token,
id_token: tokenId, id_token: tokenId,
type: "google" type: "google"
}); });
@@ -60,7 +63,7 @@ const GoogleLoginButton: FC<Props> = ({ type = "login", clientId }) => {
// console.log("google login ", loaded); // console.log("google login ", loaded);
return ( return (
<StyledSocialButton disabled={!loaded || isLoading} onClick={handleGoogleLogin}> <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`} {loaded ? `${type === "login" ? "Sign in" : "Sign up"} with Google` : `Initializing`}
</StyledSocialButton> </StyledSocialButton>
); );
-8
View File
@@ -38,14 +38,6 @@ export default function LoginPage() {
const exists = query.get("exists"); const exists = query.get("exists");
if (oauth) { if (oauth) {
switch (oauth) { switch (oauth) {
case "github":
if (code) {
login({
code: code,
type: "github"
});
}
break;
case "oidc": case "oidc":
if (code && state) { if (code && state) {
login({ login({
+3 -1
View File
@@ -1,7 +1,7 @@
import { useState, useEffect } from "react"; import { useState, useEffect } from "react";
import toast from "react-hot-toast"; import toast from "react-hot-toast";
// import { useNavigate } from "react-router-dom"; // import { useNavigate } from "react-router-dom";
import BASE_URL from "../../app/config"; import BASE_URL, { KEY_LOCAL_MAGIC_TOKEN } from "../../app/config";
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 { useLazyCheckEmailQuery, useSendRegMagicLinkMutation } from "../../app/services/auth"; import { useLazyCheckEmailQuery, useSendRegMagicLinkMutation } from "../../app/services/auth";
@@ -30,6 +30,8 @@ export default function Reg() {
// const githubCode = query.get("gcode"); // const githubCode = query.get("gcode");
const token = query.get("magic_token"); const token = query.get("magic_token");
if (token) { if (token) {
//本地存一下 magic token 后续oauth流程用到
localStorage.setItem(KEY_LOCAL_MAGIC_TOKEN, token);
setMagicToken(token); setMagicToken(token);
} }
}, []); }, []);