Merge remote-tracking branch 'upstream/main' into refactor/typescript
# Conflicts: # src/app/services/server.ts # src/common/component/GoogleLoginButton.tsx # src/common/utils.tsx
This commit is contained in:
@@ -1,7 +1,11 @@
|
||||
import { FC } from "react";
|
||||
import { useEffect, FC } 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%;
|
||||
@@ -20,26 +24,58 @@ const StyledSocialButton = styled(Button)`
|
||||
}
|
||||
`;
|
||||
|
||||
interface Props {
|
||||
config: {
|
||||
client_id: string;
|
||||
};
|
||||
}
|
||||
type Props = {
|
||||
client_id: string;
|
||||
type?: "login" | "register";
|
||||
};
|
||||
|
||||
const GithubLoginButton: FC<Props> = ({ config }) => {
|
||||
const { client_id } = config;
|
||||
const GithubLoginButton: FC<Props> = ({ type = "login", client_id }) => {
|
||||
//拿本地存的magic token
|
||||
const magic_token = localStorage.getItem(KEY_LOCAL_MAGIC_TOKEN);
|
||||
const [login, { isLoading, isSuccess, error }] = 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]);
|
||||
useEffect(() => {
|
||||
if (error) {
|
||||
switch (error.status) {
|
||||
case 410:
|
||||
toast.error(
|
||||
"No associated account found, please contact admin for an invitation link to join."
|
||||
);
|
||||
break;
|
||||
default:
|
||||
toast.error("Something Error");
|
||||
break;
|
||||
}
|
||||
return;
|
||||
}
|
||||
}, [error]);
|
||||
const handleGithubLogin = () => {
|
||||
location.href = `https://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" />
|
||||
Sign in with Github
|
||||
{/* {loaded ? `Sign in with Github` : `Initializing`} */}
|
||||
{` ${type === "login" ? "Sign in" : "Sign up"} with Github`}
|
||||
</StyledSocialButton>
|
||||
);
|
||||
};
|
||||
|
||||
export default GithubLoginButton;
|
||||
|
||||
@@ -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";
|
||||
|
||||
@@ -24,11 +25,13 @@ const StyledSocialButton = styled(Button)`
|
||||
|
||||
interface Props {
|
||||
clientId: string;
|
||||
type?: "login" | "register";
|
||||
}
|
||||
|
||||
const GoogleLoginButton: FC<Props> = ({ clientId }) => {
|
||||
const [login, { isSuccess, isLoading }] = useLoginMutation();
|
||||
|
||||
const GoogleLoginButton: FC<Props> = ({ type = "login", clientId }) => {
|
||||
const [login, { isSuccess, isLoading, error }] = 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);
|
||||
@@ -37,6 +40,7 @@ const GoogleLoginButton: FC<Props> = ({ clientId }) => {
|
||||
onSuccess: ({ tokenId, ...rest }) => {
|
||||
console.info("google oauth success", tokenId, rest);
|
||||
login({
|
||||
magic_token,
|
||||
id_token: tokenId,
|
||||
type: "google"
|
||||
});
|
||||
@@ -52,6 +56,21 @@ const GoogleLoginButton: FC<Props> = ({ clientId }) => {
|
||||
// navigateTo("/");
|
||||
}
|
||||
}, [isSuccess]);
|
||||
useEffect(() => {
|
||||
if (error) {
|
||||
switch (error.status) {
|
||||
case 410:
|
||||
toast.error(
|
||||
"No associated account found, please contact admin for an invitation link to join."
|
||||
);
|
||||
break;
|
||||
default:
|
||||
toast.error("Something Error");
|
||||
break;
|
||||
}
|
||||
return;
|
||||
}
|
||||
}, [error]);
|
||||
const handleGoogleLogin = () => {
|
||||
signIn();
|
||||
};
|
||||
@@ -59,8 +78,8 @@ const GoogleLoginButton: FC<Props> = ({ clientId }) => {
|
||||
// console.log("google login ", loaded);
|
||||
return (
|
||||
<StyledSocialButton disabled={!loaded || isLoading} onClick={handleGoogleLogin}>
|
||||
<img className="icon" src={googleSvg} alt="google icon" />
|
||||
{loaded ? "Sign in with Google" : "Initializing"}
|
||||
<IconGoogle className="icon" alt="google icon" />
|
||||
{loaded ? `${type === "login" ? "Sign in" : "Sign up"} with Google` : `Initializing`}
|
||||
</StyledSocialButton>
|
||||
);
|
||||
};
|
||||
|
||||
@@ -3,7 +3,7 @@ import Prompt from "./Prompt";
|
||||
import usePrompt from "./usePrompt";
|
||||
|
||||
export default function Manifest() {
|
||||
const { setCanneled, prompted } = usePrompt();
|
||||
const { setCanceled: setCanceled, prompted } = usePrompt();
|
||||
const deferredPromptRef = useRef(null);
|
||||
const [popup, setPopup] = useState(false);
|
||||
// const { data, isSuccess } = useGetServerQuery();
|
||||
@@ -57,7 +57,7 @@ export default function Manifest() {
|
||||
deferredPromptRef.current = null;
|
||||
};
|
||||
const handleClose = async () => {
|
||||
setCanneled();
|
||||
setCanceled();
|
||||
setPopup(false);
|
||||
};
|
||||
if (!popup || prompted) return null;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"name": "RustChat",
|
||||
"name": "VoceChat",
|
||||
"short_name": "Your private chat APP",
|
||||
"icons": [
|
||||
{
|
||||
|
||||
@@ -10,7 +10,7 @@ export default function usePrompt() {
|
||||
};
|
||||
|
||||
return {
|
||||
setCanneled: setPrompt,
|
||||
setCanceled: setPrompt,
|
||||
prompted: !!localStorage.getItem(KEY_PWA_INSTALLED),
|
||||
resetPrompt
|
||||
};
|
||||
|
||||
@@ -31,7 +31,7 @@ import useUploadFile from "../../hook/useUploadFile";
|
||||
import Styled from "./styled";
|
||||
import { CONFIG } from "./config";
|
||||
import Contact from "../Contact";
|
||||
export const TEXT_EDITOR_PREFIX = "rustchat_text_editor";
|
||||
export const TEXT_EDITOR_PREFIX = "_text_editor";
|
||||
|
||||
let components = createPlateUI({
|
||||
// [ELEMENT_IMAGE]: ImageElement,
|
||||
|
||||
Reference in New Issue
Block a user