From 939de83654797ae853cf221bc22f539414af4624 Mon Sep 17 00:00:00 2001 From: Oleg Yakovenko Date: Wed, 7 Aug 2024 12:42:02 +0300 Subject: [PATCH] added real ton proof fixed timeout on splash screen --- src/pages/root/steps/welcome-step/index.tsx | 36 +++--- src/shared/services/auth/index.ts | 116 +++++++++++++------- 2 files changed, 94 insertions(+), 58 deletions(-) diff --git a/src/pages/root/steps/welcome-step/index.tsx b/src/pages/root/steps/welcome-step/index.tsx index 6e52067..ff51d1f 100644 --- a/src/pages/root/steps/welcome-step/index.tsx +++ b/src/pages/root/steps/welcome-step/index.tsx @@ -12,43 +12,41 @@ export const WelcomeStep = ({ nextStep }: WelcomeStepProps) => { const [tonConnectUI] = useTonConnectUI(); const [isLoaded, setLoaded] = useState(false); -console.log('💩💩💩 enter WelcomeStep'); + console.log("💩💩💩 enter WelcomeStep"); const auth = useAuth(); - console.log('💩💩💩 after useAuth'); + console.log("💩💩💩 after useAuth"); const handleNextClick = async () => { if (tonConnectUI.connected) { - const res = await auth.mutateAsync(); - sessionStorage.setItem("auth_v1_token", res.data.auth_v1_token); - + await auth.mutateAsync(); nextStep(); } else { await tonConnectUI.openModal(); - - const res = await auth.mutateAsync(); - sessionStorage.setItem("auth_v1_token", res.data.auth_v1_token); + await auth.mutateAsync(); } }; useEffect(() => { - setTimeout(async () => { + const first = setTimeout(async () => { console.log("💩💩💩 call auth"); - const res = await auth.mutateAsync(); - sessionStorage.setItem("auth_v1_token", res.data.auth_v1_token); + await auth.mutateAsync(); }, 1000); - setTimeout(() => { + const second = setTimeout(() => { setLoaded(true); - }, 4000); - }, []); - useEffect(() => { - if (tonConnectUI.connected) { - nextStep(); - } - }, [nextStep, tonConnectUI.connected]); + if (tonConnectUI.connected) { + nextStep(); + } + }, 4000); + + return () => { + clearTimeout(first); + clearTimeout(second); + }; + }, [tonConnectUI.connected]); if (!isLoaded) { return ( diff --git a/src/shared/services/auth/index.ts b/src/shared/services/auth/index.ts index d3f8fda..2d55ce7 100644 --- a/src/shared/services/auth/index.ts +++ b/src/shared/services/auth/index.ts @@ -1,54 +1,92 @@ +import { useRef } from "react"; +import { useTonConnectUI, useTonWallet } from "@tonconnect/ui-react"; import { useMutation } from "react-query"; +import { request } from "~/shared/libs"; import { useWebApp } from "@vkruglikov/react-telegram-web-app"; -import { request } from "~/shared/libs"; +const sessionStorageKey = "auth_v1_token"; +const payloadTTLMS = 1000 * 60 * 20; export const useAuth = () => { const WebApp = useWebApp(); - console.log("👀👀👀 webapp: ", WebApp); + const wallet = useTonWallet(); + const [tonConnectUI] = useTonConnectUI(); + const interval = useRef | undefined>(); return useMutation(["auth"], async () => { - console.log("👀👀👀 in mutation - auth"); + clearInterval(interval.current); - const authV1Token = sessionStorage.getItem("auth_v1_token"); + if (!wallet) { + sessionStorage.removeItem(sessionStorageKey); - let tonProof; - if (authV1Token) { - try { - tonProof = await WebApp.initDataUnsafe.signData({ - data: authV1Token, - }); - console.log("👀👀👀 tonProof: ", tonProof); - } catch (error: any) { - console.error("👀👀👀 Error signing data: ", error); - } + const refreshPayload = async () => { + tonConnectUI.setConnectRequestParameters({ state: "loading" }); + + const value = await request + .post<{ + auth_v1_token: string; + }>("/auth.twa", { + twa_data: WebApp.initData, + }) + .catch((error: any) => { + console.error("Error in authentication request: ", error); + throw new Error("Failed to authenticate."); + }); + if (!value) { + tonConnectUI.setConnectRequestParameters(null); + } else { + tonConnectUI.setConnectRequestParameters({ + state: "ready", + value: { + tonProof: value?.data?.auth_v1_token, + }, + }); + } + }; + + void refreshPayload(); + setInterval(refreshPayload, payloadTTLMS); + + return; } - return request.post<{ - connected_wallet: null | { - version: string; - address: string; - ton_balance: string; - }; - auth_v1_token: string; - }>("/auth.twa", { - twa_data: WebApp.initData, - ton_proof: tonProof - ? { - account: { - address: tonProof.address, - // O.: add more as needed - }, - ton_proof: { - signature: tonProof.signature, - payload: tonProof.payload, - // O.: add more as needed - }, + if ( + wallet.connectItems?.tonProof && + !("error" in wallet.connectItems.tonProof) + ) { + const tonProof = wallet.connectItems.tonProof.proof; + + console.log("DEBUG TON-PROOF", tonProof); + + request + .post<{ + connected_wallet: null | { + version: string; + address: string; + ton_balance: string; + }; + auth_v1_token: string; + }>("/auth.twa", { + twa_data: WebApp.initData, + ton_proof: { + account: wallet.account, + ton_proof: tonProof, + }, + }) + .then((res) => { + if (res?.data?.auth_v1_token) { + sessionStorage.setItem(sessionStorageKey, res?.data?.auth_v1_token); + } else { + alert("Please try another wallet"); + tonConnectUI.disconnect(); } - : undefined, - }).catch((error: any) => { - console.error("Error in authentication request: ", error); - throw new Error("Failed to authenticate."); - }); + }) + .catch((error: any) => { + console.error("Error in authentication request: ", error); + throw new Error("Failed to authenticate."); + }); + } else { + void tonConnectUI.disconnect(); + } }); };