parent
bf99462835
commit
939de83654
|
|
@ -12,43 +12,41 @@ export const WelcomeStep = ({ nextStep }: WelcomeStepProps) => {
|
||||||
const [tonConnectUI] = useTonConnectUI();
|
const [tonConnectUI] = useTonConnectUI();
|
||||||
const [isLoaded, setLoaded] = useState(false);
|
const [isLoaded, setLoaded] = useState(false);
|
||||||
|
|
||||||
console.log('💩💩💩 enter WelcomeStep');
|
console.log("💩💩💩 enter WelcomeStep");
|
||||||
|
|
||||||
const auth = useAuth();
|
const auth = useAuth();
|
||||||
|
|
||||||
console.log('💩💩💩 after useAuth');
|
console.log("💩💩💩 after useAuth");
|
||||||
|
|
||||||
const handleNextClick = async () => {
|
const handleNextClick = async () => {
|
||||||
if (tonConnectUI.connected) {
|
if (tonConnectUI.connected) {
|
||||||
const res = await auth.mutateAsync();
|
await auth.mutateAsync();
|
||||||
sessionStorage.setItem("auth_v1_token", res.data.auth_v1_token);
|
|
||||||
|
|
||||||
nextStep();
|
nextStep();
|
||||||
} else {
|
} else {
|
||||||
await tonConnectUI.openModal();
|
await tonConnectUI.openModal();
|
||||||
|
await auth.mutateAsync();
|
||||||
const res = await auth.mutateAsync();
|
|
||||||
sessionStorage.setItem("auth_v1_token", res.data.auth_v1_token);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setTimeout(async () => {
|
const first = setTimeout(async () => {
|
||||||
console.log("💩💩💩 call auth");
|
console.log("💩💩💩 call auth");
|
||||||
const res = await auth.mutateAsync();
|
await auth.mutateAsync();
|
||||||
sessionStorage.setItem("auth_v1_token", res.data.auth_v1_token);
|
|
||||||
}, 1000);
|
}, 1000);
|
||||||
|
|
||||||
setTimeout(() => {
|
const second = setTimeout(() => {
|
||||||
setLoaded(true);
|
setLoaded(true);
|
||||||
}, 4000);
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (tonConnectUI.connected) {
|
if (tonConnectUI.connected) {
|
||||||
nextStep();
|
nextStep();
|
||||||
}
|
}
|
||||||
}, [nextStep, tonConnectUI.connected]);
|
}, 4000);
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
clearTimeout(first);
|
||||||
|
clearTimeout(second);
|
||||||
|
};
|
||||||
|
}, [tonConnectUI.connected]);
|
||||||
|
|
||||||
if (!isLoaded) {
|
if (!isLoaded) {
|
||||||
return (
|
return (
|
||||||
|
|
|
||||||
|
|
@ -1,30 +1,65 @@
|
||||||
|
import { useRef } from "react";
|
||||||
|
import { useTonConnectUI, useTonWallet } from "@tonconnect/ui-react";
|
||||||
import { useMutation } from "react-query";
|
import { useMutation } from "react-query";
|
||||||
|
import { request } from "~/shared/libs";
|
||||||
import { useWebApp } from "@vkruglikov/react-telegram-web-app";
|
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 = () => {
|
export const useAuth = () => {
|
||||||
const WebApp = useWebApp();
|
const WebApp = useWebApp();
|
||||||
console.log("👀👀👀 webapp: ", WebApp);
|
const wallet = useTonWallet();
|
||||||
|
const [tonConnectUI] = useTonConnectUI();
|
||||||
|
const interval = useRef<ReturnType<typeof setInterval> | undefined>();
|
||||||
|
|
||||||
return useMutation(["auth"], async () => {
|
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;
|
const refreshPayload = async () => {
|
||||||
if (authV1Token) {
|
tonConnectUI.setConnectRequestParameters({ state: "loading" });
|
||||||
try {
|
|
||||||
tonProof = await WebApp.initDataUnsafe.signData({
|
const value = await request
|
||||||
data: authV1Token,
|
.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,
|
||||||
|
},
|
||||||
});
|
});
|
||||||
console.log("👀👀👀 tonProof: ", tonProof);
|
|
||||||
} catch (error: any) {
|
|
||||||
console.error("👀👀👀 Error signing data: ", error);
|
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
void refreshPayload();
|
||||||
|
setInterval(refreshPayload, payloadTTLMS);
|
||||||
|
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
return request.post<{
|
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 | {
|
connected_wallet: null | {
|
||||||
version: string;
|
version: string;
|
||||||
address: string;
|
address: string;
|
||||||
|
|
@ -33,22 +68,25 @@ export const useAuth = () => {
|
||||||
auth_v1_token: string;
|
auth_v1_token: string;
|
||||||
}>("/auth.twa", {
|
}>("/auth.twa", {
|
||||||
twa_data: WebApp.initData,
|
twa_data: WebApp.initData,
|
||||||
ton_proof: tonProof
|
|
||||||
? {
|
|
||||||
account: {
|
|
||||||
address: tonProof.address,
|
|
||||||
// O.: add more as needed
|
|
||||||
},
|
|
||||||
ton_proof: {
|
ton_proof: {
|
||||||
signature: tonProof.signature,
|
account: wallet.account,
|
||||||
payload: tonProof.payload,
|
ton_proof: tonProof,
|
||||||
// O.: add more as needed
|
|
||||||
},
|
},
|
||||||
|
})
|
||||||
|
.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) => {
|
.catch((error: any) => {
|
||||||
console.error("Error in authentication request: ", error);
|
console.error("Error in authentication request: ", error);
|
||||||
throw new Error("Failed to authenticate.");
|
throw new Error("Failed to authenticate.");
|
||||||
});
|
});
|
||||||
|
} else {
|
||||||
|
void tonConnectUI.disconnect();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue