From e53ebd63a70e95c90c81e0d351d97885070b5499 Mon Sep 17 00:00:00 2001 From: Tristan Yang Date: Thu, 23 Jun 2022 21:58:26 +0800 Subject: [PATCH] fix: login 410 error tip --- src/app/config.ts | 4 +-- src/app/services/base.query.ts | 29 ++++++++++------------ src/common/component/GithubLoginButton.tsx | 17 ++++++++++++- src/common/component/GoogleLoginButton.tsx | 17 ++++++++++++- src/routes/login/index.tsx | 8 +----- 5 files changed, 48 insertions(+), 27 deletions(-) diff --git a/src/app/config.ts b/src/app/config.ts index a276340b..16f7239f 100644 --- a/src/app/config.ts +++ b/src/app/config.ts @@ -5,9 +5,9 @@ export const ContentTypes = { text: "text/plain", markdown: "text/markdown", file: "rustchat/file", + archive: "rustchat/archive", formData: "multipart/form-data", - json: "application/json", - archive: "rustchat/archive" + json: "application/json" }; export const firebaseConfig = { apiKey: "AIzaSyDyJ6B1Ouenoha_gdGkBwIkBNStlwhlbO0", diff --git a/src/app/services/base.query.ts b/src/app/services/base.query.ts index 20a3cf8d..535fa244 100644 --- a/src/app/services/base.query.ts +++ b/src/app/services/base.query.ts @@ -77,6 +77,18 @@ const baseQueryWithTokenCheck = async (args, api, extraOptions) => { toast.error(`${api.endpoint}: Failed to fetch`); } break; + case 401: + { + if (api.endpoint !== "login") { + api.dispatch(resetAuthData()); + location.href = "/#/login"; + toast.error("API Not Authenticated"); + } + } + break; + case 403: + toast.error("Request Not Allowed"); + break; case 404: { toast.error("Request Not Found"); @@ -87,22 +99,7 @@ const baseQueryWithTokenCheck = async (args, api, extraOptions) => { toast.error(result.error.data || "Server Error"); } break; - case 401: - { - if (api.endpoint !== "login") { - api.dispatch(resetAuthData()); - location.href = "/#/login"; - toast.error("API Not Authenticated"); - } - // toast.error("token expired, please login again"); - // } else { - // return; - // } - } - break; - case 403: - toast.error("Request Not Allowed"); - break; + default: break; } diff --git a/src/common/component/GithubLoginButton.tsx b/src/common/component/GithubLoginButton.tsx index 2c1d7d9e..787c4afc 100644 --- a/src/common/component/GithubLoginButton.tsx +++ b/src/common/component/GithubLoginButton.tsx @@ -31,7 +31,7 @@ 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(); + const [login, { isLoading, isSuccess, error }] = useLoginMutation(); useEffect(() => { const query = new URLSearchParams(location.search); const isGithub = query.get("oauth") === "github"; @@ -50,6 +50,21 @@ export default function GithubLoginButton({ type = "login", client_id }: Props) // 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 = `http://github.com/login/oauth/authorize?client_id=${client_id}`; // console.log("github login"); diff --git a/src/common/component/GoogleLoginButton.tsx b/src/common/component/GoogleLoginButton.tsx index 398b3602..ccd511b4 100644 --- a/src/common/component/GoogleLoginButton.tsx +++ b/src/common/component/GoogleLoginButton.tsx @@ -29,7 +29,7 @@ interface Props { } const GoogleLoginButton: FC = ({ type = "login", clientId }) => { - const [login, { isSuccess, isLoading }] = useLoginMutation(); + const [login, { isSuccess, isLoading, error }] = useLoginMutation(); //拿本地存的magic token const magic_token = localStorage.getItem(KEY_LOCAL_MAGIC_TOKEN); const { signIn, loaded } = useGoogleLogin({ @@ -56,6 +56,21 @@ const GoogleLoginButton: FC = ({ type = "login", 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(); }; diff --git a/src/routes/login/index.tsx b/src/routes/login/index.tsx index ff2b59ab..8dc61236 100644 --- a/src/routes/login/index.tsx +++ b/src/routes/login/index.tsx @@ -70,17 +70,11 @@ export default function LoginPage() { useEffect(() => { if (error) { - console.log(error); + console.log("login err", error); switch (error.status) { - case "PARSING_ERROR": - toast.error(error.data); - break; case 401: toast.error("Username or Password incorrect"); break; - case 404: - toast.error("Account not exist"); - break; case 410: toast.error( "No associated account found, please contact admin for an invitation link to join."