try optimize setup
This commit is contained in:
parent
6e54471102
commit
37d6eda1cf
|
|
@ -1,4 +1,4 @@
|
||||||
import { useRef, useEffect } from 'react';
|
import { useRef } from 'react';
|
||||||
import { useTonConnectUI } from '@tonconnect/ui-react';
|
import { useTonConnectUI } from '@tonconnect/ui-react';
|
||||||
import { useMutation } from 'react-query';
|
import { useMutation } from 'react-query';
|
||||||
import { request } from '~/shared/libs';
|
import { request } from '~/shared/libs';
|
||||||
|
|
@ -6,7 +6,6 @@ import { useWebApp } from '@vkruglikov/react-telegram-web-app';
|
||||||
import { appendReferral } from '~/shared/utils/start-payload';
|
import { appendReferral } from '~/shared/utils/start-payload';
|
||||||
|
|
||||||
const sessionStorageKey = 'auth_v1_token';
|
const sessionStorageKey = 'auth_v1_token';
|
||||||
const tonProofStorageKey = 'stored_ton_proof';
|
|
||||||
const payloadTTLMS = 1000 * 60 * 20;
|
const payloadTTLMS = 1000 * 60 * 20;
|
||||||
|
|
||||||
export const useAuth = () => {
|
export const useAuth = () => {
|
||||||
|
|
@ -14,25 +13,6 @@ export const useAuth = () => {
|
||||||
const [tonConnectUI] = useTonConnectUI();
|
const [tonConnectUI] = useTonConnectUI();
|
||||||
const interval = useRef<ReturnType<typeof setInterval> | undefined>();
|
const interval = useRef<ReturnType<typeof setInterval> | undefined>();
|
||||||
|
|
||||||
// Store ton_proof when it becomes available
|
|
||||||
useEffect(() => {
|
|
||||||
if (
|
|
||||||
tonConnectUI.wallet?.connectItems?.tonProof &&
|
|
||||||
!('error' in tonConnectUI.wallet.connectItems.tonProof) &&
|
|
||||||
tonConnectUI.wallet.account
|
|
||||||
) {
|
|
||||||
console.log('DEBUG: Storing ton_proof for future use');
|
|
||||||
localStorage.setItem(
|
|
||||||
tonProofStorageKey,
|
|
||||||
JSON.stringify({
|
|
||||||
timestamp: Date.now(),
|
|
||||||
account: tonConnectUI.wallet.account,
|
|
||||||
proof: tonConnectUI.wallet.connectItems.tonProof.proof,
|
|
||||||
})
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}, [tonConnectUI.wallet?.connectItems?.tonProof, tonConnectUI.wallet?.account]);
|
|
||||||
|
|
||||||
const makeAuthRequest = async (params: {
|
const makeAuthRequest = async (params: {
|
||||||
twa_data: string;
|
twa_data: string;
|
||||||
ton_proof?: {
|
ton_proof?: {
|
||||||
|
|
@ -62,11 +42,6 @@ export const useAuth = () => {
|
||||||
}
|
}
|
||||||
return res;
|
return res;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// If we were using ton_proof and it failed, clear stored proof
|
|
||||||
if (params.ton_proof) {
|
|
||||||
console.log('DEBUG: Auth with proof failed, clearing stored proof');
|
|
||||||
localStorage.removeItem(tonProofStorageKey);
|
|
||||||
}
|
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
@ -81,7 +56,6 @@ export const useAuth = () => {
|
||||||
console.log('DEBUG: Wallet selection failed with 404, disconnecting');
|
console.log('DEBUG: Wallet selection failed with 404, disconnecting');
|
||||||
await tonConnectUI.disconnect();
|
await tonConnectUI.disconnect();
|
||||||
localStorage.removeItem(sessionStorageKey);
|
localStorage.removeItem(sessionStorageKey);
|
||||||
localStorage.removeItem(tonProofStorageKey);
|
|
||||||
}
|
}
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
|
|
@ -230,94 +204,14 @@ export const useAuth = () => {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Case 2: Already connected - try to use stored proof first
|
// Case 2: Already connected
|
||||||
console.log('DEBUG: Already connected');
|
console.log('DEBUG: Already connected');
|
||||||
|
// TonConnect proofs are meant for initial wallet binding; reusing old proofs
|
||||||
// Check if we have a valid stored proof
|
// commonly fails server-side (replay/unknown payload). Use TWA auth without proof.
|
||||||
const storedProofData = localStorage.getItem(tonProofStorageKey);
|
|
||||||
if (storedProofData) {
|
|
||||||
try {
|
|
||||||
const proofData = JSON.parse(storedProofData);
|
|
||||||
|
|
||||||
// Check if the proof matches current wallet and is not too old
|
|
||||||
if (tonConnectUI.wallet?.account?.address === proofData.account.address) {
|
|
||||||
console.log('DEBUG: Using stored proof');
|
|
||||||
|
|
||||||
// Try auth with stored proof but ignore errors
|
|
||||||
try {
|
|
||||||
authResult = await makeAuthRequest({
|
|
||||||
twa_data: WebApp.initData,
|
|
||||||
ton_proof: {
|
|
||||||
account: proofData.account,
|
|
||||||
ton_proof: proofData.proof,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
// If successful, remove stored proof as it's been used
|
|
||||||
localStorage.removeItem(tonProofStorageKey);
|
|
||||||
} catch (error) {
|
|
||||||
console.log(
|
|
||||||
'DEBUG: Auth with stored proof failed, proceeding without it'
|
|
||||||
);
|
|
||||||
|
|
||||||
// Fall back to auth without proof
|
|
||||||
authResult = await makeAuthRequest({
|
authResult = await makeAuthRequest({
|
||||||
twa_data: WebApp.initData,
|
twa_data: WebApp.initData,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
console.log('DEBUG: Stored proof address mismatch');
|
|
||||||
localStorage.removeItem(tonProofStorageKey);
|
|
||||||
|
|
||||||
// Auth without proof
|
|
||||||
authResult = await makeAuthRequest({
|
|
||||||
twa_data: WebApp.initData,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
console.error('DEBUG: Error parsing stored proof:', error);
|
|
||||||
localStorage.removeItem(tonProofStorageKey);
|
|
||||||
|
|
||||||
// Auth without proof
|
|
||||||
authResult = await makeAuthRequest({
|
|
||||||
twa_data: WebApp.initData,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// No stored proof, check if we have a live proof
|
|
||||||
if (
|
|
||||||
tonConnectUI.wallet?.connectItems?.tonProof &&
|
|
||||||
!('error' in tonConnectUI.wallet.connectItems.tonProof)
|
|
||||||
) {
|
|
||||||
console.log('DEBUG: Using live proof from wallet');
|
|
||||||
try {
|
|
||||||
// Try auth with the live proof
|
|
||||||
authResult = await makeAuthRequest({
|
|
||||||
twa_data: WebApp.initData,
|
|
||||||
ton_proof: {
|
|
||||||
account: tonConnectUI.wallet.account,
|
|
||||||
ton_proof: tonConnectUI.wallet.connectItems.tonProof.proof,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
console.log(
|
|
||||||
'DEBUG: Auth with live proof failed, proceeding without it'
|
|
||||||
);
|
|
||||||
|
|
||||||
// Fall back to auth without proof
|
|
||||||
authResult = await makeAuthRequest({
|
|
||||||
twa_data: WebApp.initData,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// Connected without proof - already authenticated
|
|
||||||
console.log('DEBUG: Connected without proof, proceeding without it');
|
|
||||||
authResult = await makeAuthRequest({
|
|
||||||
twa_data: WebApp.initData,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Always try to select wallet after auth (this validates the connection)
|
// Always try to select wallet after auth (this validates the connection)
|
||||||
if (tonConnectUI.wallet?.account?.address) {
|
if (tonConnectUI.wallet?.account?.address) {
|
||||||
|
|
@ -332,7 +226,6 @@ export const useAuth = () => {
|
||||||
console.log('DEBUG: Connection validation failed, disconnecting');
|
console.log('DEBUG: Connection validation failed, disconnecting');
|
||||||
await tonConnectUI.disconnect();
|
await tonConnectUI.disconnect();
|
||||||
localStorage.removeItem(sessionStorageKey);
|
localStorage.removeItem(sessionStorageKey);
|
||||||
localStorage.removeItem(tonProofStorageKey);
|
|
||||||
throw new Error('Connection validation failed');
|
throw new Error('Connection validation failed');
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue