From 6f78d7c352a3de081c869375e60974b44494efee Mon Sep 17 00:00:00 2001 From: haorwen Date: Wed, 21 Jan 2026 23:50:16 +0800 Subject: [PATCH] fix: update authentication hooks to use public config endpoints and bump version to v0.9.56 --- package.json | 2 +- src/app/services/base.query.ts | 15 ++++++++--- src/app/services/server.ts | 8 ++++++ src/hooks/useGithubAuthPublicConfig.ts | 36 +++++++++++++++++++++++++ src/hooks/useGoogleAuthPublicConfig.ts | 36 +++++++++++++++++++++++++ src/routes/login/SocialLoginButtons.tsx | 10 +++---- src/widget/Popup/Login/index.tsx | 10 +++---- 7 files changed, 103 insertions(+), 14 deletions(-) create mode 100644 src/hooks/useGithubAuthPublicConfig.ts create mode 100644 src/hooks/useGoogleAuthPublicConfig.ts diff --git a/package.json b/package.json index 0d399bc0..6e2f5582 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "vocechat-web", - "version": "0.9.55", + "version": "0.9.56", "homepage": "https://voce.chat", "dependencies": { "@metamask/onboarding": "^1.0.1", diff --git a/src/app/services/base.query.ts b/src/app/services/base.query.ts index 032f0dcf..d106e388 100644 --- a/src/app/services/base.query.ts +++ b/src/app/services/base.query.ts @@ -14,8 +14,8 @@ const whiteList = [ "sendRegMagicLink", "checkEmail", "checkMagicTokenValid", - "getGoogleAuthConfig", - "getGithubAuthConfig", + "getGoogleAuthPublicConfig", + "getGithubAuthPublicConfig", "getSMTPStatus", "getLoginConfig", "getServerVersion", @@ -32,12 +32,21 @@ const whiteList = [ "passkeyLoginStart", "passkeyLoginFinish", ]; -const whiteList401 = ["getAgoraVoicingList", "getAgoraChannels"]; +const whiteList401 = [ + "getAgoraVoicingList", + "getAgoraChannels", + "getGoogleAuthConfig", + "getGithubAuthConfig", +]; const errorWhiteList = [ "preCheckFileFromUrl", "getFavoriteDetails", "getOGInfo", "getArchiveMessage", + "getGoogleAuthPublicConfig", + "getGithubAuthPublicConfig", + "getGoogleAuthConfig", + "getGithubAuthConfig", ]; const whiteList404 = [ "login", diff --git a/src/app/services/server.ts b/src/app/services/server.ts index edc6b47a..2d647091 100644 --- a/src/app/services/server.ts +++ b/src/app/services/server.ts @@ -91,6 +91,9 @@ export const serverApi = createApi({ getGoogleAuthConfig: builder.query({ query: () => ({ url: `/admin/google_auth/config` }), }), + getGoogleAuthPublicConfig: builder.query({ + query: () => ({ url: `/admin/google_auth/public_config` }), + }), updateGoogleAuthConfig: builder.mutation({ query: (data) => ({ url: `/admin/google_auth/config`, @@ -101,6 +104,9 @@ export const serverApi = createApi({ getGithubAuthConfig: builder.query({ query: () => ({ url: `/admin/github_auth/config` }), }), + getGithubAuthPublicConfig: builder.query, void>({ + query: () => ({ url: `/admin/github_auth/public_config` }), + }), updateGithubAuthConfig: builder.mutation({ query: (data) => ({ url: `/admin/github_auth/config`, @@ -485,8 +491,10 @@ export const { useLazyGetServerVersionQuery, useGetServerVersionQuery, useGetGithubAuthConfigQuery, + useGetGithubAuthPublicConfigQuery, useUpdateGithubAuthConfigMutation, useGetGoogleAuthConfigQuery, + useGetGoogleAuthPublicConfigQuery, useUpdateGoogleAuthConfigMutation, useGetSMTPStatusQuery, useSendTestEmailMutation, diff --git a/src/hooks/useGithubAuthPublicConfig.ts b/src/hooks/useGithubAuthPublicConfig.ts new file mode 100644 index 00000000..6f0c5203 --- /dev/null +++ b/src/hooks/useGithubAuthPublicConfig.ts @@ -0,0 +1,36 @@ +import { useEffect, useState } from "react"; +import { + useGetGithubAuthPublicConfigQuery, + useGetGithubAuthConfigQuery +} from "@/app/services/server"; + +export default function useGithubAuthPublicConfig() { + const [shouldUseFallback, setShouldUseFallback] = useState(false); + + // Try public config first + const { data: publicData, error: publicError } = useGetGithubAuthPublicConfigQuery(undefined, { + refetchOnMountOrArgChange: true, + skip: shouldUseFallback + }); + + // Fallback to old config endpoint if public config returns 404 + const { data: fallbackData, error: fallbackError } = useGetGithubAuthConfigQuery(undefined, { + refetchOnMountOrArgChange: true, + skip: !shouldUseFallback + }); + + useEffect(() => { + // If public config returns 404, switch to fallback + if (publicError && 'status' in publicError && publicError.status === 404) { + setShouldUseFallback(true); + } + }, [publicError]); + + // Use public data if available, otherwise use fallback data + // Ignore fallback errors (401 means new server requiring auth, 404 means very old server) + const data = shouldUseFallback ? fallbackData : publicData; + + return { + clientId: data?.client_id || "" + }; +} diff --git a/src/hooks/useGoogleAuthPublicConfig.ts b/src/hooks/useGoogleAuthPublicConfig.ts new file mode 100644 index 00000000..fb814e19 --- /dev/null +++ b/src/hooks/useGoogleAuthPublicConfig.ts @@ -0,0 +1,36 @@ +import { useEffect, useState } from "react"; +import { + useGetGoogleAuthPublicConfigQuery, + useGetGoogleAuthConfigQuery +} from "@/app/services/server"; + +export default function useGoogleAuthPublicConfig() { + const [shouldUseFallback, setShouldUseFallback] = useState(false); + + // Try public config first + const { data: publicData, error: publicError } = useGetGoogleAuthPublicConfigQuery(undefined, { + refetchOnMountOrArgChange: true, + skip: shouldUseFallback + }); + + // Fallback to old config endpoint if public config returns 404 + const { data: fallbackData, error: fallbackError } = useGetGoogleAuthConfigQuery(undefined, { + refetchOnMountOrArgChange: true, + skip: !shouldUseFallback + }); + + useEffect(() => { + // If public config returns 404, switch to fallback + if (publicError && 'status' in publicError && publicError.status === 404) { + setShouldUseFallback(true); + } + }, [publicError]); + + // Use public data if available, otherwise use fallback data + // Ignore fallback errors (401 means new server requiring auth, 404 means very old server) + const data = shouldUseFallback ? fallbackData : publicData; + + return { + clientId: data?.client_id || "" + }; +} diff --git a/src/routes/login/SocialLoginButtons.tsx b/src/routes/login/SocialLoginButtons.tsx index 495878e6..e92f9386 100644 --- a/src/routes/login/SocialLoginButtons.tsx +++ b/src/routes/login/SocialLoginButtons.tsx @@ -5,8 +5,8 @@ import { useTranslation } from "react-i18next"; import { useGetLoginConfigQuery } from "@/app/services/server"; import GithubLoginButton from "@/components/GithubLoginButton"; import GoogleLoginButton from "@/components/GoogleLoginButton"; -import useGithubAuthConfig from "@/hooks/useGithubAuthConfig"; -import useGoogleAuthConfig from "@/hooks/useGoogleAuthConfig"; +import useGithubAuthPublicConfig from "@/hooks/useGithubAuthPublicConfig"; +import useGoogleAuthPublicConfig from "@/hooks/useGoogleAuthPublicConfig"; import { useLoginMutation } from "../../app/services/auth"; import { AuthType } from "../../types/common"; import MetamaskLoginButton from "./MetamaskLoginButton"; @@ -19,9 +19,9 @@ type Props = { const SocialLoginButtons = ({ type = "login" }: Props) => { const { t: ct } = useTranslation(); const [login, { isSuccess }] = useLoginMutation(); - const { config: githubAuthConfig } = useGithubAuthConfig(); + const { clientId: githubClientId } = useGithubAuthPublicConfig(); const { data: loginConfig, isSuccess: loginConfigSuccess } = useGetLoginConfigQuery(); - const { clientId } = useGoogleAuthConfig(); + const { clientId } = useGoogleAuthPublicConfig(); useEffect(() => { if (isSuccess) { @@ -41,7 +41,7 @@ const SocialLoginButtons = ({ type = "login" }: Props) => { <> {googleLogin && } {enableGithubLogin && ( - + )} {enableMetamaskLogin && } {oidc.length > 0 && } diff --git a/src/widget/Popup/Login/index.tsx b/src/widget/Popup/Login/index.tsx index b34d3432..c68b7949 100644 --- a/src/widget/Popup/Login/index.tsx +++ b/src/widget/Popup/Login/index.tsx @@ -13,8 +13,8 @@ import GithubLoginButton from "../../../components/GithubLoginButton"; import GoogleLoginButton from "../../../components/GoogleLoginButton"; import StyledButton from "../../../components/styled/Button"; import Input from "../../../components/styled/Input"; -import useGithubAuthConfig from "../../../hooks/useGithubAuthConfig"; -import useGoogleAuthConfig from "../../../hooks/useGoogleAuthConfig"; +import useGithubAuthPublicConfig from "../../../hooks/useGithubAuthPublicConfig"; +import useGoogleAuthPublicConfig from "../../../hooks/useGoogleAuthPublicConfig"; import { useWidget } from "../../WidgetContext"; import Loading from "@/components/Loading"; import { WIDGET_USER_PWD } from "@/app/config"; @@ -26,8 +26,8 @@ const Login = () => { const { t } = useTranslation("widget"); const dispatch = useDispatch(); const { color, fgColor, from, autoReg, token, id } = useWidget(); - const { clientId } = useGoogleAuthConfig(); - const { config: githubAuthConfig } = useGithubAuthConfig(); + const { clientId } = useGoogleAuthPublicConfig(); + const { clientId: githubClientId } = useGithubAuthPublicConfig(); const [register, { isLoading, isSuccess, data, error }] = useRegisterMutation(); const [loginByToken, { isLoading: isLogging, isError: tokenLoginError }] = useLoginMutation(); // const [login]= useLoginMutation(); @@ -121,7 +121,7 @@ const Login = () => { {hasSocialLogins && } {googleLogin && } {enableGithubLogin && ( - + )}