From e78d23f827e6d063052293b1a83a4a44dbe90379 Mon Sep 17 00:00:00 2001 From: Antonio Georgiev Date: Thu, 30 May 2024 19:28:15 +0300 Subject: [PATCH] fixed x auth, listen on mint --- src/components/Oauth2/oauth2.tsx | 37 +++++++++++++++++++++++++++ src/pages/Nova/NovaDetails.tsx | 9 ++++++- src/pages/Oauth2Callback/Callback.tsx | 10 +++++--- 3 files changed, 51 insertions(+), 5 deletions(-) diff --git a/src/components/Oauth2/oauth2.tsx b/src/components/Oauth2/oauth2.tsx index ab600ed..cf6d439 100644 --- a/src/components/Oauth2/oauth2.tsx +++ b/src/components/Oauth2/oauth2.tsx @@ -27,6 +27,12 @@ const cleanup = (intervalRef, popupRef, handleMessageListener) => { window.removeEventListener("message", handleMessageListener); }; +const xCleanUp = (xIntervalRef) => { + if (xIntervalRef.current) { + clearInterval(xIntervalRef.current); + } +}; + export const useOAuth = () => { const [authenticating, setAuthenticating] = useState(false); const [finsihedFlow, setFinishedFlow] = useState(false); @@ -102,6 +108,7 @@ export const useOAuthSocials = () => { const [finsihedFlow, setFinishedFlow] = useState(false); const popupRef = useRef(); const intervalRef = useRef>(); + const xIntervalRef = useRef>(); const getAuthDiscord = useCallback(async (onSuccess, onFailure) => { setAuthenticating(true); @@ -182,6 +189,7 @@ export const useOAuthSocials = () => { if (error) { onFailure(error); } else { + xCleanUp(xIntervalRef); const response = await axios.post( `${environment.apiUrl}/autID/config/oauth2AccessTokenX`, { @@ -217,9 +225,38 @@ export const useOAuthSocials = () => { } }, 550); + xIntervalRef.current = setInterval(async () => { + const oauthResponse = JSON.parse(localStorage.getItem("OAUTH_RESPONSE")); + if (oauthResponse) { + cleanup(intervalRef, popupRef, handleMessageListener); + localStorage.removeItem("OAUTH_RESPONSE"); + try { + if (oauthResponse.error) { + onFailure(oauthResponse.error); + } else { + const response = await axios.post( + `${environment.apiUrl}/autID/config/oauth2AccessTokenX`, + { + code: oauthResponse.payload.code, + callbackUrl + } + ); + setAuthenticating(false); + popupRef.current.close(); + onSuccess(response.data); + xCleanUp(xIntervalRef); + } + } catch (genericError) { + onFailure(genericError); + console.error(genericError); + } + } + }, 550); + return () => { setAuthenticating(false); cleanup(intervalRef, popupRef, handleMessageListener); + xCleanUp(xIntervalRef); }; }, []); diff --git a/src/pages/Nova/NovaDetails.tsx b/src/pages/Nova/NovaDetails.tsx index 1a46aa7..6f3ba19 100644 --- a/src/pages/Nova/NovaDetails.tsx +++ b/src/pages/Nova/NovaDetails.tsx @@ -23,7 +23,8 @@ import { useCheckHasMintedForNovaQuery, ArchetypeTypes, useGetAllNovasQuery, - useGetNovaTasksQuery + useGetNovaTasksQuery, + communityApi } from "@api/community.api"; import { ipfsCIDToHttpUrl } from "@api/storage.api"; import { ReactComponent as DiscordIcon } from "@assets/SocialIcons/DiscordIcon.svg"; @@ -368,7 +369,12 @@ const NovaDetails = () => { // }; }, []); + const onMinted = async (event: CustomEvent) => { + dispatch(communityApi.util.invalidateTags(["hasMinted"])); + }; + useEffect(() => { + window.addEventListener("aut-minted", onMinted); // window.addEventListener("aut_profile", onAutMenuProfile); // window.addEventListener("aut-Init", onAutInit); // window.addEventListener("aut-onConnected", onAutLogin); @@ -382,6 +388,7 @@ const NovaDetails = () => { // if (abort.current) { // abort.current.abort(); // } + window.removeEventListener("aut-minted", onMinted); }; }, []); diff --git a/src/pages/Oauth2Callback/Callback.tsx b/src/pages/Oauth2Callback/Callback.tsx index d0fe95a..9628b7f 100644 --- a/src/pages/Oauth2Callback/Callback.tsx +++ b/src/pages/Oauth2Callback/Callback.tsx @@ -10,9 +10,12 @@ const Callback = () => { useEffect(() => { const payload = queryToObject(window.location.search.split("?")[1]); const error = payload && payload.error; - - if (!window.opener) { - throw new Error("No window opener"); + if (!window.opener && !error) { + localStorage.setItem("OAUTH_RESPONSE", JSON.stringify({ payload })); + window.close(); + } else if (!window.opener && error) { + localStorage.setItem("OAUTH_RESPONSE", JSON.stringify({ error })); + window.close(); } if (error) { window.opener.postMessage({ @@ -26,7 +29,6 @@ const Callback = () => { payload }); } - // window.close(); }, []); return (