@arcblock/did-connect-react 3.1.40 → 3.1.42

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 (81) hide show
  1. package/.aigne/doc-smith/config.yaml +83 -0
  2. package/.aigne/doc-smith/output/structure-plan.json +197 -0
  3. package/.aigne/doc-smith/upload-cache.yaml +168 -0
  4. package/docs/_sidebar.md +18 -0
  5. package/docs/advanced-authentication-methods.ja.md +261 -0
  6. package/docs/advanced-authentication-methods.md +261 -0
  7. package/docs/advanced-authentication-methods.zh-TW.md +261 -0
  8. package/docs/advanced-authentication-methods.zh.md +261 -0
  9. package/docs/advanced-utilities.ja.md +132 -0
  10. package/docs/advanced-utilities.md +132 -0
  11. package/docs/advanced-utilities.zh-TW.md +132 -0
  12. package/docs/advanced-utilities.zh.md +132 -0
  13. package/docs/advanced.ja.md +95 -0
  14. package/docs/advanced.md +95 -0
  15. package/docs/advanced.zh-TW.md +95 -0
  16. package/docs/advanced.zh.md +95 -0
  17. package/docs/api-reference.ja.md +178 -0
  18. package/docs/api-reference.md +178 -0
  19. package/docs/api-reference.zh-TW.md +178 -0
  20. package/docs/api-reference.zh.md +178 -0
  21. package/docs/core-components-did-connect.ja.md +214 -0
  22. package/docs/core-components-did-connect.md +213 -0
  23. package/docs/core-components-did-connect.zh-TW.md +214 -0
  24. package/docs/core-components-did-connect.zh.md +213 -0
  25. package/docs/core-components-session-provider.ja.md +239 -0
  26. package/docs/core-components-session-provider.md +239 -0
  27. package/docs/core-components-session-provider.zh-TW.md +239 -0
  28. package/docs/core-components-session-provider.zh.md +239 -0
  29. package/docs/core-components.ja.md +16 -0
  30. package/docs/core-components.md +16 -0
  31. package/docs/core-components.zh-TW.md +16 -0
  32. package/docs/core-components.zh.md +16 -0
  33. package/docs/getting-started.ja.md +138 -0
  34. package/docs/getting-started.md +138 -0
  35. package/docs/getting-started.zh-TW.md +138 -0
  36. package/docs/getting-started.zh.md +138 -0
  37. package/docs/hooks-use-connect.ja.md +214 -0
  38. package/docs/hooks-use-connect.md +214 -0
  39. package/docs/hooks-use-connect.zh-TW.md +214 -0
  40. package/docs/hooks-use-connect.zh.md +214 -0
  41. package/docs/hooks-use-did.ja.md +107 -0
  42. package/docs/hooks-use-did.md +107 -0
  43. package/docs/hooks-use-did.zh-TW.md +107 -0
  44. package/docs/hooks-use-did.zh.md +107 -0
  45. package/docs/hooks-use-oauth-passkey.ja.md +188 -0
  46. package/docs/hooks-use-oauth-passkey.md +188 -0
  47. package/docs/hooks-use-oauth-passkey.zh-TW.md +188 -0
  48. package/docs/hooks-use-oauth-passkey.zh.md +188 -0
  49. package/docs/hooks.ja.md +23 -0
  50. package/docs/hooks.md +23 -0
  51. package/docs/hooks.zh-TW.md +23 -0
  52. package/docs/hooks.zh.md +23 -0
  53. package/docs/overview.ja.md +159 -0
  54. package/docs/overview.md +159 -0
  55. package/docs/overview.zh-TW.md +159 -0
  56. package/docs/overview.zh.md +160 -0
  57. package/docs/ui-components-address.ja.md +121 -0
  58. package/docs/ui-components-address.md +121 -0
  59. package/docs/ui-components-address.zh-TW.md +121 -0
  60. package/docs/ui-components-address.zh.md +121 -0
  61. package/docs/ui-components-avatar.ja.md +65 -0
  62. package/docs/ui-components-avatar.md +65 -0
  63. package/docs/ui-components-avatar.zh-TW.md +65 -0
  64. package/docs/ui-components-avatar.zh.md +65 -0
  65. package/docs/ui-components-button.ja.md +99 -0
  66. package/docs/ui-components-button.md +99 -0
  67. package/docs/ui-components-button.zh-TW.md +99 -0
  68. package/docs/ui-components-button.zh.md +99 -0
  69. package/docs/ui-components-logo.ja.md +52 -0
  70. package/docs/ui-components-logo.md +52 -0
  71. package/docs/ui-components-logo.zh-TW.md +52 -0
  72. package/docs/ui-components-logo.zh.md +52 -0
  73. package/docs/ui-components.ja.md +57 -0
  74. package/docs/ui-components.md +57 -0
  75. package/docs/ui-components.zh-TW.md +57 -0
  76. package/docs/ui-components.zh.md +57 -0
  77. package/glossary.md +1 -0
  78. package/lib/package.json.js +1 -1
  79. package/package.json +5 -5
  80. package/src/Session/hooks/use-federated.js +3 -0
  81. package/src/Session/libs/federated.js +3 -0
@@ -0,0 +1,213 @@
1
+ # Connection UI (DidConnect)
2
+
3
+ The `DidConnect` component is a pre-built, comprehensive UI solution for handling various decentralized identity (DID) connection flows. It provides a user-friendly interface for actions like login, authentication, and profile requests, supporting multiple connection methods out-of-the-box.
4
+
5
+ It serves as the visual centerpiece of the `@arcblock/did-connect-react` library, abstracting away the complexity of session management and real-time status updates. The component displays a QR code for DID Wallet users and offers alternative login options such as social logins (OAuth) and passwordless authentication (Passkeys).
6
+
7
+ Before using `DidConnect`, ensure you have wrapped your application with the [`SessionProvider`](./core-components-session-provider.md) to manage the overall session state.
8
+
9
+ ## Basic Usage
10
+
11
+ To use `DidConnect`, you need to import it and provide a few essential props. The component is typically controlled by a state variable that determines its visibility.
12
+
13
+ ```jsx DID Connect Example icon=logos:react
14
+ import React, { useState } from 'react';
15
+ import axios from 'axios';
16
+ import DidConnect from '@arcblock/did-connect-react/lib/Connect';
17
+
18
+ function App() {
19
+ const [isConnectOpen, setConnectOpen] = useState(false);
20
+
21
+ const handleClose = () => setConnectOpen(false);
22
+ const handleSuccess = () => {
23
+ // Handle successful login, e.g., redirect the user
24
+ window.location.href = '/profile';
25
+ };
26
+
27
+ return (
28
+ <div>
29
+ <button onClick={() => setConnectOpen(true)}>Login with DID</button>
30
+ {isConnectOpen && (
31
+ <DidConnect
32
+ action="login"
33
+ checkFn={axios.get}
34
+ onClose={handleClose}
35
+ onSuccess={handleSuccess}
36
+ messages={{
37
+ title: 'Scan to Sign In',
38
+ scan: 'Scan QR code with your DID Wallet',
39
+ confirm: 'Confirm login on your DID Wallet',
40
+ success: 'You have successfully signed in!',
41
+ }}
42
+ />
43
+ )}
44
+ </div>
45
+ );
46
+ }
47
+
48
+ export default App;
49
+ ```
50
+
51
+ In this example, clicking the "Login with DID" button sets the `isConnectOpen` state to `true`, which renders the `DidConnect` component as a modal dialog. The `checkFn` prop is crucial, as it's the function the component uses to poll for the session status from your backend.
52
+
53
+ ## How It Works
54
+
55
+ The `DidConnect` component orchestrates the entire user authentication flow, from displaying connection options to handling the final success or error state.
56
+
57
+ ```d2
58
+ direction: down
59
+
60
+ User: {
61
+ shape: c4-person
62
+ }
63
+
64
+ Application: {
65
+ label: "React Application"
66
+ shape: rectangle
67
+
68
+ Login-Button: {
69
+ label: "Login Button"
70
+ }
71
+
72
+ DidConnect-UI: {
73
+ label: "DidConnect Component"
74
+ shape: rectangle
75
+
76
+ QR-Code: {
77
+ label: "QR Code"
78
+ }
79
+
80
+ Login-Methods: {
81
+ label: "Other Login Methods\n(OAuth, Passkey)"
82
+ }
83
+ }
84
+ }
85
+
86
+ Backend: {
87
+ label: "Backend Server"
88
+ shape: rectangle
89
+ }
90
+
91
+ DID-Wallet: {
92
+ label: "DID Wallet"
93
+ icon: "https://www.arcblock.io/image-bin/uploads/37198ddc4a0b9e91e5c1c821ab895a34.svg"
94
+ }
95
+
96
+ User -> Application.Login-Button: "1. Clicks Login"
97
+ Application.Login-Button -> Application.DidConnect-UI: "2. Renders UI"
98
+ Application.DidConnect-UI -> Backend: "3. Creates session, starts polling via checkFn"
99
+ User -> DID-Wallet: "4a. Scans QR Code"
100
+ DID-Wallet -> Backend: "5. Sends approval"
101
+ User -> Application.DidConnect-UI.Login-Methods: "4b. Clicks OAuth/Passkey"
102
+ Application.DidConnect-UI.Login-Methods -> Backend: "5. Authenticates"
103
+ Backend -> Application.DidConnect-UI: "6. Returns success status"
104
+ Application.DidConnect-UI -> User: "7. Calls onSuccess & closes UI"
105
+
106
+ ```
107
+
108
+ ## Component Props
109
+
110
+ The `DidConnect` component is highly configurable through its props. Below is a detailed list of the available options.
111
+
112
+ ### Core Configuration
113
+
114
+ These props are essential for setting up the component's basic functionality.
115
+
116
+ <x-field-group>
117
+ <x-field data-name="action" data-type="string" data-required="true">
118
+ <x-field-desc markdown>Defines the purpose of the connection, such as `login`, `claim`, `sign`, etc. This string is passed to your backend to determine the required actions.</x-field-desc>
119
+ </x-field>
120
+ <x-field data-name="checkFn" data-type="function" data-required="true">
121
+ <x-field-desc markdown>The function used to poll the backend for session status updates. It should be a function that makes an API request, like `axios.get`.</x-field-desc>
122
+ </x-field>
123
+ <x-field data-name="baseUrl" data-type="string" data-default="''">
124
+ <x-field-desc markdown>The base URL for the API endpoints. If your API is on a different domain, specify it here.</x-field-desc>
125
+ </x-field>
126
+ <x-field data-name="prefix" data-type="string" data-default="/api/did">
127
+ <x-field-desc markdown>The URL prefix for the DID Connect API endpoints on your backend.</x-field-desc>
128
+ </x-field>
129
+ </x-field-group>
130
+
131
+ ### Behavior Control
132
+
133
+ These props control how the connection process behaves.
134
+
135
+ <x-field-group>
136
+ <x-field data-name="enabledConnectTypes" data-type="string[]" data-default='["web", "mobile", ...]'>
137
+ <x-field-desc markdown>An array of strings specifying which connection methods to display. Possible values include `web`, `mobile`, `github`, `google`, `apple`, `passkey`, etc.</x-field-desc>
138
+ </x-field>
139
+ <x-field data-name="passkeyBehavior" data-type="'none' | 'both' | 'only-existing' | 'only-new'" data-default="'none'">
140
+ <x-field-desc markdown>Controls the behavior of Passkey authentication. `none` disables it, `both` allows creating new passkeys and using existing ones, `only-existing` restricts to existing passkeys, and `only-new` forces new passkey creation.</x-field-desc>
141
+ </x-field>
142
+ <x-field data-name="allowWallet" data-type="boolean" data-default="true">
143
+ <x-field-desc markdown>If `false`, the QR code and mobile wallet connection options will be hidden.</x-field-desc>
144
+ </x-field>
145
+ <x-field data-name="autoConnect" data-type="boolean" data-default="true">
146
+ <x-field-desc markdown>If `true`, the component will automatically try to use a previously established connection from the same device.</x-field-desc>
147
+ </x-field>
148
+ <x-field data-name="forceConnected" data-type="boolean | string" data-default="true">
149
+ <x-field-desc markdown>If `true`, a user must connect with the same DID they are already logged in with. If a DID string is provided, the user must connect with that specific DID.</x-field-desc>
150
+ </x-field>
151
+ <x-field data-name="saveConnect" data-type="boolean" data-default="true">
152
+ <x-field-desc markdown>If `true`, the successful connection information is saved locally for future `autoConnect` attempts.</x-field-desc>
153
+ </x-field>
154
+ <x-field data-name="useSocket" data-type="boolean" data-default="true">
155
+ <x-field-desc markdown>If `true`, attempts to use a WebSocket for real-time status updates, falling back to polling if a connection cannot be established.</x-field-desc>
156
+ </x-field>
157
+ </x-field-group>
158
+
159
+ ### UI Customization
160
+
161
+ Customize the appearance and text of the `DidConnect` component.
162
+
163
+ <x-field-group>
164
+ <x-field data-name="mode" data-type="'dialog' | 'drawer' | 'page'" data-default="'dialog'">
165
+ <x-field-desc markdown>Determines how the component is displayed. `dialog` shows it as a centered modal, `drawer` as a bottom sheet (on mobile), and `page` embeds it directly into the document flow.</x-field-desc>
166
+ </x-field>
167
+ <x-field data-name="messages" data-type="object">
168
+ <x-field-desc markdown>An object to customize the text displayed in the UI.</x-field-desc>
169
+ <x-field data-name="title" data-type="string" data-desc="The main title at the top."></x-field>
170
+ <x-field data-name="scan" data-type="string" data-desc="The instructional text below the title."></x-field>
171
+ <x-field data-name="confirm" data-type="string" data-desc="Text shown after the QR code is scanned."></x-field>
172
+ <x-field data-name="success" data-type="string | ReactNode" data-desc="Message displayed upon successful connection."></x-field>
173
+ <x-field data-name="error" data-type="string" data-desc="Message displayed on error."></x-field>
174
+ </x-field>
175
+ <x-field data-name="hideCloseButton" data-type="boolean" data-default="false">
176
+ <x-field-desc markdown>If `true`, the close button (X) in the top-right corner is hidden.</x-field-desc>
177
+ </x-field>
178
+ <x-field data-name="extraContent" data-type="ReactNode" data-default="null">
179
+ <x-field-desc markdown>Allows you to inject a custom React component or element into the UI, typically appearing below the main title/description.</x-field-desc>
180
+ </x-field>
181
+ <x-field data-name="customItems" data-type="ReactNode[]" data-default="[]">
182
+ <x-field-desc markdown>An array of custom React nodes to be added to the list of connection methods.</x-field-desc>
183
+ </x-field>
184
+ <x-field data-name="disableSwitchApp" data-type="boolean" data-default="false">
185
+ <x-field-desc markdown>If you are using Federated Login, this prop prevents users from switching between different applications in the group during the login process.</x-field-desc>
186
+ </x-field>
187
+ <x-field data-name="webWalletUrl" data-type="string">
188
+ <x-field-desc markdown>The URL of the web-based DID Wallet to be used for the "Connect with Web Wallet" option. Defaults to the official ArcBlock web wallet.</x-field-desc>
189
+ </x-field>
190
+ </x-field-group>
191
+
192
+ ### Callbacks
193
+
194
+ Functions that are called at different stages of the connection lifecycle.
195
+
196
+ <x-field-group>
197
+ <x-field data-name="onClose" data-type="function" data-required="true">
198
+ <x-field-desc markdown>Callback function that is executed when the user closes the `DidConnect` dialog (e.g., by clicking the close button or outside the modal).</x-field-desc>
199
+ </x-field>
200
+ <x-field data-name="onSuccess" data-type="function" data-required="true">
201
+ <x-field-desc markdown>Callback function that is executed upon a successful connection. The session data is passed as an argument.</x-field-desc>
202
+ </x-field>
203
+ <x-field data-name="onError" data-type="function">
204
+ <x-field-desc markdown>Callback function that is executed when an error occurs during the process. The error object or message is passed as an argument.</x-field-desc>
205
+ </x-field>
206
+ <x-field data-name="onRecreateSession" data-type="function">
207
+ <x-field-desc markdown>A callback function that is invoked when the session needs to be reset, for example, after a timeout or when the user manually cancels and retries.</x-field-desc>
208
+ </x-field>
209
+ </x-field-group>
210
+
211
+ ## Next Steps
212
+
213
+ With a solid understanding of the `DidConnect` component, you can now build robust authentication experiences. For more programmatic control over the connection UI, explore the [`useConnect` hook](./hooks-use-connect.md), which allows you to open and close the `DidConnect` modal from anywhere in your application.
@@ -0,0 +1,214 @@
1
+ # 連線 UI (DidConnect)
2
+
3
+ `DidConnect` 元件是一個預先建置、全面的 UI 解決方案,用於處理各種去中心化身分 (DID) 連線流程。它為登入、驗證和個人資料請求等操作提供了一個使用者友善的介面,並支援多種開箱即用的連線方法。
4
+
5
+ 它是 `@arcblock/did-connect-react` 函式庫的視覺核心,抽象化了會話管理和即時狀態更新的複雜性。該元件為 DID Wallet 使用者顯示 QR 碼,並提供其他登入選項,例如社交登入 (OAuth) 和無密碼驗證 (Passkeys)。
6
+
7
+ 在使用 `DidConnect` 之前,請確保您已使用 [`SessionProvider`](./core-components-session-provider.md) 包裹您的應用程式,以管理整體會話狀態。
8
+
9
+ ## 基本用法
10
+
11
+ 要使用 `DidConnect`,您需要匯入它並提供一些必要的 props。該元件通常由一個決定其可見性的狀態變數控制。
12
+
13
+ ```jsx DID Connect Example icon=logos:react
14
+ import React, { useState } from 'react';
15
+ import axios from 'axios';
16
+ import DidConnect from '@arcblock/did-connect-react/lib/Connect';
17
+
18
+ function App() {
19
+ const [isConnectOpen, setConnectOpen] = useState(false);
20
+
21
+ const handleClose = () => setConnectOpen(false);
22
+ const handleSuccess = () => {
23
+ // 處理成功登入,例如,重新導向使用者
24
+ window.location.href = '/profile';
25
+ };
26
+
27
+ return (
28
+ <div>
29
+ <button onClick={() => setConnectOpen(true)}>使用 DID 登入</button>
30
+ {isConnectOpen && (
31
+ <DidConnect
32
+ action="login"
33
+ checkFn={axios.get}
34
+ onClose={handleClose}
35
+ onSuccess={handleSuccess}
36
+ messages={{
37
+ title: '掃描以登入',
38
+ scan: '使用您的 DID Wallet 掃描 QR 碼',
39
+ confirm: '在您的 DID Wallet 上確認登入',
40
+ success: '您已成功登入!',
41
+ }}
42
+ />
43
+ )}
44
+ </div>
45
+ );
46
+ }
47
+
48
+ export default App;
49
+ ```
50
+
51
+ 在此範例中,點擊「使用 DID 登入」按鈕會將 `isConnectOpen` 狀態設定為 `true`,這會將 `DidConnect` 元件渲染為一個強制回應對話方塊。`checkFn` prop 至關重要,因為它是該元件用來從您的後端輪詢會話狀態的函式。
52
+
53
+ ## 運作方式
54
+
55
+ `DidConnect` 元件協調了整個使用者驗證流程,從顯示連線選項到處理最終的成功或錯誤狀態。
56
+
57
+ ```d2
58
+ direction: down
59
+
60
+ User: {
61
+ label: "使用者"
62
+ shape: c4-person
63
+ }
64
+
65
+ Application: {
66
+ label: "React 應用程式"
67
+ shape: rectangle
68
+
69
+ Login-Button: {
70
+ label: "登入按鈕"
71
+ }
72
+
73
+ DidConnect-UI: {
74
+ label: "DidConnect 元件"
75
+ shape: rectangle
76
+
77
+ QR-Code: {
78
+ label: "QR 碼"
79
+ }
80
+
81
+ Login-Methods: {
82
+ label: "其他登入方式\n(OAuth, Passkey)"
83
+ }
84
+ }
85
+ }
86
+
87
+ Backend: {
88
+ label: "後端伺服器"
89
+ shape: rectangle
90
+ }
91
+
92
+ DID-Wallet: {
93
+ label: "DID Wallet"
94
+ icon: "https://www.arcblock.io/image-bin/uploads/37198ddc4a0b9e91e5c1c821ab895a34.svg"
95
+ }
96
+
97
+ User -> Application.Login-Button: "1. 點擊登入"
98
+ Application.Login-Button -> Application.DidConnect-UI: "2. 渲染 UI"
99
+ Application.DidConnect-UI -> Backend: "3. 建立會話,透過 checkFn 開始輪詢"
100
+ User -> DID-Wallet: "4a. 掃描 QR 碼"
101
+ DID-Wallet -> Backend: "5. 發送批准"
102
+ User -> Application.DidConnect-UI.Login-Methods: "4b. 點擊 OAuth/Passkey"
103
+ Application.DidConnect-UI.Login-Methods -> Backend: "5. 進行驗證"
104
+ Backend -> Application.DidConnect-UI: "6. 返回成功狀態"
105
+ Application.DidConnect-UI -> User: "7. 呼叫 onSuccess 並關閉 UI"
106
+
107
+ ```
108
+
109
+ ## 元件 Props
110
+
111
+ `DidConnect` 元件可透過其 props 進行高度設定。以下是可用選項的詳細列表。
112
+
113
+ ### 核心設定
114
+
115
+ 這些 props 對於設定元件的基本功能至關重要。
116
+
117
+ <x-field-group>
118
+ <x-field data-name="action" data-type="string" data-required="true">
119
+ <x-field-desc markdown>定義連線的目的,例如 `login`、`claim`、`sign` 等。此字串會傳遞到您的後端以確定所需的操作。</x-field-desc>
120
+ </x-field>
121
+ <x-field data-name="checkFn" data-type="function" data-required="true">
122
+ <x-field-desc markdown>用於輪詢後端以獲取會話狀態更新的函式。它應該是一個發出 API 請求的函式,例如 `axios.get`。</x-field-desc>
123
+ </x-field>
124
+ <x-field data-name="baseUrl" data-type="string" data-default="''">
125
+ <x-field-desc markdown>API 端點的基礎 URL。如果您的 API 在不同的網域上,請在此處指定。</x-field-desc>
126
+ </x-field>
127
+ <x-field data-name="prefix" data-type="string" data-default="/api/did">
128
+ <x-field-desc markdown>您後端 DID Connect API 端點的 URL 前綴。</x-field-desc>
129
+ </x-field>
130
+ </x-field-group>
131
+
132
+ ### 行為控制
133
+
134
+ 這些 props 控制連線過程的行為方式。
135
+
136
+ <x-field-group>
137
+ <x-field data-name="enabledConnectTypes" data-type="string[]" data-default='["web", "mobile", ...]'>
138
+ <x-field-desc markdown>一個字串陣列,指定要顯示哪些連線方法。可能的值包括 `web`、`mobile`、`github`、`google`、`apple`、`passkey` 等。</x-field-desc>
139
+ </x-field>
140
+ <x-field data-name="passkeyBehavior" data-type="'none' | 'both' | 'only-existing' | 'only-new'" data-default="'none'">
141
+ <x-field-desc markdown>控制 Passkey 驗證的行為。`none` 禁用它,`both` 允許建立新的 passkey 並使用現有的,`only-existing` 限制為使用現有的 passkey,而 `only-new` 強制建立新的 passkey。</x-field-desc>
142
+ </x-field>
143
+ <x-field data-name="allowWallet" data-type="boolean" data-default="true">
144
+ <x-field-desc markdown>如果為 `false`,QR 碼和行動錢包連線選項將被隱藏。</x-field-desc>
145
+ </x-field>
146
+ <x-field data-name="autoConnect" data-type="boolean" data-default="true">
147
+ <x-field-desc markdown>如果為 `true`,元件將自動嘗試使用來自同一裝置的先前建立的連線。</x-field-desc>
148
+ </x-field>
149
+ <x-field data-name="forceConnected" data-type="boolean | string" data-default="true">
150
+ <x-field-desc markdown>如果為 `true`,使用者必須使用他們已經登入的同一個 DID 進行連線。如果提供了一個 DID 字串,使用者必須使用該特定的 DID 進行連線。</x-field-desc>
151
+ </x-field>
152
+ <x-field data-name="saveConnect" data-type="boolean" data-default="true">
153
+ <x-field-desc markdown>如果為 `true`,成功的連線資訊將被儲存在本地,以供將來的 `autoConnect` 嘗試使用。</x-field-desc>
154
+ </x-field>
155
+ <x-field data-name="useSocket" data-type="boolean" data-default="true">
156
+ <x-field-desc markdown>如果為 `true`,則嘗試使用 WebSocket 進行即時狀態更新,如果無法建立連線,則退回到輪詢方式。</x-field-desc>
157
+ </x-field>
158
+ </x-field-group>
159
+
160
+ ### UI 客製化
161
+
162
+ 客製化 `DidConnect` 元件的外觀和文字。
163
+
164
+ <x-field-group>
165
+ <x-field data-name="mode" data-type="'dialog' | 'drawer' | 'page'" data-default="'dialog'">
166
+ <x-field-desc markdown>決定元件的顯示方式。`dialog` 將其顯示為置中的強制回應視窗,`drawer` 將其顯示為底部彈出視窗(在行動裝置上),而 `page` 將其直接嵌入到文件流中。</x-field-desc>
167
+ </x-field>
168
+ <x-field data-name="messages" data-type="object">
169
+ <x-field-desc markdown>一個用於客製化 UI 中顯示文字的物件。</x-field-desc>
170
+ <x-field data-name="title" data-type="string" data-desc="頂部的主標題。"></x-field>
171
+ <x-field data-name="scan" data-type="string" data-desc="標題下方的說明文字。"></x-field>
172
+ <x-field data-name="confirm" data-type="string" data-desc="QR 碼掃描後顯示的文字。"></x-field>
173
+ <x-field data-name="success" data-type="string | ReactNode" data-desc="成功連線後顯示的訊息。"></x-field>
174
+ <x-field data-name="error" data-type="string" data-desc="發生錯誤時顯示的訊息。"></x-field>
175
+ </x-field>
176
+ <x-field data-name="hideCloseButton" data-type="boolean" data-default="false">
177
+ <x-field-desc markdown>如果為 `true`,右上角的關閉按鈕(X)將被隱藏。</x-field-desc>
178
+ </x-field>
179
+ <x-field data-name="extraContent" data-type="ReactNode" data-default="null">
180
+ <x-field-desc markdown>允許您將自訂的 React 元件或元素注入到 UI 中,通常顯示在主標題/描述下方。</x-field-desc>
181
+ </x-field>
182
+ <x-field data-name="customItems" data-type="ReactNode[]" data-default="[]">
183
+ <x-field-desc markdown>一個要新增到連線方法列表中的自訂 React 節點陣列。</x-field-desc>
184
+ </x-field>
185
+ <x-field data-name="disableSwitchApp" data-type="boolean" data-default="false">
186
+ <x-field-desc markdown>如果您正在使用統一登入站點群,此 prop 可防止使用者在登入過程中在群組中的不同應用程式之間切換。</x-field-desc>
187
+ </x-field>
188
+ <x-field data-name="webWalletUrl" data-type="string">
189
+ <x-field-desc markdown>用於「使用網頁錢包連線」選項的網頁版 DID Wallet 的 URL。預設為官方的 ArcBlock 網頁錢包。</x-field-desc>
190
+ </x-field>
191
+ </x-field-group>
192
+
193
+ ### 回呼函式
194
+
195
+ 在連線生命週期的不同階段被呼叫的函式。
196
+
197
+ <x-field-group>
198
+ <x-field data-name="onClose" data-type="function" data-required="true">
199
+ <x-field-desc markdown>當使用者關閉 `DidConnect` 對話方塊時(例如,透過點擊關閉按鈕或在強制回應視窗外點擊)執行的回呼函式。</x-field-desc>
200
+ </x-field>
201
+ <x-field data-name="onSuccess" data-type="function" data-required="true">
202
+ <x-field-desc markdown>成功連線後執行的回呼函式。會話資料會作為參數傳遞。</x-field-desc>
203
+ </x-field>
204
+ <x-field data-name="onError" data-type="function">
205
+ <x-field-desc markdown>在過程中發生錯誤時執行的回呼函式。錯誤物件或訊息會作為參數傳遞。</x-field-desc>
206
+ </x-field>
207
+ <x-field data-name="onRecreateSession" data-type="function">
208
+ <x-field-desc markdown>當需要重設會話時呼叫的回呼函式,例如,在超時後或當使用者手動取消並重試時。</x-field-desc>
209
+ </x-field>
210
+ </x-field-group>
211
+
212
+ ## 後續步驟
213
+
214
+ 在對 `DidConnect` 元件有了深入的了解後,您現在可以建構穩健的驗證體驗。若要對連線 UI 進行更多的程式化控制,請探索 [`useConnect` hook](./hooks-use-connect.md),它允許您從應用程式的任何地方開啟和關閉 `DidConnect` 強制回應視窗。
@@ -0,0 +1,213 @@
1
+ # 连接 UI (DidConnect)
2
+
3
+ `DidConnect` 组件是一个预构建的、全面的 UI 解决方案,用于处理各种去中心化身份(DID)连接流程。它为登录、身份验证和个人资料请求等操作提供了用户友好的界面,并开箱即用地支持多种连接方法。
4
+
5
+ 它作为 `@arcblock/did-connect-react` 库的视觉核心,抽象了会话管理和实时状态更新的复杂性。该组件显示一个供 DID 钱包用户使用的二维码,并提供其他登录选项,如社交登录(OAuth)和无密码身份验证(Passkeys)。
6
+
7
+ 在使用 `DidConnect` 之前,请确保您已使用 [`SessionProvider`](./core-components-session-provider.md) 包装您的应用程序,以管理整体会话状态。
8
+
9
+ ## 基本用法
10
+
11
+ 要使用 `DidConnect`,您需要导入它并提供一些必要的 props。该组件通常由一个决定其可见性的状态变量控制。
12
+
13
+ ```jsx DID Connect Example icon=logos:react
14
+ import React, { useState } from 'react';
15
+ import axios from 'axios';
16
+ import DidConnect from '@arcblock/did-connect-react/lib/Connect';
17
+
18
+ function App() {
19
+ const [isConnectOpen, setConnectOpen] = useState(false);
20
+
21
+ const handleClose = () => setConnectOpen(false);
22
+ const handleSuccess = () => {
23
+ // 处理成功登录,例如,重定向用户
24
+ window.location.href = '/profile';
25
+ };
26
+
27
+ return (
28
+ <div>
29
+ <button onClick={() => setConnectOpen(true)}>使用 DID 登录</button>
30
+ {isConnectOpen && (
31
+ <DidConnect
32
+ action="login"
33
+ checkFn={axios.get}
34
+ onClose={handleClose}
35
+ onSuccess={handleSuccess}
36
+ messages={{
37
+ title: '扫描以登录',
38
+ scan: '使用您的 DID 钱包扫描二维码',
39
+ confirm: '在您的 DID 钱包上确认登录',
40
+ success: '您已成功登录!',
41
+ }}
42
+ />
43
+ )}
44
+ </div>
45
+ );
46
+ }
47
+
48
+ export default App;
49
+ ```
50
+
51
+ 在此示例中,单击“使用 DID 登录”按钮会将 `isConnectOpen` 状态设置为 `true`,这会将 `DidConnect` 组件渲染为模态对话框。`checkFn` 属性至关重要,因为它是组件用来从您的后端轮询会话状态的函数。
52
+
53
+ ## 工作原理
54
+
55
+ `DidConnect` 组件协调整个用户身份验证流程,从显示连接选项到处理最终的成功或错误状态。
56
+
57
+ ```d2
58
+ direction: down
59
+
60
+ User: {
61
+ shape: c4-person
62
+ }
63
+
64
+ Application: {
65
+ label: "React Application"
66
+ shape: rectangle
67
+
68
+ Login-Button: {
69
+ label: "Login Button"
70
+ }
71
+
72
+ DidConnect-UI: {
73
+ label: "DidConnect Component"
74
+ shape: rectangle
75
+
76
+ QR-Code: {
77
+ label: "QR Code"
78
+ }
79
+
80
+ Login-Methods: {
81
+ label: "Other Login Methods\n(OAuth, Passkey)"
82
+ }
83
+ }
84
+ }
85
+
86
+ Backend: {
87
+ label: "Backend Server"
88
+ shape: rectangle
89
+ }
90
+
91
+ DID-Wallet: {
92
+ label: "DID Wallet"
93
+ icon: "https://www.arcblock.io/image-bin/uploads/37198ddc4a0b9e91e5c1c821ab895a34.svg"
94
+ }
95
+
96
+ User -> Application.Login-Button: "1. Clicks Login"
97
+ Application.Login-Button -> Application.DidConnect-UI: "2. Renders UI"
98
+ Application.DidConnect-UI -> Backend: "3. Creates session, starts polling via checkFn"
99
+ User -> DID-Wallet: "4a. Scans QR Code"
100
+ DID-Wallet -> Backend: "5. Sends approval"
101
+ User -> Application.DidConnect-UI.Login-Methods: "4b. Clicks OAuth/Passkey"
102
+ Application.DidConnect-UI.Login-Methods -> Backend: "5. Authenticates"
103
+ Backend -> Application.DidConnect-UI: "6. Returns success status"
104
+ Application.DidConnect-UI -> User: "7. Calls onSuccess & closes UI"
105
+
106
+ ```
107
+
108
+ ## 组件属性
109
+
110
+ `DidConnect` 组件通过其 props 高度可配置。以下是可用选项的详细列表。
111
+
112
+ ### 核心配置
113
+
114
+ 这些 props 对于设置组件的基本功能至关重要。
115
+
116
+ <x-field-group>
117
+ <x-field data-name="action" data-type="string" data-required="true">
118
+ <x-field-desc markdown>定义连接的目的,例如 `login`、`claim`、`sign` 等。此字符串会传递给您的后端,以确定所需的操作。</x-field-desc>
119
+ </x-field>
120
+ <x-field data-name="checkFn" data-type="function" data-required="true">
121
+ <x-field-desc markdown>用于向后端轮询会话状态更新的函数。它应该是一个发出 API 请求的函数,例如 `axios.get`。</x-field-desc>
122
+ </x-field>
123
+ <x-field data-name="baseUrl" data-type="string" data-default="''">
124
+ <x-field-desc markdown>API 端点的基础 URL。如果您的 API 位于不同的域上,请在此处指定。</x-field-desc>
125
+ </x-field>
126
+ <x-field data-name="prefix" data-type="string" data-default="/api/did">
127
+ <x-field-desc markdown>您后端上 DID Connect API 端点的 URL 前缀。</x-field-desc>
128
+ </x-field>
129
+ </x-field-group>
130
+
131
+ ### 行为控制
132
+
133
+ 这些 props 控制连接过程的行为方式。
134
+
135
+ <x-field-group>
136
+ <x-field data-name="enabledConnectTypes" data-type="string[]" data-default='["web", "mobile", ...]'>
137
+ <x-field-desc markdown>一个字符串数组,指定要显示的连接方法。可能的值包括 `web`、`mobile`、`github`、`google`、`apple`、`passkey` 等。</x-field-desc>
138
+ </x-field>
139
+ <x-field data-name="passkeyBehavior" data-type="'none' | 'both' | 'only-existing' | 'only-new'" data-default="'none'">
140
+ <x-field-desc markdown>控制 Passkey 身份验证的行为。`none` 表示禁用,`both` 允许创建新密钥和使用现有密钥,`only-existing` 限制为仅使用现有密钥,`only-new` 强制创建新密钥。</x-field-desc>
141
+ </x-field>
142
+ <x-field data-name="allowWallet" data-type="boolean" data-default="true">
143
+ <x-field-desc markdown>如果为 `false`,二维码和移动钱包连接选项将被隐藏。</x-field-desc>
144
+ </x-field>
145
+ <x-field data-name="autoConnect" data-type="boolean" data-default="true">
146
+ <x-field-desc markdown>如果为 `true`,组件将自动尝试使用来自同一设备的先前已建立的连接。</x-field-desc>
147
+ </x-field>
148
+ <x-field data-name="forceConnected" data-type="boolean | string" data-default="true">
149
+ <x-field-desc markdown>如果为 `true`,用户必须使用他们已经登录的同一个 DID 进行连接。如果提供了一个 DID 字符串,用户必须使用该特定的 DID 进行连接。</x-field-desc>
150
+ </x-field>
151
+ <x-field data-name="saveConnect" data-type="boolean" data-default="true">
152
+ <x-field-desc markdown>如果为 `true`,成功的连接信息将保存在本地,以供将来的 `autoConnect` 尝试使用。</x-field-desc>
153
+ </x-field>
154
+ <x-field data-name="useSocket" data-type="boolean" data-default="true">
155
+ <x-field-desc markdown>如果为 `true`,则尝试使用 WebSocket 进行实时状态更新,如果无法建立连接,则回退到轮询。</x-field-desc>
156
+ </x-field>
157
+ </x-field-group>
158
+
159
+ ### UI 自定义
160
+
161
+ 自定义 `DidConnect` 组件的外观和文本。
162
+
163
+ <x-field-group>
164
+ <x-field data-name="mode" data-type="'dialog' | 'drawer' | 'page'" data-default="'dialog'">
165
+ <x-field-desc markdown>决定组件的显示方式。`dialog` 将其显示为居中模态框,`drawer` 将其显示为底部抽屉(在移动设备上),`page` 将其直接嵌入到文档流中。</x-field-desc>
166
+ </x-field>
167
+ <x-field data-name="messages" data-type="object">
168
+ <x-field-desc markdown>一个用于自定义 UI 中显示的文本的对象。</x-field-desc>
169
+ <x-field data-name="title" data-type="string" data-desc="顶部的​​主标题。"></x-field>
170
+ <x-field data-name="scan" data-type="string" data-desc="标题下方的说明文字。"></x-field>
171
+ <x-field data-name="confirm" data-type="string" data-desc="扫描二维码后显示的文本。"></x-field>
172
+ <x-field data-name="success" data-type="string | ReactNode" data-desc="连接成功时显示的消息。"></x-field>
173
+ <x-field data-name="error" data-type="string" data-desc="出错时显示的消息。"></x-field>
174
+ </x-field>
175
+ <x-field data-name="hideCloseButton" data-type="boolean" data-default="false">
176
+ <x-field-desc markdown>如果为 `true`,右上角的关闭按钮(X)将被隐藏。</x-field-desc>
177
+ </x-field>
178
+ <x-field data-name="extraContent" data-type="ReactNode" data-default="null">
179
+ <x-field-desc markdown>允许您将自定义的 React 组件或元素注入到 UI 中,通常显示在主标题/描述下方。</x-field-desc>
180
+ </x-field>
181
+ <x-field data-name="customItems" data-type="ReactNode[]" data-default="[]">
182
+ <x-field-desc markdown>一个自定义 React 节点数组,将被添加到连接方法列表中。</x-field-desc>
183
+ </x-field>
184
+ <x-field data-name="disableSwitchApp" data-type="boolean" data-default="false">
185
+ <x-field-desc markdown>如果您正在使用统一登录站点群,此属性可防止用户在登录过程中在站点群中的不同应用程序之间切换。</x-field-desc>
186
+ </x-field>
187
+ <x-field data-name="webWalletUrl" data-type="string">
188
+ <x-field-desc markdown>用于“使用网页钱包连接”选项的基于 Web 的 DID 钱包的 URL。默认为官方的 ArcBlock 网页钱包。</x-field-desc>
189
+ </x-field>
190
+ </x-field-group>
191
+
192
+ ### 回调函数
193
+
194
+ 在连接生命周期的不同阶段调用的函数。
195
+
196
+ <x-field-group>
197
+ <x-field data-name="onClose" data-type="function" data-required="true">
198
+ <x-field-desc markdown>当用户关闭 `DidConnect` 对话框时(例如,通过单击关闭按钮或在模态框外部单击)执行的回调函数。</x-field-desc>
199
+ </x-field>
200
+ <x-field data-name="onSuccess" data-type="function" data-required="true">
201
+ <x-field-desc markdown>连接成功时执行的回调函数。会话数据作为参数传递。</x-field-desc>
202
+ </x-field>
203
+ <x-field data-name="onError" data-type="function">
204
+ <x-field-desc markdown>在过程中发生错误时执行的回调函数。错误对象或消息作为参数传递。</x-field-desc>
205
+ </x-field>
206
+ <x-field data-name="onRecreateSession" data-type="function">
207
+ <x-field-desc markdown>当会话需要重置时(例如,在超时后或用户手动取消并重试时)调用的回调函数。</x-field-desc>
208
+ </x-field>
209
+ </x-field-group>
210
+
211
+ ## 后续步骤
212
+
213
+ 在对 `DidConnect` 组件有了深入了解之后,您现在可以构建稳健的身份验证体验。要对连接 UI 进行更多编程控制,请探索 [`useConnect` 钩子](./hooks-use-connect.md),它允许您从应用程序的任何位置打开和关闭 `DidConnect` 模态框。