| Fixed price and add button to upload own content
This commit is contained in:
parent
d298c58e08
commit
e4f3ddb6cd
|
|
@ -4,7 +4,7 @@ import { useMutation } from "react-query";
|
||||||
import { request } from "~/shared/libs";
|
import { request } from "~/shared/libs";
|
||||||
import { useWebApp } from "@vkruglikov/react-telegram-web-app";
|
import { useWebApp } from "@vkruglikov/react-telegram-web-app";
|
||||||
|
|
||||||
const localStorageKey = "auth_v1_token";
|
const sessionStorageKey = "auth_v1_token";
|
||||||
const payloadTTLMS = 1000 * 60 * 20;
|
const payloadTTLMS = 1000 * 60 * 20;
|
||||||
|
|
||||||
export const useAuth = () => {
|
export const useAuth = () => {
|
||||||
|
|
@ -16,12 +16,8 @@ export const useAuth = () => {
|
||||||
return useMutation(["auth"], async () => {
|
return useMutation(["auth"], async () => {
|
||||||
clearInterval(interval.current);
|
clearInterval(interval.current);
|
||||||
|
|
||||||
// Проверяем токен в localStorage
|
|
||||||
const storedToken = localStorage.getItem(localStorageKey);
|
|
||||||
|
|
||||||
// Если нет кошелька, удаляем токен и запрашиваем новый
|
|
||||||
if (!wallet) {
|
if (!wallet) {
|
||||||
localStorage.removeItem(localStorageKey);
|
localStorage.removeItem(sessionStorageKey);
|
||||||
|
|
||||||
const refreshPayload = async () => {
|
const refreshPayload = async () => {
|
||||||
tonConnectUI.setConnectRequestParameters({ state: "loading" });
|
tonConnectUI.setConnectRequestParameters({ state: "loading" });
|
||||||
|
|
@ -36,12 +32,9 @@ export const useAuth = () => {
|
||||||
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.");
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!value) {
|
if (!value) {
|
||||||
tonConnectUI.setConnectRequestParameters(null);
|
tonConnectUI.setConnectRequestParameters(null);
|
||||||
} else {
|
} else {
|
||||||
// Сохраняем токен в localStorage
|
|
||||||
localStorage.setItem(localStorageKey, value.data.auth_v1_token);
|
|
||||||
tonConnectUI.setConnectRequestParameters({
|
tonConnectUI.setConnectRequestParameters({
|
||||||
state: "ready",
|
state: "ready",
|
||||||
value: {
|
value: {
|
||||||
|
|
@ -51,25 +44,12 @@ export const useAuth = () => {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Обновляем токен каждые 20 минут
|
|
||||||
void refreshPayload();
|
void refreshPayload();
|
||||||
interval.current = setInterval(refreshPayload, payloadTTLMS);
|
setInterval(refreshPayload, payloadTTLMS);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Если токен уже сохранён в localStorage, пропускаем повторную авторизацию
|
|
||||||
if (storedToken) {
|
|
||||||
tonConnectUI.setConnectRequestParameters({
|
|
||||||
state: "ready",
|
|
||||||
value: {
|
|
||||||
tonProof: storedToken,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Логика для случая, когда есть кошелек и требуется тонProof
|
|
||||||
if (
|
if (
|
||||||
wallet.connectItems?.tonProof &&
|
wallet.connectItems?.tonProof &&
|
||||||
!("error" in wallet.connectItems.tonProof)
|
!("error" in wallet.connectItems.tonProof)
|
||||||
|
|
@ -95,8 +75,7 @@ export const useAuth = () => {
|
||||||
})
|
})
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
if (res?.data?.auth_v1_token) {
|
if (res?.data?.auth_v1_token) {
|
||||||
// Сохраняем токен в localStorage
|
localStorage.setItem(sessionStorageKey, res?.data?.auth_v1_token);
|
||||||
localStorage.setItem(localStorageKey, res?.data?.auth_v1_token);
|
|
||||||
} else {
|
} else {
|
||||||
alert("Please try another wallet");
|
alert("Please try another wallet");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue