@@ -784,21 +784,22 @@ <h3>Payment and provisioning are coming next.</h3>
784784}
785785
786786async function signInWithEthereum ( ) {
787- if ( ! window . ethereum ) { alert ( 'No Ethereum wallet detected. Install a wallet extension first.' ) ; return ; }
788- updateSiweModeHint ( ) ;
789787 const statusEl = document . getElementById ( 'siweStatus' ) ;
790788 const walletEl = document . getElementById ( 'siweWallet' ) ;
791- statusEl . textContent = 'Status: Requesting signature...' ;
792- const [ address ] = await window . ethereum . request ( { method :'eth_requestAccounts' } ) ;
793- const nonceRes = await fetch ( '/api/auth/nonce' , { method :'GET' , headers :{ 'Accept' :'application/json' } } ) ;
794- const nonceData = await nonceRes . json ( ) ;
795- if ( ! nonceData . ok ) { throw new Error ( 'Nonce request failed' ) ; }
796- const domain = window . location . host ;
797- const uri = window . location . origin ;
798- const chainHex = await window . ethereum . request ( { method :'eth_chainId' } ) ;
799- const chainId = parseInt ( chainHex , 16 ) ;
800- const issuedAt = new Date ( ) . toISOString ( ) ;
801- const msg = `${ domain } wants you to sign in with your Ethereum account:
789+ try {
790+ if ( ! window . ethereum ) { throw new Error ( 'NO_WALLET' ) ; }
791+ updateSiweModeHint ( ) ;
792+ statusEl . textContent = 'Status: Requesting signature...' ;
793+ const [ address ] = await window . ethereum . request ( { method :'eth_requestAccounts' } ) ;
794+ const nonceRes = await fetch ( '/api/auth/nonce' , { method :'GET' , headers :{ 'Accept' :'application/json' } } ) ;
795+ const nonceData = await nonceRes . json ( ) ;
796+ if ( ! nonceData . ok ) { throw new Error ( 'NONCE_FAILED' ) ; }
797+ const domain = window . location . host ;
798+ const uri = window . location . origin ;
799+ const chainHex = await window . ethereum . request ( { method :'eth_chainId' } ) ;
800+ const chainId = parseInt ( chainHex , 16 ) ;
801+ const issuedAt = new Date ( ) . toISOString ( ) ;
802+ const msg = `${ domain } wants you to sign in with your Ethereum account:
802803${ address }
803804
804805CommandLayer Claim activation
@@ -808,13 +809,22 @@ <h3>Payment and provisioning are coming next.</h3>
808809Chain ID: ${ chainId }
809810Nonce: ${ nonceData . nonce }
810811Issued At: ${ issuedAt } ` ;
811- const signature = await window . ethereum . request ( { method :'personal_sign' , params :[ msg , address ] } ) ;
812- const verifyRes = await fetch ( '/api/auth/verify' , { method :'POST' , headers :{ 'Content-Type' :'application/json' } , body :JSON . stringify ( { message :msg , signature} ) } ) ;
813- const verify = await verifyRes . json ( ) ;
814- if ( ! verify . ok ) { throw new Error ( verify . error || 'Authentication failed' ) ; }
815- state . authenticatedAddress = verify . address ; state . authStatus = verify . status ; state . authChainId = verify . chainId ;
816- statusEl . textContent = 'Status: Authenticated' ;
817- walletEl . textContent = `Connected wallet: ${ shortAddr ( verify . address ) } ` ;
812+ const signature = await window . ethereum . request ( { method :'personal_sign' , params :[ msg , address ] } ) ;
813+ const verifyRes = await fetch ( '/api/auth/verify' , { method :'POST' , headers :{ 'Content-Type' :'application/json' } , body :JSON . stringify ( { message :msg , signature} ) } ) ;
814+ const verify = await verifyRes . json ( ) ;
815+ if ( ! verify . ok ) { throw new Error ( verify . error || 'Authentication failed' ) ; }
816+ state . authenticatedAddress = verify . address ; state . authStatus = verify . status ; state . authChainId = verify . chainId ;
817+ statusEl . textContent = 'Status: Authenticated' ;
818+ walletEl . textContent = `Connected wallet: ${ shortAddr ( verify . address ) } ` ;
819+ } catch ( err ) {
820+ const msg = ( err && err . message ) || '' ;
821+ if ( msg === 'NO_WALLET' ) statusEl . textContent = 'Status: No wallet found.' ;
822+ else if ( msg . includes ( 'User rejected' ) || msg . includes ( 'rejected' ) ) statusEl . textContent = 'Status: Wallet rejected signature.' ;
823+ else if ( msg . includes ( 'dependency unavailable' ) ) statusEl . textContent = 'Status: Auth dependency unavailable.' ;
824+ else if ( msg . includes ( 'domain mismatch' ) ) statusEl . textContent = 'Status: Domain mismatch.' ;
825+ else if ( msg . includes ( 'signature' ) || msg . includes ( 'SIWE' ) ) statusEl . textContent = 'Status: Signature invalid.' ;
826+ else statusEl . textContent = 'Status: Authentication failed.' ;
827+ }
818828}
819829
820830function goStep5Auth ( ) {
0 commit comments