-
Notifications
You must be signed in to change notification settings - Fork 0
/
with-email.tsx
123 lines (100 loc) · 3.35 KB
/
with-email.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import React, { useState, useEffect } from "react";
import { useAtom } from "jotai";
import Authentication from "../../demo-ui/components/authentication";
import {
disableNextAtom,
disablePrevAtom,
emailAtom,
isLoadingAtom,
isLoggedInAtom,
verificationCodeAtom,
} from "../../demo-ui/state";
import { withMinimumLoadingTime } from "../../demo-ui/lib/utils";
import { capsuleClient } from "../capsule-client";
type AuthWithEmailProps = {};
const AuthWithEmail: React.FC<AuthWithEmailProps> = () => {
const [internalStep, setInternalStep] = useState(0);
const [isLoggedIn, setIsLoggedIn] = useAtom(isLoggedInAtom);
const [isLoading, setIsLoading] = useAtom(isLoadingAtom);
const [, setDisableNext] = useAtom(disableNextAtom);
const [, setDisablePrev] = useAtom(disablePrevAtom);
const [email, setEmail] = useAtom(emailAtom);
const [verificationCode, setVerificationCode] = useAtom(verificationCodeAtom);
useEffect(() => {
checkLoginStatus();
}, []);
const checkLoginStatus = () => {
withMinimumLoadingTime(
async () => {
const loggedIn = await capsuleClient.isFullyLoggedIn();
setIsLoggedIn(loggedIn);
setDisableNext(!loggedIn);
if (loggedIn) {
setInternalStep(2);
}
},
250,
setIsLoading
);
};
useEffect(() => {
if (isLoggedIn && internalStep === 2) {
setDisableNext(false);
setDisablePrev(true);
}
}, [isLoggedIn, internalStep]);
const handleAuthenticateUser = async () => {
setIsLoading(true);
const isExistingUser = await capsuleClient.checkIfUserExists(email);
if (isExistingUser) {
const webAuthUrlForLogin = await capsuleClient.initiateUserLogin(email, false, "email");
const popupWindow = window.open(webAuthUrlForLogin, "loginPopup", "popup=true");
const { needsWallet } = await capsuleClient.waitForLoginAndSetup(popupWindow!);
if (needsWallet) {
const [wallet, recoverySecret] = await capsuleClient.createWallet();
// do something with the wallet and
}
setIsLoggedIn(true);
setInternalStep(2);
} else {
await capsuleClient.createUser(email);
setInternalStep(1);
}
setIsLoading(false);
};
const handleVerifyAndCreateWallet = async () => {
setIsLoading(true);
const isVerified = await capsuleClient.verifyEmail(verificationCode);
if (!isVerified) {
setIsLoading(false);
return;
}
const authUrl = await capsuleClient.getSetUpBiometricsURL(false);
window.open(authUrl, "signUpPopup", "popup=true");
const { recoverySecret } = await capsuleClient.waitForPasskeyAndCreateWallet();
setIsLoggedIn(true);
setInternalStep(2);
setIsLoading(false);
};
return (
<div className="flex flex-col items-center justify-center h-full">
<Authentication
authType="email"
internalStep={internalStep}
email={email}
setEmail={setEmail}
phoneNumber=""
setPhoneNumber={() => {}}
countryCode=""
setCountryCode={() => {}}
verificationCode={verificationCode}
setVerificationCode={setVerificationCode}
isLoading={isLoading}
isLoggedIn={isLoggedIn}
handleAuthenticateUser={handleAuthenticateUser}
handleVerifyAndCreateWallet={handleVerifyAndCreateWallet}
/>
</div>
);
};
export default AuthWithEmail;