Skip to content

Commit

Permalink
fixed x auth, listen on mint
Browse files Browse the repository at this point in the history
  • Loading branch information
Antonio Georgiev authored and Antonio Georgiev committed May 30, 2024
1 parent 18c350b commit e78d23f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
37 changes: 37 additions & 0 deletions src/components/Oauth2/oauth2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -102,6 +108,7 @@ export const useOAuthSocials = () => {
const [finsihedFlow, setFinishedFlow] = useState(false);
const popupRef = useRef<Window>();
const intervalRef = useRef<ReturnType<typeof setInterval>>();
const xIntervalRef = useRef<ReturnType<typeof setInterval>>();

const getAuthDiscord = useCallback(async (onSuccess, onFailure) => {
setAuthenticating(true);
Expand Down Expand Up @@ -182,6 +189,7 @@ export const useOAuthSocials = () => {
if (error) {
onFailure(error);
} else {
xCleanUp(xIntervalRef);
const response = await axios.post(
`${environment.apiUrl}/autID/config/oauth2AccessTokenX`,
{
Expand Down Expand Up @@ -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);
};
}, []);

Expand Down
9 changes: 8 additions & 1 deletion src/pages/Nova/NovaDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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);
Expand All @@ -382,6 +388,7 @@ const NovaDetails = () => {
// if (abort.current) {
// abort.current.abort();
// }
window.removeEventListener("aut-minted", onMinted);
};
}, []);

Expand Down
10 changes: 6 additions & 4 deletions src/pages/Oauth2Callback/Callback.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -26,7 +29,6 @@ const Callback = () => {
payload
});
}
// window.close();
}, []);

return (
Expand Down

0 comments on commit e78d23f

Please sign in to comment.