@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.
Files changed (183) hide show
  1. package/LICENSE +13 -0
  2. package/README.md +134 -0
  3. package/lib/Address/index.js +4 -0
  4. package/lib/Avatar/index.js +4 -0
  5. package/lib/Button/index.js +17 -0
  6. package/lib/Connect/assets/locale.js +143 -0
  7. package/lib/Connect/assets/login-bg.png +0 -0
  8. package/lib/Connect/assets/login-slogan.js +9 -0
  9. package/lib/Connect/components/action-button.js +26 -0
  10. package/lib/Connect/components/app-tips.js +132 -0
  11. package/lib/Connect/components/auto-height.js +31 -0
  12. package/lib/Connect/components/back-button.js +24 -0
  13. package/lib/Connect/components/connect-status.js +263 -0
  14. package/lib/Connect/components/did-connect-title.js +126 -0
  15. package/lib/Connect/components/download-tips.js +52 -0
  16. package/lib/Connect/components/loading.js +26 -0
  17. package/lib/Connect/components/login-item/connect-choose-list.js +249 -0
  18. package/lib/Connect/components/login-item/login-method-item.js +129 -0
  19. package/lib/Connect/components/login-item/mobile-login-item.js +114 -0
  20. package/lib/Connect/components/login-item/passkey-login-item.js +44 -0
  21. package/lib/Connect/components/login-item/web-login-item.js +97 -0
  22. package/lib/Connect/components/mask-overlay.js +34 -0
  23. package/lib/Connect/components/refresh-overlay.js +57 -0
  24. package/lib/Connect/components/switch-app.js +70 -0
  25. package/lib/Connect/contexts/state.js +142 -0
  26. package/lib/Connect/fullpage.js +5 -0
  27. package/lib/Connect/hooks/auth-url.js +23 -0
  28. package/lib/Connect/hooks/method-list.js +46 -0
  29. package/lib/Connect/hooks/page-show.js +17 -0
  30. package/lib/Connect/hooks/security.js +27 -0
  31. package/lib/Connect/hooks/token.js +305 -0
  32. package/lib/Connect/hooks/use-apps.js +19 -0
  33. package/lib/Connect/hooks/use-quick-connect.js +97 -0
  34. package/lib/Connect/index.js +498 -0
  35. package/lib/Connect/landing-page.js +5 -0
  36. package/lib/Connect/plugins/email/index.js +62 -0
  37. package/lib/Connect/plugins/email/list-item.js +28 -0
  38. package/lib/Connect/plugins/email/placeholder.js +283 -0
  39. package/lib/Connect/plugins/index.js +4 -0
  40. package/lib/Connect/use-connect.js +164 -0
  41. package/lib/Connect/with-blocklet.js +15 -0
  42. package/lib/Connect/with-bridge-call.js +108 -0
  43. package/lib/Federated/context.js +61 -0
  44. package/lib/Federated/index.js +7 -0
  45. package/lib/Logo/index.js +4 -0
  46. package/lib/OAuth/context.js +234 -0
  47. package/lib/OAuth/guest.svg.js +5 -0
  48. package/lib/OAuth/index.js +7 -0
  49. package/lib/OAuth/passport-switcher.js +114 -0
  50. package/lib/Passkey/actions.js +165 -0
  51. package/lib/Passkey/constants.js +4 -0
  52. package/lib/Passkey/context.js +266 -0
  53. package/lib/Passkey/dialog.js +277 -0
  54. package/lib/Passkey/icon.js +13 -0
  55. package/lib/Passkey/index.js +9 -0
  56. package/lib/Service/index.js +62 -0
  57. package/lib/Session/assets/did-spaces-guide-cover.svg.js +135 -0
  58. package/lib/Session/assets/did-spaces-guide-icon.svg.js +9 -0
  59. package/lib/Session/context.js +5 -0
  60. package/lib/Session/did-spaces-guide.js +136 -0
  61. package/lib/Session/hooks/use-federated.js +64 -0
  62. package/lib/Session/hooks/use-mobile.js +8 -0
  63. package/lib/Session/hooks/use-protected-routes.js +11 -0
  64. package/lib/Session/hooks/use-session-token.js +169 -0
  65. package/lib/Session/hooks/use-verify.js +45 -0
  66. package/lib/Session/index.js +896 -0
  67. package/lib/Session/libs/constants.js +15 -0
  68. package/lib/Session/libs/did-spaces.js +10 -0
  69. package/lib/Session/libs/federated.js +42 -0
  70. package/lib/Session/libs/index.js +15 -0
  71. package/lib/Session/libs/locales.js +161 -0
  72. package/lib/Session/libs/login-mobile.js +55 -0
  73. package/lib/Session/window-focus-aware.js +17 -0
  74. package/lib/SessionManager/index.js +4 -0
  75. package/lib/Storage/engine/cookie.js +21 -0
  76. package/lib/Storage/engine/local-storage.js +36 -0
  77. package/lib/Storage/index.js +23 -0
  78. package/lib/User/index.js +6 -0
  79. package/lib/User/use-did.js +59 -0
  80. package/lib/User/wrap-did.js +13 -0
  81. package/lib/WebWalletSWKeeper/index.js +5 -0
  82. package/lib/constant.js +22 -0
  83. package/lib/error.js +8 -0
  84. package/lib/hooks/use-locale.js +7 -0
  85. package/lib/index.js +33 -0
  86. package/lib/locales/en.js +17 -0
  87. package/lib/locales/index.js +10 -0
  88. package/lib/locales/zh.js +17 -0
  89. package/lib/package.json.js +7 -0
  90. package/lib/types.d.ts +355 -0
  91. package/lib/utils.js +214 -0
  92. package/package.json +84 -0
  93. package/src/Address/index.jsx +2 -0
  94. package/src/Avatar/index.jsx +2 -0
  95. package/src/Button/Button.stories.jsx +7 -0
  96. package/src/Button/index.jsx +21 -0
  97. package/src/Connect/Connect.stories.jsx +34 -0
  98. package/src/Connect/assets/locale.js +145 -0
  99. package/src/Connect/assets/login-bg.png +0 -0
  100. package/src/Connect/assets/login-slogan.js +7 -0
  101. package/src/Connect/components/action-button.jsx +22 -0
  102. package/src/Connect/components/app-tips.jsx +156 -0
  103. package/src/Connect/components/auto-height.jsx +38 -0
  104. package/src/Connect/components/back-button.jsx +23 -0
  105. package/src/Connect/components/connect-status.jsx +259 -0
  106. package/src/Connect/components/did-connect-title.jsx +106 -0
  107. package/src/Connect/components/download-tips.jsx +55 -0
  108. package/src/Connect/components/loading.jsx +25 -0
  109. package/src/Connect/components/login-item/connect-choose-list.jsx +304 -0
  110. package/src/Connect/components/login-item/login-method-item.jsx +118 -0
  111. package/src/Connect/components/login-item/mobile-login-item.jsx +179 -0
  112. package/src/Connect/components/login-item/passkey-login-item.jsx +52 -0
  113. package/src/Connect/components/login-item/web-login-item.jsx +149 -0
  114. package/src/Connect/components/mask-overlay.jsx +32 -0
  115. package/src/Connect/components/refresh-overlay.jsx +52 -0
  116. package/src/Connect/components/switch-app.jsx +69 -0
  117. package/src/Connect/contexts/state.jsx +219 -0
  118. package/src/Connect/fullpage.jsx +3 -0
  119. package/src/Connect/hooks/auth-url.js +31 -0
  120. package/src/Connect/hooks/method-list.js +121 -0
  121. package/src/Connect/hooks/page-show.js +24 -0
  122. package/src/Connect/hooks/security.js +40 -0
  123. package/src/Connect/hooks/token.js +639 -0
  124. package/src/Connect/hooks/use-apps.js +69 -0
  125. package/src/Connect/hooks/use-quick-connect.js +130 -0
  126. package/src/Connect/index.jsx +600 -0
  127. package/src/Connect/landing-page.jsx +3 -0
  128. package/src/Connect/plugins/email/index.jsx +82 -0
  129. package/src/Connect/plugins/email/list-item.jsx +31 -0
  130. package/src/Connect/plugins/email/placeholder.jsx +365 -0
  131. package/src/Connect/plugins/index.js +2 -0
  132. package/src/Connect/use-connect.jsx +321 -0
  133. package/src/Connect/with-blocklet.jsx +26 -0
  134. package/src/Connect/with-bridge-call.jsx +138 -0
  135. package/src/Federated/context.jsx +93 -0
  136. package/src/Federated/index.jsx +1 -0
  137. package/src/Logo/index.jsx +2 -0
  138. package/src/OAuth/context.jsx +346 -0
  139. package/src/OAuth/guest.svg +20 -0
  140. package/src/OAuth/index.jsx +1 -0
  141. package/src/OAuth/passport-switcher.jsx +133 -0
  142. package/src/Passkey/actions.jsx +212 -0
  143. package/src/Passkey/constants.js +2 -0
  144. package/src/Passkey/context.jsx +381 -0
  145. package/src/Passkey/dialog.jsx +391 -0
  146. package/src/Passkey/icon.jsx +10 -0
  147. package/src/Passkey/index.jsx +2 -0
  148. package/src/Service/index.jsx +96 -0
  149. package/src/Session/assets/did-spaces-guide-cover.svg +128 -0
  150. package/src/Session/assets/did-spaces-guide-icon.svg +7 -0
  151. package/src/Session/context.jsx +7 -0
  152. package/src/Session/did-spaces-guide.jsx +173 -0
  153. package/src/Session/hooks/use-federated.js +88 -0
  154. package/src/Session/hooks/use-mobile.jsx +6 -0
  155. package/src/Session/hooks/use-protected-routes.js +16 -0
  156. package/src/Session/hooks/use-session-token.js +365 -0
  157. package/src/Session/hooks/use-verify.jsx +76 -0
  158. package/src/Session/index.jsx +1687 -0
  159. package/src/Session/libs/constants.js +14 -0
  160. package/src/Session/libs/did-spaces.js +38 -0
  161. package/src/Session/libs/federated.js +79 -0
  162. package/src/Session/libs/index.js +5 -0
  163. package/src/Session/libs/locales.js +160 -0
  164. package/src/Session/libs/login-mobile.js +80 -0
  165. package/src/Session/window-focus-aware.jsx +28 -0
  166. package/src/SessionManager/index.jsx +2 -0
  167. package/src/Storage/engine/cookie.js +23 -0
  168. package/src/Storage/engine/local-storage.js +55 -0
  169. package/src/Storage/index.js +25 -0
  170. package/src/User/index.js +4 -0
  171. package/src/User/use-did.js +80 -0
  172. package/src/User/wrap-did.jsx +18 -0
  173. package/src/WebWalletSWKeeper/index.jsx +3 -0
  174. package/src/constant.js +26 -0
  175. package/src/error.js +6 -0
  176. package/src/hooks/use-locale.jsx +6 -0
  177. package/src/index.js +43 -0
  178. package/src/locales/en.jsx +15 -0
  179. package/src/locales/index.jsx +13 -0
  180. package/src/locales/zh.jsx +15 -0
  181. package/src/types.d.ts +355 -0
  182. package/src/utils.js +395 -0
  183. package/vite.config.mjs +29 -0
@@ -0,0 +1,266 @@
1
+ import { jsxs as j, jsx as V } from "react/jsx-runtime";
2
+ import u from "prop-types";
3
+ import O from "lodash/isUndefined";
4
+ import { createContext as z, use as J } from "react";
5
+ import p from "@arcblock/ux/lib/Toast";
6
+ import { useReactive as g, useMemoizedFn as i, useCreation as K } from "ahooks";
7
+ import w from "lodash/noop";
8
+ import { joinURL as S } from "ufo";
9
+ import { translate as W } from "@arcblock/ux/lib/Locale/util";
10
+ import { BLOCKLET_SERVICE_PATH_PREFIX as q } from "@arcblock/ux/lib/Util/constant";
11
+ import { getBlockletData as G, getFederatedEnabled as H, getMaster as X } from "@arcblock/ux/lib/Util/federated";
12
+ import { startRegistration as Q, startAuthentication as Y } from "@simplewebauthn/browser";
13
+ import { createAxios as Z, getWebAuthnErrorMessage as h, logger as U, getApiErrorMessage as A } from "../utils.js";
14
+ import { parseResponse as $, PassportSwitcher as ee } from "../OAuth/passport-switcher.js";
15
+ const F = z({}), { Provider: se, Consumer: we } = F, te = {
16
+ zh: {
17
+ cancel: "取消",
18
+ usePasskey: "使用 Passkey",
19
+ createPasskey: "新建 Passkey",
20
+ creatingPasskey: "创建 Passkey...",
21
+ connectPasskey: "绑定 Passkey",
22
+ nonePasskey: "未配置 Passkey 方式",
23
+ connectPasskeySucceed: "Passkey 绑定成功",
24
+ disconnectPasskeySucceed: "Passkey 解绑成功",
25
+ verifyPasskeyFailed: "Passkey 验证失败",
26
+ connectPasskeyFailed: "Passkey 绑定失败",
27
+ disconnectPasskeyFailed: "Passkey 解绑失败",
28
+ createPasskeyDesc1: "Passkey 是一种密码替代品,使用指纹、面部识别、设备密码或 PIN 验证您的身份。",
29
+ createPasskeyDesc2: "Passkey 可以用于登录、验证,作为您账户简单且安全的替代方案。",
30
+ emailPlaceholder: "请输入邮箱以作为备用",
31
+ verifyButton: "验证邮箱",
32
+ sendCodeButton: "获取验证码",
33
+ codeSentMessage: "我们已向 {email} 发送验证码,验证码将在 30 分钟内有效,请检查您的邮箱或垃圾邮件文件夹。",
34
+ codeSentSuccess: "验证码发送成功",
35
+ noPassports: "没有可更换的通行证",
36
+ cancelAuth: "取消授权",
37
+ emailInvalid: "邮箱格式不正确",
38
+ webauthn: {
39
+ error: {
40
+ canceled: "身份验证已被取消",
41
+ security: "身份验证过程中出现安全错误",
42
+ notSupported: "当前浏览器不支持 Passkey 身份验证",
43
+ aborted: "身份验证已被中止"
44
+ }
45
+ }
46
+ },
47
+ en: {
48
+ cancel: "Cancel",
49
+ usePasskey: "Use Existing Passkey",
50
+ createPasskey: "Create New Passkey",
51
+ creatingPasskey: "Creating Passkey...",
52
+ connectPasskey: "Connect Passkey",
53
+ nonePasskey: "Passkey is not configured",
54
+ connectPasskeySucceed: "Passkey add succeed",
55
+ disconnectPasskeySucceed: "Passkey remove succeed",
56
+ verifyPasskeyFailed: "Passkey verify failed",
57
+ connectPasskeyFailed: "Passkey add failed",
58
+ disconnectPasskeyFailed: "Passkey remove failed",
59
+ createPasskeyDesc1: "Passkeys are a password replacement that validates your identity using touch, facial recognition, a device password, or a PIN.",
60
+ createPasskeyDesc2: "Passkeys can be used for sign-in, verification as a simple and secure alternative to your account.",
61
+ emailPlaceholder: "Enter your email address",
62
+ verifyButton: "Verify Email",
63
+ sendCodeButton: "Get Verify Code",
64
+ codeSentMessage: "We just sent a verification code to {email}, the code will be valid for 30 minutes, please check your inbox or spam folder.",
65
+ codeSentSuccess: "Verification code sent successfully",
66
+ noPassports: "No passports to switch",
67
+ cancelAuth: "Cancel authentication",
68
+ emailInvalid: "Invalid email address",
69
+ webauthn: {
70
+ error: {
71
+ canceled: "Authentication was canceled",
72
+ security: "A security error occurred during authentication",
73
+ notSupported: "This browser does not support passkey authentication",
74
+ aborted: "Authentication was aborted"
75
+ }
76
+ }
77
+ }
78
+ };
79
+ function ae({
80
+ children: m,
81
+ locale: l = "en",
82
+ onAddPasskey: E = w,
83
+ onRemovePasskey: C = w,
84
+ onSwitchPassport: I = w
85
+ }) {
86
+ const v = S(window.env?.apiPrefix || q, "/api/passkey"), t = g({
87
+ baseUrl: "/",
88
+ session: void 0,
89
+ connecting: !1,
90
+ disconnecting: !1,
91
+ targetAppPid: void 0
92
+ }), o = g({
93
+ open: !1,
94
+ currentUser: null,
95
+ passports: [],
96
+ selectedPassport: void 0,
97
+ reset() {
98
+ o.open = !1, o.currentUser = null, o.passports = [], o.selectedPassport = void 0;
99
+ }
100
+ }), d = g({
101
+ user: null,
102
+ loading: !1,
103
+ creating: !1,
104
+ verifying: !1,
105
+ creatingStatus: "",
106
+ verifyingStatus: "",
107
+ error: "",
108
+ email: "",
109
+ code: "",
110
+ verified: !window?.blocklet?.settings?.kyc?.email,
111
+ sent: !1,
112
+ openDialog: !1,
113
+ get status() {
114
+ return this.creating || this.verifying ? "scanned" : this.creatingStatus === "succeed" || this.verifyingStatus === "succeed" ? "succeed" : this.creatingStatus === "error" || this.verifyingStatus === "error" ? "error" : "";
115
+ },
116
+ reset() {
117
+ d.creating = !1, d.verifying = !1, d.creatingStatus = "", d.verifyingStatus = "", d.error = "";
118
+ }
119
+ }), n = i((s, e = {}) => W(te, s, l, "en", e)), c = K(() => Z({ baseURL: S(t.baseUrl, v), sessionTokenKey: "__sst", timeout: 60 * 1e3 }), [t.baseUrl, v]), R = i(async () => {
120
+ if (t.baseUrl === "/")
121
+ return window.blocklet;
122
+ try {
123
+ if (new URL(t.baseUrl).host === window.location.host)
124
+ return window.blocklet;
125
+ } catch {
126
+ }
127
+ return await G(t.baseUrl);
128
+ }), P = window?.blocklet?.componentId, x = (s) => {
129
+ t.baseUrl = s || "/";
130
+ }, B = (s) => {
131
+ t.targetAppPid = s;
132
+ }, b = i(async (s) => {
133
+ const { data: e } = await c.get("/register", { params: s });
134
+ console.warn("passkey.create.options", e);
135
+ try {
136
+ const a = await Q({ optionsJSON: e, useAutoRegister: !1 });
137
+ console.warn("passkey.create.response", a);
138
+ const { data: r } = await c.post("/register", a, { params: { challenge: e.challenge } });
139
+ if (console.warn("passkey.create.result", r), !r.verified)
140
+ throw new Error(n("createPasskeyFailed"));
141
+ return r;
142
+ } catch (a) {
143
+ throw a.name ? new Error(h(a, n("createPasskeyFailed"), n)) : a;
144
+ }
145
+ }), k = i(async (s) => {
146
+ const { data: e } = await c.get("/auth", { params: s });
147
+ console.warn("passkey.auth.options", e);
148
+ try {
149
+ const a = await Y({ optionsJSON: e });
150
+ console.warn("passkey.auth.response", a);
151
+ const { data: r } = await c.post("/auth", a, {
152
+ params: { challenge: e.challenge, targetAppPid: t.targetAppPid }
153
+ });
154
+ if (console.warn("passkey.auth.result", r), !r.verified)
155
+ throw new Error(n("verifyPasskeyFailed"));
156
+ return r;
157
+ } catch (a) {
158
+ throw a.name ? new Error(h(a, n("verifyPasskeyFailed"), n)) : a;
159
+ }
160
+ }), T = async (s) => {
161
+ t.connecting = !0;
162
+ try {
163
+ const e = await b({
164
+ ...s,
165
+ locale: l,
166
+ componentId: P
167
+ });
168
+ return p.success(n("connectPasskeySucceed")), E(e), e;
169
+ } catch (e) {
170
+ U.error("Failed to connect passkey", e), p.error(A(e, n("connectPasskeyFailed")));
171
+ } finally {
172
+ t.connecting = !1;
173
+ }
174
+ }, M = async ({ session: s, connectedAccount: e }) => {
175
+ t.session = s, t.disconnecting = !0;
176
+ try {
177
+ const a = await k({
178
+ action: "disconnect",
179
+ locale: l,
180
+ componentId: P,
181
+ sourceAppPid: s?.user?.sourceAppPid,
182
+ credentialId: e.id
183
+ });
184
+ p.success(n("disconnectPasskeySucceed")), C(a);
185
+ } catch (a) {
186
+ U.error("Failed to disconnect passkey", a), p.error(A(a, n("disconnectPasskeyFailed")));
187
+ } finally {
188
+ t.disconnecting = !1;
189
+ }
190
+ }, D = async (s = {}) => {
191
+ try {
192
+ const { data: e } = await c.get("/passports");
193
+ e.length ? (o.open = !0, o.currentUser = s, o.passports = e || []) : p.error(n("noPassports"));
194
+ } catch (e) {
195
+ p.error(e.message || n("getPassportFailed"));
196
+ }
197
+ }, N = i((...s) => {
198
+ o.reset(), I(...s);
199
+ }), L = i(async ({ action: s = "login", ...e } = {}) => {
200
+ const a = t.baseUrl;
201
+ if (e?.sourceAppPid === window.blocklet?.appPid)
202
+ t.baseUrl = window.blocklet?.appUrl || "/";
203
+ else if (e?.sourceAppPid) {
204
+ const r = await R(), y = H(r), f = X(r);
205
+ y && f?.appPid && e?.sourceAppPid === f?.appPid && (t.baseUrl = f.appUrl);
206
+ }
207
+ try {
208
+ const r = {
209
+ ...e,
210
+ action: s,
211
+ locale: l,
212
+ componentId: P
213
+ };
214
+ O(r.inviter) && window.localStorage.getItem("inviter") && (r.inviter = window.localStorage.getItem("inviter"));
215
+ const y = $(await k(r));
216
+ return y.provider = "passkey", y;
217
+ } catch (r) {
218
+ const y = h(r, n("verifyPasskeyFailed"), n);
219
+ throw t.baseUrl = a, new Error(y);
220
+ }
221
+ }), _ = i(() => {
222
+ });
223
+ return /* @__PURE__ */ j(
224
+ se,
225
+ {
226
+ value: {
227
+ api: c,
228
+ locale: l,
229
+ connectPasskey: T,
230
+ disconnectPasskey: M,
231
+ createPasskey: b,
232
+ verifyPasskey: k,
233
+ loginPasskey: L,
234
+ logoutPasskey: _,
235
+ switchPassport: D,
236
+ baseUrl: t.baseUrl,
237
+ setBaseUrl: x,
238
+ setTargetAppPid: B,
239
+ passkeyState: d,
240
+ disconnecting: t.disconnecting,
241
+ connecting: t.connecting,
242
+ t: n
243
+ },
244
+ children: [
245
+ m,
246
+ /* @__PURE__ */ V(ee, { api: c, locale: l, switchState: o, onSwitchPassport: N })
247
+ ]
248
+ }
249
+ );
250
+ }
251
+ function he() {
252
+ return J(F);
253
+ }
254
+ ae.propTypes = {
255
+ children: u.node.isRequired,
256
+ locale: u.string,
257
+ onAddPasskey: u.func,
258
+ onRemovePasskey: u.func,
259
+ onSwitchPassport: u.func
260
+ };
261
+ export {
262
+ we as PasskeyConsumer,
263
+ F as PasskeyContext,
264
+ ae as PasskeyProvider,
265
+ he as usePasskey
266
+ };
@@ -0,0 +1,277 @@
1
+ import { jsxs as l, jsx as o } from "react/jsx-runtime";
2
+ import c from "prop-types";
3
+ import { useState as W, useImperativeHandle as H } from "react";
4
+ import p from "@emotion/styled";
5
+ import { Dialog as K, DialogTitle as L, DialogContent as G, DialogActions as N, Alert as Y, TextField as F, Stack as J, Box as Q, Button as x, CircularProgress as M } from "@mui/material";
6
+ import m from "validator/lib/isEmail";
7
+ import { Icon as U } from "@iconify/react";
8
+ import X from "@iconify-icons/material-symbols/passkey-rounded";
9
+ import { useMemoizedFn as d } from "ahooks";
10
+ import k from "@arcblock/ux/lib/Toast";
11
+ import b from "@arcblock/ux/lib/Typography";
12
+ import { getCookieOptions as Z } from "@arcblock/ux/lib/Util";
13
+ import g from "lodash/trim";
14
+ import ee from "base64-url";
15
+ import S from "js-cookie";
16
+ import z from "lodash/noop";
17
+ import { usePasskey as te } from "./context.js";
18
+ import { getWebAuthnErrorMessage as j } from "../utils.js";
19
+ import { VERIFY_CODE_LENGTH as $ } from "./constants.js";
20
+ function re({ ref: y, extraParams: w = {}, createMode: D = "connect", action: C, onSuccess: P = z, onError: A = z }) {
21
+ const { api: E, locale: I, t: i, loginPasskey: _, logoutPasskey: q, passkeyState: e, connectPasskey: R } = te(), a = D === "register", [T, s] = W(!1), B = d(() => {
22
+ e.openDialog = !0, e.error = "", e.creatingStatus = "", e.verifyingStatus = "";
23
+ const t = e.email;
24
+ s(t && a ? !m(t) : !1);
25
+ }), u = d(() => {
26
+ e.openDialog = !1, e.error = "", e.creatingStatus = "", e.verifyingStatus = "";
27
+ const t = e.email;
28
+ s(t && a ? !m(t) : !1);
29
+ }), V = d(async (t) => {
30
+ if (t.preventDefault(), a && !m(e.email)) {
31
+ s(!0);
32
+ return;
33
+ }
34
+ s(!1), e.loading = !0;
35
+ try {
36
+ await E.post(`../kyc/email/send?locale=${I}`, { email: e.email }), e.sent = !0, k.success(i("codeSentSuccess"));
37
+ } catch (r) {
38
+ k.error(r.message), document.getElementById("email-input")?.focus();
39
+ } finally {
40
+ e.loading = !1;
41
+ }
42
+ }), O = d(async (t = "") => {
43
+ try {
44
+ e.verifying = !0, e.error = "", t || (e.verifyingStatus = "");
45
+ const r = await _({
46
+ ...w,
47
+ action: C,
48
+ credentialId: t
49
+ }), n = Z({ expireInDays: 7 });
50
+ S.remove("connected_did", n), S.remove("connected_pk", n), S.remove("connected_wallet_os", n), e.verifying = !1, r?.sessionToken && (e.verifyingStatus = "succeed", P({ ...r, encrypted: !1 }, (f) => f));
51
+ } catch (r) {
52
+ console.error("Failed to verify passkey", r);
53
+ const n = j(r, i("verifyPasskeyFailed"));
54
+ e.verifying = !1, e.error = n, e.verifyingStatus = "error", await q(), A(new Error(n));
55
+ }
56
+ }), h = d(async () => {
57
+ if (a && !m(e.email)) {
58
+ s(!0);
59
+ return;
60
+ }
61
+ s(!1), e.creating = !0, e.error = "", e.creatingStatus = "";
62
+ try {
63
+ const t = await R({
64
+ ...w,
65
+ action: C,
66
+ purpose: a ? "register" : "connect",
67
+ email: a ? ee.encode(e.email) : ""
68
+ });
69
+ if (!t) {
70
+ e.error = i("cancelAuth"), e.creatingStatus = "error", e.creating = !1, e.verifying = !1, e.verifyingStatus = "";
71
+ return;
72
+ }
73
+ e.creating = !1, e.openDialog = !1, e.creatingStatus = "succeed", D === "connect" ? P({ ...t, encrypted: !1 }, (r) => r) : t?.credentialId && O(t.credentialId);
74
+ } catch (t) {
75
+ console.error("Failed to create passkey", t), e.creating = !1, e.error = j(t, i("createPasskeyFailed")), e.creatingStatus = "error";
76
+ }
77
+ }), v = d(async (t) => {
78
+ t && t.preventDefault(), e.loading = !0;
79
+ try {
80
+ await E.post(`../kyc/email/verify?locale=${I}`, { code: e.code }), e.verified = !0, h();
81
+ } catch (r) {
82
+ k.error(r.message), e.verified = !1, document.getElementById(`code-input-${g(e.code).length - 1}`)?.focus();
83
+ } finally {
84
+ e.loading = !1;
85
+ }
86
+ });
87
+ return H(y, () => ({
88
+ open: B,
89
+ close: u,
90
+ handleVerifyCode: v
91
+ }), [B, u, v]), /* @__PURE__ */ l(
92
+ ie,
93
+ {
94
+ open: e.openDialog,
95
+ onClose: u,
96
+ fullWidth: !0,
97
+ maxWidth: "sm",
98
+ fullScreen: window.innerWidth < 600,
99
+ children: [
100
+ /* @__PURE__ */ l(oe, { children: [
101
+ /* @__PURE__ */ o(se, { children: /* @__PURE__ */ o(U, { icon: X, fontSize: 24, color: "initial" }) }),
102
+ i("createPasskey")
103
+ ] }),
104
+ /* @__PURE__ */ l(ne, { children: [
105
+ /* @__PURE__ */ o(b, { variant: "body1", children: i("createPasskeyDesc1") }),
106
+ /* @__PURE__ */ o(b, { variant: "body1", children: i("createPasskeyDesc2") }),
107
+ e.error && /* @__PURE__ */ o(Y, { severity: "error", children: e.error }),
108
+ a && /* @__PURE__ */ l(le, { children: [
109
+ !e.sent && /* @__PURE__ */ o(
110
+ F,
111
+ {
112
+ type: "email",
113
+ id: "email-input",
114
+ size: "medium",
115
+ placeholder: i("emailPlaceholder"),
116
+ sx: { width: "100%", ".MuiFormHelperText-root": { ml: 0 } },
117
+ value: e.email,
118
+ onChange: (t) => {
119
+ e.email = t.target.value, s(!m(e.email));
120
+ },
121
+ disabled: e.loading || e.creating,
122
+ required: !0,
123
+ error: T,
124
+ helperText: T ? i("emailInvalid") : ""
125
+ }
126
+ ),
127
+ e.sent && !e.verified && /* @__PURE__ */ l(
128
+ J,
129
+ {
130
+ direction: "column",
131
+ sx: {
132
+ justifyContent: "center",
133
+ alignItems: "center",
134
+ mb: 2
135
+ },
136
+ children: [
137
+ /* @__PURE__ */ o(b, { variant: "body2", children: i("codeSentMessage", { email: e.email }) }),
138
+ /* @__PURE__ */ o(Q, { sx: { width: "100%", display: "flex", justifyContent: "space-between", gap: 2, mt: 0 }, children: Array($).fill("").map((t, r) => /* @__PURE__ */ o(
139
+ F,
140
+ {
141
+ value: g(e.code[r]) || "",
142
+ type: "number",
143
+ margin: "none",
144
+ onChange: (n) => {
145
+ const f = e.code.split("");
146
+ f[r] = n.target.value, e.code = f.join(""), n.target.value && r < 5 && document.getElementById(`code-input-${r + 1}`)?.focus();
147
+ },
148
+ onKeyDown: (n) => {
149
+ n.key === "Backspace" && !g(e.code[r]) && r > 0 && document.getElementById(`code-input-${r - 1}`)?.focus();
150
+ },
151
+ disabled: e.loading,
152
+ required: !0,
153
+ id: `code-input-${r}`,
154
+ autoComplete: "off",
155
+ slotProps: {
156
+ htmlInput: {
157
+ maxLength: 1,
158
+ style: { textAlign: "center", fontSize: "1.5rem" },
159
+ autoComplete: "off"
160
+ }
161
+ }
162
+ },
163
+ `code-input-${r}`
164
+ )) })
165
+ ]
166
+ }
167
+ )
168
+ ] })
169
+ ] }),
170
+ /* @__PURE__ */ l(ae, { children: [
171
+ /* @__PURE__ */ o(x, { onClick: u, sx: { color: "text.secondary" }, children: i("cancel") }),
172
+ a && /* @__PURE__ */ l(
173
+ x,
174
+ {
175
+ variant: "contained",
176
+ onClick: (
177
+ // eslint-disable-next-line no-nested-ternary
178
+ e.verified ? h : e.sent ? v : V
179
+ ),
180
+ disabled: e.verifying || e.creating || !e.email || e.sent && g(e.code).length !== $,
181
+ children: [
182
+ (e.loading || e.creating) && /* @__PURE__ */ o(M, { size: 16, sx: { mr: 1 } }),
183
+ e.verified ? i("createPasskey") : e.sent ? i("verifyButton") : i("sendCodeButton")
184
+ ]
185
+ }
186
+ ),
187
+ !a && /* @__PURE__ */ o(
188
+ x,
189
+ {
190
+ variant: "contained",
191
+ color: "primary",
192
+ onClick: h,
193
+ disabled: e.loading || e.creating || a && (!e.email || !e.verified),
194
+ startIcon: (e.loading || e.creating) && /* @__PURE__ */ o(M, { size: 16, sx: { mr: 1 }, color: "inherit" }),
195
+ children: e.loading || e.creating ? i("creatingPasskey") : i("createPasskey")
196
+ }
197
+ )
198
+ ] })
199
+ ]
200
+ }
201
+ );
202
+ }
203
+ re.propTypes = {
204
+ extraParams: c.object,
205
+ createMode: c.string,
206
+ action: c.string.isRequired,
207
+ onSuccess: c.func,
208
+ onError: c.func,
209
+ ref: c.any.isRequired
210
+ };
211
+ const ie = p(K)`
212
+ .MuiDialog-paper {
213
+ border-radius: 12px;
214
+ max-width: 440px;
215
+ }
216
+ `, oe = p(L)`
217
+ text-align: center;
218
+ padding: 24px 24px 16px;
219
+ font-size: 20px;
220
+ font-weight: 600;
221
+ `, ne = p(G)`
222
+ padding: 0 24px;
223
+
224
+ .MuiTypography-body1 {
225
+ font-size: 14px;
226
+ line-height: 1.5;
227
+ margin-bottom: 16px;
228
+ }
229
+
230
+ .MuiTextField-root {
231
+ margin-top: 16px;
232
+ width: 100%;
233
+ }
234
+ `, ae = p(N)`
235
+ text-align: center;
236
+ padding: 16px 24px 24px;
237
+
238
+ .MuiButton-root {
239
+ text-transform: none;
240
+ font-size: 14px;
241
+ border-radius: 6px;
242
+ padding: 6px 16px;
243
+ }
244
+ `, se = p("div")`
245
+ width: 48px;
246
+ height: 48px;
247
+ margin: 0 auto 16px;
248
+ background-color: #f6f8fa;
249
+ border-radius: 50%;
250
+ display: flex;
251
+ align-items: center;
252
+ justify-content: center;
253
+
254
+ svg {
255
+ width: 32px;
256
+ height: 32px;
257
+ }
258
+ `, le = p.div`
259
+ display: flex;
260
+ flex-direction: column;
261
+ align-items: center;
262
+ justify-content: center;
263
+ width: 100%;
264
+ gap: 24px;
265
+
266
+ input[type='number']::-webkit-inner-spin-button,
267
+ input[type='number']::-webkit-outer-spin-button {
268
+ -webkit-appearance: none;
269
+ margin: 0;
270
+ }
271
+ div.Mui-focused input {
272
+ caret-color: ${(y) => y.theme.palette.primary.main};
273
+ }
274
+ `;
275
+ export {
276
+ re as default
277
+ };
@@ -0,0 +1,13 @@
1
+ import { jsx as t } from "react/jsx-runtime";
2
+ function o(T) {
3
+ return /* @__PURE__ */ t("svg", { xmlns: "http://www.w3.org/2000/svg", width: 24, height: 24, viewBox: "0 0 24 24", ...T, children: /* @__PURE__ */ t(
4
+ "path",
5
+ {
6
+ fill: "currentColor",
7
+ 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"
8
+ }
9
+ ) });
10
+ }
11
+ export {
12
+ o as default
13
+ };
@@ -0,0 +1,9 @@
1
+ import { PasskeyConsumer as o, PasskeyContext as a, PasskeyProvider as r, usePasskey as t } from "./context.js";
2
+ import { default as k } from "./actions.js";
3
+ export {
4
+ k as PasskeyActions,
5
+ o as PasskeyConsumer,
6
+ a as PasskeyContext,
7
+ r as PasskeyProvider,
8
+ t as usePasskey
9
+ };
@@ -0,0 +1,62 @@
1
+ import { joinURL as y } from "ufo";
2
+ import { createAxios as m } from "../utils.js";
3
+ async function g(o, { authServicePrefix: s, serviceHost: i }) {
4
+ if (!o)
5
+ throw new Error("Refresh token not found");
6
+ const u = m({
7
+ baseURL: i,
8
+ timeout: 10 * 1e3,
9
+ secure: !0,
10
+ headers: {
11
+ authorization: `Bearer ${encodeURIComponent(o)}`
12
+ }
13
+ }), { data: a } = await u.post(y(s, "/refreshSession"));
14
+ return a;
15
+ }
16
+ function v({
17
+ refreshTokenStorage: o,
18
+ sessionTokenStorage: s,
19
+ serviceHost: i,
20
+ authServicePrefix: u,
21
+ onRefreshTokenError: a,
22
+ onRefreshTokenSuccess: f
23
+ }, k = {}) {
24
+ let n = null;
25
+ const c = m({
26
+ baseURL: i,
27
+ timeout: 30 * 1e3,
28
+ ...k
29
+ }), { getToken: h, setToken: p, removeToken: T } = s, { getToken: R, setToken: l, removeToken: d } = o;
30
+ return c.interceptors.request.use(
31
+ async (e) => {
32
+ if (s.engine === "ls") {
33
+ const t = h();
34
+ t && (e.headers.authorization = `Bearer ${encodeURIComponent(t)}`);
35
+ }
36
+ return n && await n, e;
37
+ },
38
+ (e) => Promise.reject(e)
39
+ ), c.interceptors.response.use(
40
+ (e) => e,
41
+ async (e) => {
42
+ const t = e.config;
43
+ if (!t)
44
+ return Promise.reject(e);
45
+ if (t.headers = { ...t?.headers }, e?.response?.status === 401 && !t._retry) {
46
+ t._retry = !0, n || (n = g(R(), { serviceHost: i, authServicePrefix: u }));
47
+ try {
48
+ const r = await n;
49
+ return p(r.nextToken), l(r.nextRefreshToken), typeof f == "function" && f(r), c(t);
50
+ } catch (r) {
51
+ return T(), d(), typeof a == "function" && a(), Promise.reject(r);
52
+ } finally {
53
+ n = null;
54
+ }
55
+ }
56
+ return Promise.reject(e);
57
+ }
58
+ ), c;
59
+ }
60
+ export {
61
+ v as default
62
+ };