@arcblock/did-connect-react 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +13 -0
- package/README.md +134 -0
- package/lib/Address/index.js +4 -0
- package/lib/Avatar/index.js +4 -0
- package/lib/Button/index.js +17 -0
- package/lib/Connect/assets/locale.js +143 -0
- package/lib/Connect/assets/login-bg.png +0 -0
- package/lib/Connect/assets/login-slogan.js +9 -0
- package/lib/Connect/components/action-button.js +26 -0
- package/lib/Connect/components/app-tips.js +132 -0
- package/lib/Connect/components/auto-height.js +31 -0
- package/lib/Connect/components/back-button.js +24 -0
- package/lib/Connect/components/connect-status.js +263 -0
- package/lib/Connect/components/did-connect-title.js +126 -0
- package/lib/Connect/components/download-tips.js +52 -0
- package/lib/Connect/components/loading.js +26 -0
- package/lib/Connect/components/login-item/connect-choose-list.js +249 -0
- package/lib/Connect/components/login-item/login-method-item.js +129 -0
- package/lib/Connect/components/login-item/mobile-login-item.js +114 -0
- package/lib/Connect/components/login-item/passkey-login-item.js +44 -0
- package/lib/Connect/components/login-item/web-login-item.js +97 -0
- package/lib/Connect/components/mask-overlay.js +34 -0
- package/lib/Connect/components/refresh-overlay.js +57 -0
- package/lib/Connect/components/switch-app.js +70 -0
- package/lib/Connect/contexts/state.js +142 -0
- package/lib/Connect/fullpage.js +5 -0
- package/lib/Connect/hooks/auth-url.js +23 -0
- package/lib/Connect/hooks/method-list.js +46 -0
- package/lib/Connect/hooks/page-show.js +17 -0
- package/lib/Connect/hooks/security.js +27 -0
- package/lib/Connect/hooks/token.js +305 -0
- package/lib/Connect/hooks/use-apps.js +19 -0
- package/lib/Connect/hooks/use-quick-connect.js +97 -0
- package/lib/Connect/index.js +498 -0
- package/lib/Connect/landing-page.js +5 -0
- package/lib/Connect/plugins/email/index.js +62 -0
- package/lib/Connect/plugins/email/list-item.js +28 -0
- package/lib/Connect/plugins/email/placeholder.js +283 -0
- package/lib/Connect/plugins/index.js +4 -0
- package/lib/Connect/use-connect.js +164 -0
- package/lib/Connect/with-blocklet.js +15 -0
- package/lib/Connect/with-bridge-call.js +108 -0
- package/lib/Federated/context.js +61 -0
- package/lib/Federated/index.js +7 -0
- package/lib/Logo/index.js +4 -0
- package/lib/OAuth/context.js +234 -0
- package/lib/OAuth/guest.svg.js +5 -0
- package/lib/OAuth/index.js +7 -0
- package/lib/OAuth/passport-switcher.js +114 -0
- package/lib/Passkey/actions.js +165 -0
- package/lib/Passkey/constants.js +4 -0
- package/lib/Passkey/context.js +266 -0
- package/lib/Passkey/dialog.js +277 -0
- package/lib/Passkey/icon.js +13 -0
- package/lib/Passkey/index.js +9 -0
- package/lib/Service/index.js +62 -0
- package/lib/Session/assets/did-spaces-guide-cover.svg.js +135 -0
- package/lib/Session/assets/did-spaces-guide-icon.svg.js +9 -0
- package/lib/Session/context.js +5 -0
- package/lib/Session/did-spaces-guide.js +136 -0
- package/lib/Session/hooks/use-federated.js +64 -0
- package/lib/Session/hooks/use-mobile.js +8 -0
- package/lib/Session/hooks/use-protected-routes.js +11 -0
- package/lib/Session/hooks/use-session-token.js +169 -0
- package/lib/Session/hooks/use-verify.js +45 -0
- package/lib/Session/index.js +896 -0
- package/lib/Session/libs/constants.js +15 -0
- package/lib/Session/libs/did-spaces.js +10 -0
- package/lib/Session/libs/federated.js +42 -0
- package/lib/Session/libs/index.js +15 -0
- package/lib/Session/libs/locales.js +161 -0
- package/lib/Session/libs/login-mobile.js +55 -0
- package/lib/Session/window-focus-aware.js +17 -0
- package/lib/SessionManager/index.js +4 -0
- package/lib/Storage/engine/cookie.js +21 -0
- package/lib/Storage/engine/local-storage.js +36 -0
- package/lib/Storage/index.js +23 -0
- package/lib/User/index.js +6 -0
- package/lib/User/use-did.js +59 -0
- package/lib/User/wrap-did.js +13 -0
- package/lib/WebWalletSWKeeper/index.js +5 -0
- package/lib/constant.js +22 -0
- package/lib/error.js +8 -0
- package/lib/hooks/use-locale.js +7 -0
- package/lib/index.js +33 -0
- package/lib/locales/en.js +17 -0
- package/lib/locales/index.js +10 -0
- package/lib/locales/zh.js +17 -0
- package/lib/package.json.js +7 -0
- package/lib/types.d.ts +355 -0
- package/lib/utils.js +214 -0
- package/package.json +84 -0
- package/src/Address/index.jsx +2 -0
- package/src/Avatar/index.jsx +2 -0
- package/src/Button/Button.stories.jsx +7 -0
- package/src/Button/index.jsx +21 -0
- package/src/Connect/Connect.stories.jsx +34 -0
- package/src/Connect/assets/locale.js +145 -0
- package/src/Connect/assets/login-bg.png +0 -0
- package/src/Connect/assets/login-slogan.js +7 -0
- package/src/Connect/components/action-button.jsx +22 -0
- package/src/Connect/components/app-tips.jsx +156 -0
- package/src/Connect/components/auto-height.jsx +38 -0
- package/src/Connect/components/back-button.jsx +23 -0
- package/src/Connect/components/connect-status.jsx +259 -0
- package/src/Connect/components/did-connect-title.jsx +106 -0
- package/src/Connect/components/download-tips.jsx +55 -0
- package/src/Connect/components/loading.jsx +25 -0
- package/src/Connect/components/login-item/connect-choose-list.jsx +304 -0
- package/src/Connect/components/login-item/login-method-item.jsx +118 -0
- package/src/Connect/components/login-item/mobile-login-item.jsx +179 -0
- package/src/Connect/components/login-item/passkey-login-item.jsx +52 -0
- package/src/Connect/components/login-item/web-login-item.jsx +149 -0
- package/src/Connect/components/mask-overlay.jsx +32 -0
- package/src/Connect/components/refresh-overlay.jsx +52 -0
- package/src/Connect/components/switch-app.jsx +69 -0
- package/src/Connect/contexts/state.jsx +219 -0
- package/src/Connect/fullpage.jsx +3 -0
- package/src/Connect/hooks/auth-url.js +31 -0
- package/src/Connect/hooks/method-list.js +121 -0
- package/src/Connect/hooks/page-show.js +24 -0
- package/src/Connect/hooks/security.js +40 -0
- package/src/Connect/hooks/token.js +639 -0
- package/src/Connect/hooks/use-apps.js +69 -0
- package/src/Connect/hooks/use-quick-connect.js +130 -0
- package/src/Connect/index.jsx +600 -0
- package/src/Connect/landing-page.jsx +3 -0
- package/src/Connect/plugins/email/index.jsx +82 -0
- package/src/Connect/plugins/email/list-item.jsx +31 -0
- package/src/Connect/plugins/email/placeholder.jsx +365 -0
- package/src/Connect/plugins/index.js +2 -0
- package/src/Connect/use-connect.jsx +321 -0
- package/src/Connect/with-blocklet.jsx +26 -0
- package/src/Connect/with-bridge-call.jsx +138 -0
- package/src/Federated/context.jsx +93 -0
- package/src/Federated/index.jsx +1 -0
- package/src/Logo/index.jsx +2 -0
- package/src/OAuth/context.jsx +346 -0
- package/src/OAuth/guest.svg +20 -0
- package/src/OAuth/index.jsx +1 -0
- package/src/OAuth/passport-switcher.jsx +133 -0
- package/src/Passkey/actions.jsx +212 -0
- package/src/Passkey/constants.js +2 -0
- package/src/Passkey/context.jsx +381 -0
- package/src/Passkey/dialog.jsx +391 -0
- package/src/Passkey/icon.jsx +10 -0
- package/src/Passkey/index.jsx +2 -0
- package/src/Service/index.jsx +96 -0
- package/src/Session/assets/did-spaces-guide-cover.svg +128 -0
- package/src/Session/assets/did-spaces-guide-icon.svg +7 -0
- package/src/Session/context.jsx +7 -0
- package/src/Session/did-spaces-guide.jsx +173 -0
- package/src/Session/hooks/use-federated.js +88 -0
- package/src/Session/hooks/use-mobile.jsx +6 -0
- package/src/Session/hooks/use-protected-routes.js +16 -0
- package/src/Session/hooks/use-session-token.js +365 -0
- package/src/Session/hooks/use-verify.jsx +76 -0
- package/src/Session/index.jsx +1687 -0
- package/src/Session/libs/constants.js +14 -0
- package/src/Session/libs/did-spaces.js +38 -0
- package/src/Session/libs/federated.js +79 -0
- package/src/Session/libs/index.js +5 -0
- package/src/Session/libs/locales.js +160 -0
- package/src/Session/libs/login-mobile.js +80 -0
- package/src/Session/window-focus-aware.jsx +28 -0
- package/src/SessionManager/index.jsx +2 -0
- package/src/Storage/engine/cookie.js +23 -0
- package/src/Storage/engine/local-storage.js +55 -0
- package/src/Storage/index.js +25 -0
- package/src/User/index.js +4 -0
- package/src/User/use-did.js +80 -0
- package/src/User/wrap-did.jsx +18 -0
- package/src/WebWalletSWKeeper/index.jsx +3 -0
- package/src/constant.js +26 -0
- package/src/error.js +6 -0
- package/src/hooks/use-locale.jsx +6 -0
- package/src/index.js +43 -0
- package/src/locales/en.jsx +15 -0
- package/src/locales/index.jsx +13 -0
- package/src/locales/zh.jsx +15 -0
- package/src/types.d.ts +355 -0
- package/src/utils.js +395 -0
- package/vite.config.mjs +29 -0
|
@@ -0,0 +1,391 @@
|
|
|
1
|
+
import PropTypes from 'prop-types';
|
|
2
|
+
import { useImperativeHandle, useState } from 'react';
|
|
3
|
+
import styled from '@emotion/styled';
|
|
4
|
+
import {
|
|
5
|
+
Alert,
|
|
6
|
+
Box,
|
|
7
|
+
Button,
|
|
8
|
+
CircularProgress,
|
|
9
|
+
Dialog,
|
|
10
|
+
DialogActions,
|
|
11
|
+
DialogContent,
|
|
12
|
+
DialogTitle,
|
|
13
|
+
Stack,
|
|
14
|
+
TextField,
|
|
15
|
+
} from '@mui/material';
|
|
16
|
+
import isEmail from 'validator/lib/isEmail';
|
|
17
|
+
import { Icon } from '@iconify/react';
|
|
18
|
+
import passKeyRoundedIcon from '@iconify-icons/material-symbols/passkey-rounded';
|
|
19
|
+
import { useMemoizedFn } from 'ahooks';
|
|
20
|
+
import Toast from '@arcblock/ux/lib/Toast';
|
|
21
|
+
import Typography from '@arcblock/ux/lib/Typography';
|
|
22
|
+
import { getCookieOptions } from '@arcblock/ux/lib/Util';
|
|
23
|
+
import trim from 'lodash/trim';
|
|
24
|
+
import base64 from 'base64-url';
|
|
25
|
+
import Cookie from 'js-cookie';
|
|
26
|
+
import noop from 'lodash/noop';
|
|
27
|
+
|
|
28
|
+
import { usePasskey } from './context';
|
|
29
|
+
import { getWebAuthnErrorMessage } from '../utils';
|
|
30
|
+
import { VERIFY_CODE_LENGTH } from './constants';
|
|
31
|
+
|
|
32
|
+
function PasskeyDialog({ ref, extraParams = {}, createMode = 'connect', action, onSuccess = noop, onError = noop }) {
|
|
33
|
+
const { api, locale, t, loginPasskey, logoutPasskey, passkeyState, connectPasskey } = usePasskey();
|
|
34
|
+
const isEmailRequired = createMode === 'register';
|
|
35
|
+
const [isEmailVerified, setIsEmailVerified] = useState(false);
|
|
36
|
+
const handleOpenDialog = useMemoizedFn(() => {
|
|
37
|
+
passkeyState.openDialog = true;
|
|
38
|
+
passkeyState.error = '';
|
|
39
|
+
passkeyState.creatingStatus = '';
|
|
40
|
+
passkeyState.verifyingStatus = '';
|
|
41
|
+
const initialEmail = passkeyState.email;
|
|
42
|
+
setIsEmailVerified(initialEmail && isEmailRequired ? !isEmail(initialEmail) : false);
|
|
43
|
+
});
|
|
44
|
+
const handleCloseDialog = useMemoizedFn(() => {
|
|
45
|
+
passkeyState.openDialog = false;
|
|
46
|
+
passkeyState.error = '';
|
|
47
|
+
passkeyState.creatingStatus = '';
|
|
48
|
+
passkeyState.verifyingStatus = '';
|
|
49
|
+
const initialEmail = passkeyState.email;
|
|
50
|
+
setIsEmailVerified(initialEmail && isEmailRequired ? !isEmail(initialEmail) : false);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
const handleSendCode = useMemoizedFn(async (e) => {
|
|
54
|
+
e.preventDefault();
|
|
55
|
+
if (isEmailRequired && !isEmail(passkeyState.email)) {
|
|
56
|
+
setIsEmailVerified(true);
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
setIsEmailVerified(false);
|
|
60
|
+
passkeyState.loading = true;
|
|
61
|
+
try {
|
|
62
|
+
await api.post(`../kyc/email/send?locale=${locale}`, { email: passkeyState.email });
|
|
63
|
+
passkeyState.sent = true;
|
|
64
|
+
Toast.success(t('codeSentSuccess'));
|
|
65
|
+
} catch (error) {
|
|
66
|
+
Toast.error(error.message);
|
|
67
|
+
document.getElementById('email-input')?.focus();
|
|
68
|
+
} finally {
|
|
69
|
+
passkeyState.loading = false;
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
const handleVerifyPasskey = useMemoizedFn(async (credentialId = '') => {
|
|
74
|
+
try {
|
|
75
|
+
passkeyState.verifying = true;
|
|
76
|
+
passkeyState.error = '';
|
|
77
|
+
if (!credentialId) {
|
|
78
|
+
passkeyState.verifyingStatus = '';
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const result = await loginPasskey({
|
|
82
|
+
...extraParams,
|
|
83
|
+
action,
|
|
84
|
+
credentialId,
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
const cookieOptions = getCookieOptions({ expireInDays: 7 });
|
|
88
|
+
Cookie.remove('connected_did', cookieOptions);
|
|
89
|
+
Cookie.remove('connected_pk', cookieOptions);
|
|
90
|
+
Cookie.remove('connected_wallet_os', cookieOptions);
|
|
91
|
+
|
|
92
|
+
passkeyState.verifying = false;
|
|
93
|
+
if (result?.sessionToken) {
|
|
94
|
+
passkeyState.verifyingStatus = 'succeed';
|
|
95
|
+
onSuccess({ ...result, encrypted: false }, (val) => val);
|
|
96
|
+
}
|
|
97
|
+
} catch (err) {
|
|
98
|
+
console.error('Failed to verify passkey', err);
|
|
99
|
+
const errorMessage = getWebAuthnErrorMessage(err, t('verifyPasskeyFailed'));
|
|
100
|
+
passkeyState.verifying = false;
|
|
101
|
+
passkeyState.error = errorMessage;
|
|
102
|
+
passkeyState.verifyingStatus = 'error';
|
|
103
|
+
await logoutPasskey();
|
|
104
|
+
onError(new Error(errorMessage));
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
const handleCreatePasskey = useMemoizedFn(async () => {
|
|
109
|
+
if (isEmailRequired && !isEmail(passkeyState.email)) {
|
|
110
|
+
setIsEmailVerified(true);
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
setIsEmailVerified(false);
|
|
114
|
+
passkeyState.creating = true;
|
|
115
|
+
passkeyState.error = '';
|
|
116
|
+
passkeyState.creatingStatus = '';
|
|
117
|
+
|
|
118
|
+
try {
|
|
119
|
+
const result = await connectPasskey({
|
|
120
|
+
...extraParams,
|
|
121
|
+
action,
|
|
122
|
+
purpose: isEmailRequired ? 'register' : 'connect',
|
|
123
|
+
email: isEmailRequired ? base64.encode(passkeyState.email) : '',
|
|
124
|
+
});
|
|
125
|
+
if (!result) {
|
|
126
|
+
// 用户取消的操作
|
|
127
|
+
passkeyState.error = t('cancelAuth');
|
|
128
|
+
passkeyState.creatingStatus = 'error';
|
|
129
|
+
passkeyState.creating = false;
|
|
130
|
+
passkeyState.verifying = false;
|
|
131
|
+
passkeyState.verifyingStatus = '';
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
passkeyState.creating = false;
|
|
135
|
+
passkeyState.openDialog = false;
|
|
136
|
+
passkeyState.creatingStatus = 'succeed';
|
|
137
|
+
|
|
138
|
+
if (createMode === 'connect') {
|
|
139
|
+
onSuccess({ ...result, encrypted: false }, (val) => val);
|
|
140
|
+
} else if (result?.credentialId) {
|
|
141
|
+
handleVerifyPasskey(result.credentialId);
|
|
142
|
+
}
|
|
143
|
+
} catch (err) {
|
|
144
|
+
console.error('Failed to create passkey', err);
|
|
145
|
+
passkeyState.creating = false;
|
|
146
|
+
passkeyState.error = getWebAuthnErrorMessage(err, t('createPasskeyFailed'));
|
|
147
|
+
passkeyState.creatingStatus = 'error';
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
const handleVerifyCode = useMemoizedFn(async (e) => {
|
|
152
|
+
if (e) e.preventDefault();
|
|
153
|
+
passkeyState.loading = true;
|
|
154
|
+
try {
|
|
155
|
+
await api.post(`../kyc/email/verify?locale=${locale}`, { code: passkeyState.code });
|
|
156
|
+
passkeyState.verified = true;
|
|
157
|
+
handleCreatePasskey();
|
|
158
|
+
} catch (error) {
|
|
159
|
+
Toast.error(error.message);
|
|
160
|
+
passkeyState.verified = false;
|
|
161
|
+
document.getElementById(`code-input-${trim(passkeyState.code).length - 1}`)?.focus();
|
|
162
|
+
} finally {
|
|
163
|
+
passkeyState.loading = false;
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
useImperativeHandle(ref, () => {
|
|
168
|
+
return {
|
|
169
|
+
open: handleOpenDialog,
|
|
170
|
+
close: handleCloseDialog,
|
|
171
|
+
handleVerifyCode,
|
|
172
|
+
};
|
|
173
|
+
}, [handleOpenDialog, handleCloseDialog, handleVerifyCode]);
|
|
174
|
+
|
|
175
|
+
return (
|
|
176
|
+
<StyledDialog
|
|
177
|
+
open={passkeyState.openDialog}
|
|
178
|
+
onClose={handleCloseDialog}
|
|
179
|
+
fullWidth
|
|
180
|
+
maxWidth="sm"
|
|
181
|
+
fullScreen={window.innerWidth < 600}>
|
|
182
|
+
<StyledDialogTitle>
|
|
183
|
+
<KeyIcon>
|
|
184
|
+
<Icon icon={passKeyRoundedIcon} fontSize={24} color="initial" />
|
|
185
|
+
</KeyIcon>
|
|
186
|
+
{t('createPasskey')}
|
|
187
|
+
</StyledDialogTitle>
|
|
188
|
+
<StyledDialogContent>
|
|
189
|
+
<Typography variant="body1">{t('createPasskeyDesc1')}</Typography>
|
|
190
|
+
<Typography variant="body1">{t('createPasskeyDesc2')}</Typography>
|
|
191
|
+
{passkeyState.error && <Alert severity="error">{passkeyState.error}</Alert>}
|
|
192
|
+
{isEmailRequired && (
|
|
193
|
+
<Form>
|
|
194
|
+
{!passkeyState.sent && (
|
|
195
|
+
<TextField
|
|
196
|
+
type="email"
|
|
197
|
+
id="email-input"
|
|
198
|
+
size="medium"
|
|
199
|
+
placeholder={t('emailPlaceholder')}
|
|
200
|
+
sx={{ width: '100%', '.MuiFormHelperText-root': { ml: 0 } }}
|
|
201
|
+
value={passkeyState.email}
|
|
202
|
+
onChange={(e) => {
|
|
203
|
+
passkeyState.email = e.target.value;
|
|
204
|
+
setIsEmailVerified(!isEmail(passkeyState.email));
|
|
205
|
+
}}
|
|
206
|
+
disabled={passkeyState.loading || passkeyState.creating}
|
|
207
|
+
required
|
|
208
|
+
error={isEmailVerified}
|
|
209
|
+
helperText={isEmailVerified ? t('emailInvalid') : ''}
|
|
210
|
+
/>
|
|
211
|
+
)}
|
|
212
|
+
{passkeyState.sent && !passkeyState.verified && (
|
|
213
|
+
<Stack
|
|
214
|
+
direction="column"
|
|
215
|
+
sx={{
|
|
216
|
+
justifyContent: 'center',
|
|
217
|
+
alignItems: 'center',
|
|
218
|
+
mb: 2,
|
|
219
|
+
}}>
|
|
220
|
+
<Typography variant="body2">{t('codeSentMessage', { email: passkeyState.email })}</Typography>
|
|
221
|
+
<Box sx={{ width: '100%', display: 'flex', justifyContent: 'space-between', gap: 2, mt: 0 }}>
|
|
222
|
+
{Array(VERIFY_CODE_LENGTH)
|
|
223
|
+
.fill('')
|
|
224
|
+
.map((_, index) => (
|
|
225
|
+
<TextField
|
|
226
|
+
// eslint-disable-next-line react/no-array-index-key
|
|
227
|
+
key={`code-input-${index}`}
|
|
228
|
+
value={trim(passkeyState.code[index]) || ''}
|
|
229
|
+
type="number"
|
|
230
|
+
margin="none"
|
|
231
|
+
onChange={(e) => {
|
|
232
|
+
const newCode = passkeyState.code.split('');
|
|
233
|
+
newCode[index] = e.target.value;
|
|
234
|
+
passkeyState.code = newCode.join('');
|
|
235
|
+
if (e.target.value && index < 5) {
|
|
236
|
+
document.getElementById(`code-input-${index + 1}`)?.focus();
|
|
237
|
+
}
|
|
238
|
+
}}
|
|
239
|
+
onKeyDown={(e) => {
|
|
240
|
+
if (e.key === 'Backspace' && !trim(passkeyState.code[index]) && index > 0) {
|
|
241
|
+
document.getElementById(`code-input-${index - 1}`)?.focus();
|
|
242
|
+
}
|
|
243
|
+
}}
|
|
244
|
+
disabled={passkeyState.loading}
|
|
245
|
+
required
|
|
246
|
+
id={`code-input-${index}`}
|
|
247
|
+
autoComplete="off"
|
|
248
|
+
slotProps={{
|
|
249
|
+
htmlInput: {
|
|
250
|
+
maxLength: 1,
|
|
251
|
+
style: { textAlign: 'center', fontSize: '1.5rem' },
|
|
252
|
+
autoComplete: 'off',
|
|
253
|
+
},
|
|
254
|
+
}}
|
|
255
|
+
/>
|
|
256
|
+
))}
|
|
257
|
+
</Box>
|
|
258
|
+
</Stack>
|
|
259
|
+
)}
|
|
260
|
+
</Form>
|
|
261
|
+
)}
|
|
262
|
+
</StyledDialogContent>
|
|
263
|
+
<StyledDialogActions>
|
|
264
|
+
<Button onClick={handleCloseDialog} sx={{ color: 'text.secondary' }}>
|
|
265
|
+
{t('cancel')}
|
|
266
|
+
</Button>
|
|
267
|
+
{isEmailRequired && (
|
|
268
|
+
<Button
|
|
269
|
+
variant="contained"
|
|
270
|
+
onClick={
|
|
271
|
+
// eslint-disable-next-line no-nested-ternary
|
|
272
|
+
passkeyState.verified ? handleCreatePasskey : passkeyState.sent ? handleVerifyCode : handleSendCode
|
|
273
|
+
}
|
|
274
|
+
disabled={
|
|
275
|
+
passkeyState.verifying ||
|
|
276
|
+
passkeyState.creating ||
|
|
277
|
+
!passkeyState.email ||
|
|
278
|
+
(passkeyState.sent && trim(passkeyState.code).length !== VERIFY_CODE_LENGTH)
|
|
279
|
+
}>
|
|
280
|
+
{(passkeyState.loading || passkeyState.creating) && <CircularProgress size={16} sx={{ mr: 1 }} />}
|
|
281
|
+
{/* eslint-disable-next-line no-nested-ternary */}
|
|
282
|
+
{passkeyState.verified ? t('createPasskey') : passkeyState.sent ? t('verifyButton') : t('sendCodeButton')}
|
|
283
|
+
</Button>
|
|
284
|
+
)}
|
|
285
|
+
{!isEmailRequired && (
|
|
286
|
+
<Button
|
|
287
|
+
variant="contained"
|
|
288
|
+
color="primary"
|
|
289
|
+
onClick={handleCreatePasskey}
|
|
290
|
+
disabled={
|
|
291
|
+
passkeyState.loading ||
|
|
292
|
+
passkeyState.creating ||
|
|
293
|
+
(isEmailRequired && (!passkeyState.email || !passkeyState.verified))
|
|
294
|
+
}
|
|
295
|
+
startIcon={
|
|
296
|
+
(passkeyState.loading || passkeyState.creating) && (
|
|
297
|
+
<CircularProgress size={16} sx={{ mr: 1 }} color="inherit" />
|
|
298
|
+
)
|
|
299
|
+
}>
|
|
300
|
+
{passkeyState.loading || passkeyState.creating ? t('creatingPasskey') : t('createPasskey')}
|
|
301
|
+
</Button>
|
|
302
|
+
)}
|
|
303
|
+
</StyledDialogActions>
|
|
304
|
+
</StyledDialog>
|
|
305
|
+
);
|
|
306
|
+
}
|
|
307
|
+
PasskeyDialog.propTypes = {
|
|
308
|
+
extraParams: PropTypes.object,
|
|
309
|
+
createMode: PropTypes.string,
|
|
310
|
+
action: PropTypes.string.isRequired,
|
|
311
|
+
onSuccess: PropTypes.func,
|
|
312
|
+
onError: PropTypes.func,
|
|
313
|
+
ref: PropTypes.any.isRequired,
|
|
314
|
+
};
|
|
315
|
+
|
|
316
|
+
export default PasskeyDialog;
|
|
317
|
+
|
|
318
|
+
const StyledDialog = styled(Dialog)`
|
|
319
|
+
.MuiDialog-paper {
|
|
320
|
+
border-radius: 12px;
|
|
321
|
+
max-width: 440px;
|
|
322
|
+
}
|
|
323
|
+
`;
|
|
324
|
+
|
|
325
|
+
const StyledDialogTitle = styled(DialogTitle)`
|
|
326
|
+
text-align: center;
|
|
327
|
+
padding: 24px 24px 16px;
|
|
328
|
+
font-size: 20px;
|
|
329
|
+
font-weight: 600;
|
|
330
|
+
`;
|
|
331
|
+
|
|
332
|
+
const StyledDialogContent = styled(DialogContent)`
|
|
333
|
+
padding: 0 24px;
|
|
334
|
+
|
|
335
|
+
.MuiTypography-body1 {
|
|
336
|
+
font-size: 14px;
|
|
337
|
+
line-height: 1.5;
|
|
338
|
+
margin-bottom: 16px;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
.MuiTextField-root {
|
|
342
|
+
margin-top: 16px;
|
|
343
|
+
width: 100%;
|
|
344
|
+
}
|
|
345
|
+
`;
|
|
346
|
+
|
|
347
|
+
const StyledDialogActions = styled(DialogActions)`
|
|
348
|
+
text-align: center;
|
|
349
|
+
padding: 16px 24px 24px;
|
|
350
|
+
|
|
351
|
+
.MuiButton-root {
|
|
352
|
+
text-transform: none;
|
|
353
|
+
font-size: 14px;
|
|
354
|
+
border-radius: 6px;
|
|
355
|
+
padding: 6px 16px;
|
|
356
|
+
}
|
|
357
|
+
`;
|
|
358
|
+
|
|
359
|
+
const KeyIcon = styled('div')`
|
|
360
|
+
width: 48px;
|
|
361
|
+
height: 48px;
|
|
362
|
+
margin: 0 auto 16px;
|
|
363
|
+
background-color: #f6f8fa;
|
|
364
|
+
border-radius: 50%;
|
|
365
|
+
display: flex;
|
|
366
|
+
align-items: center;
|
|
367
|
+
justify-content: center;
|
|
368
|
+
|
|
369
|
+
svg {
|
|
370
|
+
width: 32px;
|
|
371
|
+
height: 32px;
|
|
372
|
+
}
|
|
373
|
+
`;
|
|
374
|
+
|
|
375
|
+
const Form = styled.div`
|
|
376
|
+
display: flex;
|
|
377
|
+
flex-direction: column;
|
|
378
|
+
align-items: center;
|
|
379
|
+
justify-content: center;
|
|
380
|
+
width: 100%;
|
|
381
|
+
gap: 24px;
|
|
382
|
+
|
|
383
|
+
input[type='number']::-webkit-inner-spin-button,
|
|
384
|
+
input[type='number']::-webkit-outer-spin-button {
|
|
385
|
+
-webkit-appearance: none;
|
|
386
|
+
margin: 0;
|
|
387
|
+
}
|
|
388
|
+
div.Mui-focused input {
|
|
389
|
+
caret-color: ${(props) => props.theme.palette.primary.main};
|
|
390
|
+
}
|
|
391
|
+
`;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export default function PasskeyIcon(props) {
|
|
2
|
+
return (
|
|
3
|
+
<svg xmlns="http://www.w3.org/2000/svg" width={24} height={24} viewBox="0 0 24 24" {...props}>
|
|
4
|
+
<path
|
|
5
|
+
fill="currentColor"
|
|
6
|
+
d="M3 20v-2.8q0-.85.438-1.562T4.6 14.55q1.55-.775 3.15-1.162T11 13q.5 0 1 .038t1 .112q-.1 1.45.525 2.738T15.35 18v2zm16 3l-1.5-1.5v-4.65q-1.1-.325-1.8-1.237T15 13.5q0-1.45 1.025-2.475T18.5 10t2.475 1.025T22 13.5q0 1.125-.638 2t-1.612 1.25L21 18l-1.5 1.5L21 21zm-8-11q-1.65 0-2.825-1.175T7 8t1.175-2.825T11 4t2.825 1.175T15 8t-1.175 2.825T11 12m7.5 2q.425 0 .713-.288T19.5 13t-.288-.712T18.5 12t-.712.288T17.5 13t.288.713t.712.287"
|
|
7
|
+
/>
|
|
8
|
+
</svg>
|
|
9
|
+
);
|
|
10
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { joinURL } from 'ufo';
|
|
2
|
+
import { createAxios } from '../utils';
|
|
3
|
+
|
|
4
|
+
async function renewRefreshToken(refreshToken, { authServicePrefix, serviceHost }) {
|
|
5
|
+
if (!refreshToken) {
|
|
6
|
+
throw new Error('Refresh token not found');
|
|
7
|
+
}
|
|
8
|
+
const refreshApi = createAxios({
|
|
9
|
+
baseURL: serviceHost,
|
|
10
|
+
timeout: 10 * 1000,
|
|
11
|
+
secure: true,
|
|
12
|
+
headers: {
|
|
13
|
+
authorization: `Bearer ${encodeURIComponent(refreshToken)}`,
|
|
14
|
+
},
|
|
15
|
+
});
|
|
16
|
+
const { data } = await refreshApi.post(joinURL(authServicePrefix, '/refreshSession'));
|
|
17
|
+
return data;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export default function createService(
|
|
21
|
+
{
|
|
22
|
+
refreshTokenStorage,
|
|
23
|
+
sessionTokenStorage,
|
|
24
|
+
serviceHost,
|
|
25
|
+
authServicePrefix,
|
|
26
|
+
onRefreshTokenError,
|
|
27
|
+
onRefreshTokenSuccess,
|
|
28
|
+
},
|
|
29
|
+
options = {}
|
|
30
|
+
) {
|
|
31
|
+
let refreshingTokenRequest = null;
|
|
32
|
+
const service = createAxios({
|
|
33
|
+
baseURL: serviceHost,
|
|
34
|
+
timeout: 30 * 1000,
|
|
35
|
+
...options,
|
|
36
|
+
});
|
|
37
|
+
const { getToken: getSessionToken, setToken: setSessionToken, removeToken: removeSessionToken } = sessionTokenStorage;
|
|
38
|
+
const { getToken: getRefreshToken, setToken: setRefreshToken, removeToken: removeRefreshToken } = refreshTokenStorage;
|
|
39
|
+
|
|
40
|
+
service.interceptors.request.use(
|
|
41
|
+
async (config) => {
|
|
42
|
+
if (sessionTokenStorage.engine === 'ls') {
|
|
43
|
+
const token = getSessionToken();
|
|
44
|
+
if (token) {
|
|
45
|
+
config.headers.authorization = `Bearer ${encodeURIComponent(token)}`;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
if (refreshingTokenRequest) {
|
|
49
|
+
await refreshingTokenRequest;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return config;
|
|
53
|
+
},
|
|
54
|
+
(error) => Promise.reject(error)
|
|
55
|
+
);
|
|
56
|
+
|
|
57
|
+
service.interceptors.response.use(
|
|
58
|
+
(response) => response,
|
|
59
|
+
async (error) => {
|
|
60
|
+
const originalRequest = error.config;
|
|
61
|
+
if (!originalRequest) {
|
|
62
|
+
return Promise.reject(error);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
originalRequest.headers = { ...originalRequest?.headers };
|
|
66
|
+
// FIXME: @zhanghan 在某次 createTokenFn 的时候,出现了 401 导致 did-connect 页面展示了 `Refresh token not found` 错误
|
|
67
|
+
if (error?.response?.status === 401 && !originalRequest._retry) {
|
|
68
|
+
originalRequest._retry = true;
|
|
69
|
+
if (!refreshingTokenRequest) {
|
|
70
|
+
refreshingTokenRequest = renewRefreshToken(getRefreshToken(), { serviceHost, authServicePrefix });
|
|
71
|
+
}
|
|
72
|
+
try {
|
|
73
|
+
const tokenData = await refreshingTokenRequest;
|
|
74
|
+
setSessionToken(tokenData.nextToken);
|
|
75
|
+
setRefreshToken(tokenData.nextRefreshToken);
|
|
76
|
+
if (typeof onRefreshTokenSuccess === 'function') {
|
|
77
|
+
onRefreshTokenSuccess(tokenData);
|
|
78
|
+
}
|
|
79
|
+
return service(originalRequest);
|
|
80
|
+
} catch (error2) {
|
|
81
|
+
removeSessionToken();
|
|
82
|
+
removeRefreshToken();
|
|
83
|
+
if (typeof onRefreshTokenError === 'function') {
|
|
84
|
+
onRefreshTokenError();
|
|
85
|
+
}
|
|
86
|
+
return Promise.reject(error2);
|
|
87
|
+
} finally {
|
|
88
|
+
refreshingTokenRequest = null;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return Promise.reject(error);
|
|
92
|
+
}
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
return service;
|
|
96
|
+
}
|