Skip to content

Commit 0cd2206

Browse files
Remove deprecated email auth version usage (#47)
1 parent 6b51f5d commit 0cd2206

File tree

4 files changed

+85
-170
lines changed

4 files changed

+85
-170
lines changed

src/Login.tsx

+15-40
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Box, Text, TextInput, Button, Spinner, Checkbox, Divider, Modal, Switch } from '@0xsequence/design-system'
1+
import { Box, Text, TextInput, Button, Spinner, Divider, Modal, Switch } from '@0xsequence/design-system'
22
import { SetStateAction, useEffect, useRef, useState } from 'react'
33
import { CredentialResponse, GoogleLogin, useGoogleLogin } from '@react-oauth/google'
44
import AppleSignin from 'react-apple-signin-auth'
@@ -11,7 +11,6 @@ import { EmailConflictWarning } from './components/views/EmailConflictWarningVie
1111

1212
import { randomName } from './utils/indexer'
1313
import { useEmailAuth } from './utils/useEmailAuth.ts'
14-
import { useEmailAuthV2 } from './utils/useEmailAuthV2.ts'
1514
import { StytchLogin } from './components/StytchLogin.tsx'
1615
import { StytchLegacyLogin } from './components/StytchLegacyLogin.tsx'
1716
import { EmailConflictInfo } from '@0xsequence/waas'
@@ -23,8 +22,6 @@ function Login() {
2322
const [showEmailWarning, setEmailWarning] = useState(false)
2423
const [code, setCode] = useState<string[]>([])
2524

26-
const [isEmailV2Enabled, setIsEmailV2Enabled] = useState(true)
27-
2825
const [emailConflictInfo, setEmailConflictInfo] = useState<EmailConflictInfo | undefined>()
2926
const [isEmailConflictModalOpen, setIsEmailConflictModalOpen] = useState(false)
3027
const forceCreateFuncRef = useRef<(() => Promise<void>) | null>(null)
@@ -68,37 +65,19 @@ function Login() {
6865
})
6966

7067
const {
71-
inProgress: emailV2AuthInProgress,
72-
loading: emailV2AuthLoading,
73-
initiateAuth: initiateEmailV2Auth,
74-
sendChallengeAnswer: sendChallengeAnswerV2,
75-
cancel: cancelEmailV2Auth
76-
} = useEmailAuthV2({
68+
inProgress: emailAuthInProgress,
69+
loading: emailAuthLoading,
70+
initiateAuth: initiateEmailAuth,
71+
sendChallengeAnswer,
72+
cancel: cancelEmailAuth
73+
} = useEmailAuth({
7774
sessionName: randomName(),
7875
onSuccess: async ({ wallet }) => {
7976
console.log(`Wallet address: ${wallet}`)
8077
router.navigate('/')
8178
}
8279
})
8380

84-
const {
85-
inProgress: emailV1AuthInProgress,
86-
loading: emailV1AuthLoading,
87-
initiateAuth: initiateEmailV1Auth,
88-
sendChallengeAnswer: sendChallengeAnswerV1
89-
} = useEmailAuth({
90-
onSuccess: async idToken => {
91-
const walletAddress = await sequence.signIn({ idToken }, randomName())
92-
console.log(`Wallet address: ${walletAddress}`)
93-
router.navigate('/')
94-
}
95-
})
96-
97-
const emailAuthInProgress = isEmailV2Enabled ? emailV2AuthInProgress : emailV1AuthInProgress
98-
const emailAuthLoading = isEmailV2Enabled ? emailV2AuthLoading : emailV1AuthLoading
99-
const initiateEmailAuth = isEmailV2Enabled ? initiateEmailV2Auth : initiateEmailV1Auth
100-
const sendChallengeAnswer = isEmailV2Enabled ? sendChallengeAnswerV2 : sendChallengeAnswerV1
101-
10281
useEffect(() => {
10382
;(async () => {
10483
if (await sequence.isSignedIn()) {
@@ -168,8 +147,14 @@ function Login() {
168147
</Box>
169148
</Box>
170149

150+
<Box marginTop="6" marginBottom="4">
151+
<Text variant="large" color="text100" fontWeight="bold">
152+
Guest Login
153+
</Text>
154+
</Box>
155+
171156
<Box gap="4">
172-
<Button label="Guest login" onClick={handleGuestLogin} />
157+
<Button label="Login as guest" onClick={handleGuestLogin} />
173158
</Box>
174159

175160
<Divider background="buttonGlass" />
@@ -178,16 +163,6 @@ function Login() {
178163
<Text variant="large" color="text100" fontWeight="bold">
179164
Email Login
180165
</Text>
181-
182-
<Box marginTop="4">
183-
<Checkbox
184-
label="Use v2 email login"
185-
checked={isEmailV2Enabled}
186-
onCheckedChange={() => {
187-
setIsEmailV2Enabled(!isEmailV2Enabled)
188-
}}
189-
/>
190-
</Box>
191166
</Box>
192167

193168
{sendChallengeAnswer ? (
@@ -328,7 +303,7 @@ function Login() {
328303
setEmailConflictInfo(undefined)
329304
if (emailAuthInProgress) {
330305
setCode([])
331-
cancelEmailV2Auth()
306+
cancelEmailAuth()
332307
setEmail('')
333308
}
334309
}}

src/components/views/ListAccountsView.tsx

+3-14
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { Box, Button, Checkbox, Divider, PINCodeInput, Spinner, Text, TextInput, useToast } from '@0xsequence/design-system'
1+
import { Box, Button, Divider, PINCodeInput, Spinner, Text, TextInput, useToast } from '@0xsequence/design-system'
22
import { SetStateAction, useEffect, useRef, useState } from 'react'
33
import { Account, IdentityType } from '@0xsequence/waas'
44
import { CredentialResponse, GoogleLogin, useGoogleLogin } from '@react-oauth/google'
55
import AppleSignin from 'react-apple-signin-auth'
66

77
import { sequence } from '../../main'
88

9-
import { useEmailAuthV2 } from '../../utils/useEmailAuthV2'
9+
import { useEmailAuth } from '../../utils/useEmailAuth'
1010
import { randomName } from '../../utils/indexer'
1111
import { isAccountAlreadyLinkedError } from '../../utils/error'
1212

@@ -98,14 +98,12 @@ export function ListAccountsView() {
9898
const [showEmailWarning, setEmailWarning] = useState(false)
9999
const [code, setCode] = useState<string[]>([])
100100

101-
const [v2EmailLoginEnabled, setV2EmailLoginEnabled] = useState(true)
102-
103101
const {
104102
inProgress: emailAuthInProgress,
105103
loading: emailAuthLoading,
106104
initiateAuth: initiateEmailAuth,
107105
sendChallengeAnswer
108-
} = useEmailAuthV2({
106+
} = useEmailAuth({
109107
sessionName: randomName(),
110108
onSuccess: async ({ wallet }) => {
111109
console.log(`Wallet address: ${wallet}`)
@@ -291,15 +289,6 @@ export function ListAccountsView() {
291289
<Text variant="normal" color="text100" fontWeight="bold">
292290
Email
293291
</Text>
294-
295-
<Box marginTop="4">
296-
<Checkbox
297-
label="Use v2 email login"
298-
disabled
299-
checked={v2EmailLoginEnabled}
300-
onChange={() => setV2EmailLoginEnabled(!v2EmailLoginEnabled)}
301-
/>
302-
</Box>
303292
</Box>
304293

305294
{sendChallengeAnswer ? (

src/utils/useEmailAuth.ts

+67-24
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,92 @@
1-
import { useState } from 'react'
1+
import { useEffect, useState } from 'react'
22
import { sequence } from '../main'
3+
import { Challenge } from '@0xsequence/waas'
4+
import { isAccountAlreadyLinkedError } from './error'
5+
import { useToast } from '@0xsequence/design-system'
6+
7+
export function useEmailAuth({
8+
onSuccess,
9+
sessionName,
10+
linkAccount = false
11+
}: {
12+
onSuccess: (res: { wallet: string; sessionId: string }) => void
13+
sessionName: string
14+
linkAccount?: boolean
15+
}) {
16+
const toast = useToast()
317

4-
export function useEmailAuth({ onSuccess }: { onSuccess: (idToken: string) => void }) {
5-
const [email, setEmail] = useState('')
618
const [error, setError] = useState<unknown>()
719
const [loading, setLoading] = useState(false)
8-
const [instance, setInstance] = useState('')
20+
const [inProgress, setInProgress] = useState(false)
21+
const [respondWithCode, setRespondWithCode] = useState<((code: string) => Promise<void>) | null>()
22+
23+
const [challenge, setChallenge] = useState<Challenge | undefined>()
24+
25+
useEffect(() => {
26+
return sequence.onEmailAuthCodeRequired(async respondWithCode => {
27+
setLoading(false)
28+
setRespondWithCode(() => respondWithCode)
29+
})
30+
}, [sequence, setLoading, setRespondWithCode])
931

1032
const initiateAuth = async (email: string) => {
1133
setLoading(true)
12-
34+
setInProgress(true)
1335
try {
14-
const { instance } = await sequence.email.initiateAuth({ email })
15-
setInstance(instance)
16-
setEmail(email)
36+
if (linkAccount) {
37+
const challenge = await sequence.initAuth({ email })
38+
setChallenge(challenge)
39+
setLoading(false)
40+
} else {
41+
const res = await sequence.signIn({ email }, sessionName)
42+
onSuccess(res)
43+
}
1744
} catch (e: any) {
18-
console.error(e)
1945
setError(e.message || 'Unknown error')
2046
} finally {
21-
setLoading(false)
47+
if (!linkAccount) {
48+
setLoading(false)
49+
setInProgress(false)
50+
}
2251
}
2352
}
2453

2554
const sendChallengeAnswer = async (answer: string) => {
26-
setLoading(true)
27-
28-
try {
29-
const sessionHash = await sequence.getSessionHash()
30-
const identity = await sequence.email.finalizeAuth({ instance, answer, email, sessionHash })
31-
if (!('idToken' in identity)) {
32-
throw new Error('invalid identity returned by finalizeAuth')
55+
if (linkAccount && challenge) {
56+
//completeAuth(challenge.withAnswer(answer), { sessionName })
57+
try {
58+
await sequence.linkAccount(challenge.withAnswer(answer))
59+
} catch (e) {
60+
if (isAccountAlreadyLinkedError(e)) {
61+
toast({
62+
title: 'Account already linked',
63+
description: 'This account is already linked to another wallet',
64+
variant: 'error'
65+
})
66+
}
3367
}
34-
onSuccess(identity.idToken)
35-
} catch (e: any) {
36-
setError(e.message || 'Unknown error')
37-
} finally {
3868
setLoading(false)
69+
setInProgress(false)
70+
return
71+
}
72+
if (respondWithCode) {
73+
await respondWithCode(answer)
3974
}
4075
}
4176

77+
const cancel = () => {
78+
setInProgress(false)
79+
setLoading(false)
80+
setChallenge(undefined)
81+
setRespondWithCode(null)
82+
}
83+
4284
return {
43-
inProgress: loading || !!instance,
85+
inProgress,
86+
initiateAuth,
4487
loading,
4588
error,
46-
initiateAuth,
47-
sendChallengeAnswer: instance ? sendChallengeAnswer : undefined
89+
sendChallengeAnswer: inProgress ? sendChallengeAnswer : undefined,
90+
cancel
4891
}
4992
}

src/utils/useEmailAuthV2.ts

-92
This file was deleted.

0 commit comments

Comments
 (0)