@@ -10,6 +10,7 @@ import { useSearchParams } from 'react-router-dom';
1010
1111import { AnalyticsEvents } from '@/constants/analytics' ;
1212import { DialogTypes } from '@/constants/dialogs' ;
13+ import { STRING_KEYS } from '@/constants/localization' ;
1314import { ConnectorType , WalletType } from '@/constants/wallets' ;
1415import {
1516 GoogleIdTokenPayload ,
@@ -20,7 +21,9 @@ import {
2021} from '@/types/turnkey' ;
2122
2223import { useAccounts } from '@/hooks/useAccounts' ;
24+ import { useStringGetter } from '@/hooks/useStringGetter' ;
2325
26+ import { appQueryClient } from '@/state/appQueryClient' ;
2427import { useAppDispatch , useAppSelector } from '@/state/appTypes' ;
2528import { forceOpenDialog , openDialog } from '@/state/dialogs' ;
2629import {
@@ -32,6 +35,7 @@ import {
3235import { getSourceAccount , getTurnkeyEmailOnboardingData } from '@/state/walletSelectors' ;
3336
3437import { track } from '@/lib/analytics/analytics' ;
38+ import { parseTurnkeyError } from '@/lib/turnkey/turnkeyUtils' ;
3539
3640import { useTurnkeyWallet } from './TurnkeyWalletProvider' ;
3741
@@ -67,6 +71,7 @@ export const useTurnkeyAuth = () => useContext(TurnkeyAuthContext)!;
6771
6872const useTurnkeyAuthContext = ( ) => {
6973 const dispatch = useAppDispatch ( ) ;
74+ const stringGetter = useStringGetter ( ) ;
7075 const indexerUrl = useAppSelector ( selectIndexerUrl ) ;
7176 const sourceAccount = useAppSelector ( getSourceAccount ) ;
7277 const { indexedDbClient, authIframeClient } = useTurnkey ( ) ;
@@ -125,14 +130,18 @@ const useTurnkeyAuthContext = () => {
125130 if ( response . errors && Array . isArray ( response . errors ) ) {
126131 // Handle API-reported errors
127132 const errorMsg = response . errors . map ( ( e : { msg : string } ) => e . msg ) . join ( ', ' ) ;
128- throw new Error ( `useTurnkeyAuth: Backend Error: ${ errorMsg } ` ) ;
133+ throw new Error ( `Backend Error: ${ errorMsg } ` ) ;
129134 }
130135
131136 if ( response . dydxAddress === '' ) {
132137 setIsNewTurnkeyUser ( true ) ;
133138 dispatch ( setRequiresAddressUpload ( true ) ) ;
134139 }
135140
141+ if ( loginMethod === LoginMethod . OAuth && response . alreadyExists ) {
142+ throw new Error ( 'User has already registered using this email' ) ;
143+ }
144+
136145 switch ( loginMethod ) {
137146 case LoginMethod . OAuth :
138147 handleOauthResponse ( { response } ) ;
@@ -154,7 +163,7 @@ const useTurnkeyAuthContext = () => {
154163 selectWallet ( undefined ) ;
155164 logBonsaiError ( 'TurnkeyOnboarding' , 'Error during sign-in' , { error } ) ;
156165 setEmailSignInStatus ( 'error' ) ;
157- setEmailSignInError ( error . message ) ;
166+ setEmailSignInError ( parseTurnkeyError ( error . message , stringGetter ) ) ;
158167
159168 if ( variables . loginMethod === LoginMethod . OAuth ) {
160169 const providerName = variables . providerName ;
@@ -181,12 +190,6 @@ const useTurnkeyAuthContext = () => {
181190 signinMethod : providerName ,
182191 } )
183192 ) ;
184- } else if ( variables . loginMethod === LoginMethod . Email ) {
185- track (
186- AnalyticsEvents . TurnkeyLoginCompleted ( {
187- signinMethod : 'email' ,
188- } )
189- ) ;
190193 }
191194 } ,
192195 } ) ;
@@ -330,25 +333,21 @@ const useTurnkeyAuthContext = () => {
330333 let errorMessage : string | undefined ;
331334
332335 if ( error instanceof Error ) {
333- if (
334- error . message . includes ( 'unable to decrypt bundle using embedded key' ) ||
335- error . message . includes ( 'Organization ID is not available' ) ||
336- error . message . includes ( 'Unauthenticated desc' ) ||
337- error . message . includes ( 'Organization ID was not found' )
338- ) {
339- errorMessage =
340- 'Your email link has expired or you are using a different device/browser than the one used to sign in. Please try again.' ;
341- } else {
342- logBonsaiError ( 'TurnkeyOnboarding' , 'error handling email magic link' , { error } ) ;
343- errorMessage = error . message ;
344- }
336+ errorMessage = parseTurnkeyError ( error . message , stringGetter ) ;
337+ logBonsaiError ( 'TurnkeyOnboarding' , 'error handling email magic link' , { error } ) ;
345338 } else {
346339 logBonsaiError ( 'TurnkeyOnboarding' , 'error handling email magic link - unknown' , {
347340 error,
348341 } ) ;
349342 }
350343
351- setEmailSignInError ( errorMessage ?? 'An unknown error occurred' ) ;
344+ setEmailSignInError (
345+ errorMessage ??
346+ stringGetter ( {
347+ key : STRING_KEYS . SOMETHING_WENT_WRONG_WITH_MESSAGE ,
348+ params : { ERROR_MESSAGE : stringGetter ( { key : STRING_KEYS . UNKNOWN_ERROR } ) } ,
349+ } )
350+ ) ;
352351 setEmailSignInStatus ( 'error' ) ;
353352 } finally {
354353 // Clear token from state after it has been consumed
@@ -370,6 +369,7 @@ const useTurnkeyAuthContext = () => {
370369 searchParams ,
371370 setSearchParams ,
372371 dispatch ,
372+ stringGetter ,
373373 ]
374374 ) ;
375375
@@ -414,7 +414,7 @@ const useTurnkeyAuthContext = () => {
414414 } , [ searchParams , setSearchParams ] ) ;
415415
416416 /* ----------------------------- Upload Address ----------------------------- */
417- const { mutateAsync : sendUploadAddressRequest } = useMutation ( {
417+ const { mutateAsync : sendUploadAddressRequest , isPending : isUploadingAddress } = useMutation ( {
418418 mutationFn : async ( {
419419 payload,
420420 } : {
@@ -449,6 +449,9 @@ const useTurnkeyAuthContext = () => {
449449 ) ;
450450 logBonsaiError ( 'TurnkeyOnboarding' , 'Error posting to upload address' , { error } ) ;
451451 } ,
452+ onSuccess : ( ) => {
453+ appQueryClient . invalidateQueries ( { queryKey : [ 'turnkeyWallets' ] } ) ;
454+ } ,
452455 } ) ;
453456
454457 const uploadAddress = useCallback (
@@ -548,6 +551,7 @@ const useTurnkeyAuthContext = () => {
548551 isLoading : status === 'pending' || emailSignInStatus === 'loading' ,
549552 isError : status === 'error' || emailSignInStatus === 'error' ,
550553 needsAddressUpload,
554+ isUploadingAddress,
551555 signInWithOauth,
552556 signInWithOtp,
553557 resetEmailSignInStatus,
0 commit comments