parent
bf99462835
commit
939de83654
|
|
@ -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 (
|
||||
|
|
|
|||
|
|
@ -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<ReturnType<typeof setInterval> | 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();
|
||||
}
|
||||
});
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in New Issue