diff --git a/src/shared/services/auth/index.ts b/src/shared/services/auth/index.ts index 907cf61..f3fd915 100644 --- a/src/shared/services/auth/index.ts +++ b/src/shared/services/auth/index.ts @@ -1,4 +1,4 @@ -import { useRef, useEffect } from 'react'; +import { useRef } from 'react'; import { useTonConnectUI } from '@tonconnect/ui-react'; import { useMutation } from 'react-query'; import { request } from '~/shared/libs'; @@ -6,7 +6,6 @@ import { useWebApp } from '@vkruglikov/react-telegram-web-app'; import { appendReferral } from '~/shared/utils/start-payload'; const sessionStorageKey = 'auth_v1_token'; -const tonProofStorageKey = 'stored_ton_proof'; const payloadTTLMS = 1000 * 60 * 20; export const useAuth = () => { @@ -14,25 +13,6 @@ export const useAuth = () => { const [tonConnectUI] = useTonConnectUI(); const interval = useRef | 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: { twa_data: string; ton_proof?: { @@ -62,11 +42,6 @@ export const useAuth = () => { } return res; } 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; } }; @@ -77,15 +52,14 @@ export const useAuth = () => { return res; } catch (error: any) { // Check for 404 error (wallet not found or invalid) - if (error.response?.status === 404) { - console.log('DEBUG: Wallet selection failed with 404, disconnecting'); - await tonConnectUI.disconnect(); - localStorage.removeItem(sessionStorageKey); - localStorage.removeItem(tonProofStorageKey); + if (error.response?.status === 404) { + console.log('DEBUG: Wallet selection failed with 404, disconnecting'); + await tonConnectUI.disconnect(); + localStorage.removeItem(sessionStorageKey); + } + throw error; } - throw error; - } - }; + }; // Helper to prepare the connection parameters with proof requirements const prepareConnectParams = async () => { @@ -230,93 +204,13 @@ export const useAuth = () => { }); } } else { - // Case 2: Already connected - try to use stored proof first + // Case 2: Already connected console.log('DEBUG: Already connected'); - - // Check if we have a valid stored 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({ - 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, - }); - } - } + // TonConnect proofs are meant for initial wallet binding; reusing old proofs + // commonly fails server-side (replay/unknown payload). Use TWA auth without proof. + authResult = await makeAuthRequest({ + twa_data: WebApp.initData, + }); } // Always try to select wallet after auth (this validates the connection) @@ -332,7 +226,6 @@ export const useAuth = () => { console.log('DEBUG: Connection validation failed, disconnecting'); await tonConnectUI.disconnect(); localStorage.removeItem(sessionStorageKey); - localStorage.removeItem(tonProofStorageKey); throw new Error('Connection validation failed'); } } catch (error) {