@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.
- package/.aigne/doc-smith/config.yaml +83 -0
- package/.aigne/doc-smith/output/structure-plan.json +197 -0
- package/.aigne/doc-smith/upload-cache.yaml +168 -0
- package/docs/_sidebar.md +18 -0
- package/docs/advanced-authentication-methods.ja.md +261 -0
- package/docs/advanced-authentication-methods.md +261 -0
- package/docs/advanced-authentication-methods.zh-TW.md +261 -0
- package/docs/advanced-authentication-methods.zh.md +261 -0
- package/docs/advanced-utilities.ja.md +132 -0
- package/docs/advanced-utilities.md +132 -0
- package/docs/advanced-utilities.zh-TW.md +132 -0
- package/docs/advanced-utilities.zh.md +132 -0
- package/docs/advanced.ja.md +95 -0
- package/docs/advanced.md +95 -0
- package/docs/advanced.zh-TW.md +95 -0
- package/docs/advanced.zh.md +95 -0
- package/docs/api-reference.ja.md +178 -0
- package/docs/api-reference.md +178 -0
- package/docs/api-reference.zh-TW.md +178 -0
- package/docs/api-reference.zh.md +178 -0
- package/docs/core-components-did-connect.ja.md +214 -0
- package/docs/core-components-did-connect.md +213 -0
- package/docs/core-components-did-connect.zh-TW.md +214 -0
- package/docs/core-components-did-connect.zh.md +213 -0
- package/docs/core-components-session-provider.ja.md +239 -0
- package/docs/core-components-session-provider.md +239 -0
- package/docs/core-components-session-provider.zh-TW.md +239 -0
- package/docs/core-components-session-provider.zh.md +239 -0
- package/docs/core-components.ja.md +16 -0
- package/docs/core-components.md +16 -0
- package/docs/core-components.zh-TW.md +16 -0
- package/docs/core-components.zh.md +16 -0
- package/docs/getting-started.ja.md +138 -0
- package/docs/getting-started.md +138 -0
- package/docs/getting-started.zh-TW.md +138 -0
- package/docs/getting-started.zh.md +138 -0
- package/docs/hooks-use-connect.ja.md +214 -0
- package/docs/hooks-use-connect.md +214 -0
- package/docs/hooks-use-connect.zh-TW.md +214 -0
- package/docs/hooks-use-connect.zh.md +214 -0
- package/docs/hooks-use-did.ja.md +107 -0
- package/docs/hooks-use-did.md +107 -0
- package/docs/hooks-use-did.zh-TW.md +107 -0
- package/docs/hooks-use-did.zh.md +107 -0
- package/docs/hooks-use-oauth-passkey.ja.md +188 -0
- package/docs/hooks-use-oauth-passkey.md +188 -0
- package/docs/hooks-use-oauth-passkey.zh-TW.md +188 -0
- package/docs/hooks-use-oauth-passkey.zh.md +188 -0
- package/docs/hooks.ja.md +23 -0
- package/docs/hooks.md +23 -0
- package/docs/hooks.zh-TW.md +23 -0
- package/docs/hooks.zh.md +23 -0
- package/docs/overview.ja.md +159 -0
- package/docs/overview.md +159 -0
- package/docs/overview.zh-TW.md +159 -0
- package/docs/overview.zh.md +160 -0
- package/docs/ui-components-address.ja.md +121 -0
- package/docs/ui-components-address.md +121 -0
- package/docs/ui-components-address.zh-TW.md +121 -0
- package/docs/ui-components-address.zh.md +121 -0
- package/docs/ui-components-avatar.ja.md +65 -0
- package/docs/ui-components-avatar.md +65 -0
- package/docs/ui-components-avatar.zh-TW.md +65 -0
- package/docs/ui-components-avatar.zh.md +65 -0
- package/docs/ui-components-button.ja.md +99 -0
- package/docs/ui-components-button.md +99 -0
- package/docs/ui-components-button.zh-TW.md +99 -0
- package/docs/ui-components-button.zh.md +99 -0
- package/docs/ui-components-logo.ja.md +52 -0
- package/docs/ui-components-logo.md +52 -0
- package/docs/ui-components-logo.zh-TW.md +52 -0
- package/docs/ui-components-logo.zh.md +52 -0
- package/docs/ui-components.ja.md +57 -0
- package/docs/ui-components.md +57 -0
- package/docs/ui-components.zh-TW.md +57 -0
- package/docs/ui-components.zh.md +57 -0
- package/glossary.md +1 -0
- package/lib/package.json.js +1 -1
- package/package.json +5 -5
- package/src/Session/hooks/use-federated.js +3 -0
- package/src/Session/libs/federated.js +3 -0
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
# useConnect
|
|
2
|
+
|
|
3
|
+
`useConnect` hook 提供了一种强大的、编程化的方式来打开、关闭和管理 `DidConnect` UI 模态框。当您需要比声明式的 `Button` 组件提供更多对连接流程的控制时,例如从自定义 UI 元素、菜单项或响应应用程序事件来触发模态框,它就是理想的解决方案。
|
|
4
|
+
|
|
5
|
+
此 hook 是 `Button` 组件背后的引擎,让您可以直接访问相同的核心功能。
|
|
6
|
+
|
|
7
|
+
## 工作原理
|
|
8
|
+
|
|
9
|
+
`useConnect` hook 返回两个主要元素:
|
|
10
|
+
|
|
11
|
+
1. **`connectHolder`**:一个必须在组件树中渲染的 React 元素。此元素负责在 `DidConnect` 模态框被激活时进行渲染。在 `open` 函数被调用之前,它保持不可见。
|
|
12
|
+
2. **`connectApi`**:一个包含用于控制模态框生命周期的函数(`open`、`close`、`openPopup`、`loginOAuth`)的对象。
|
|
13
|
+
|
|
14
|
+
```d2
|
|
15
|
+
direction: down
|
|
16
|
+
|
|
17
|
+
User: {
|
|
18
|
+
shape: c4-person
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
Your-App: {
|
|
22
|
+
label: "你的应用程序"
|
|
23
|
+
shape: rectangle
|
|
24
|
+
|
|
25
|
+
Custom-Button: {
|
|
26
|
+
label: "自定义按钮"
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
useConnect-Hook: {
|
|
30
|
+
label: "useConnect()"
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
connectHolder: {
|
|
34
|
+
label: "connectHolder"
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
DID-Wallet: {
|
|
39
|
+
label: "DID Wallet"
|
|
40
|
+
icon: "https://www.arcblock.io/image-bin/uploads/37198ddc4a0b9e91e5c1c821ab895a34.svg"
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
User -> Your-App.Custom-Button: "1. 点击"
|
|
44
|
+
Your-App.Custom-Button -> Your-App.useConnect-Hook: "2. 调用 connectApi.open()"
|
|
45
|
+
Your-App.useConnect-Hook -> Your-App.connectHolder: "3. 激活模态框"
|
|
46
|
+
Your-App.connectHolder -> User: "4. 显示 DidConnect UI"
|
|
47
|
+
User -> DID-Wallet: "5. 扫描二维码并批准"
|
|
48
|
+
DID-Wallet -> Your-App.connectHolder: "6. 发送响应"
|
|
49
|
+
Your-App.connectHolder -> Your-App.useConnect-Hook: "7. 触发 onSuccess 回调"
|
|
50
|
+
Your-App.useConnect-Hook -> Your-App.connectHolder: "8. 停用模态框"
|
|
51
|
+
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## 设置
|
|
55
|
+
|
|
56
|
+
要使用此 hook,请在您的组件中导入并调用它。然后,确保在您的应用程序中的某个位置渲染 `connectHolder`,最好是在顶层组件中,以确保模态框可以覆盖所有其他内容。
|
|
57
|
+
|
|
58
|
+
```javascript Basic Setup icon=logos:javascript
|
|
59
|
+
import React from 'react';
|
|
60
|
+
import { useConnect } from '@arcblock/did-connect-react';
|
|
61
|
+
|
|
62
|
+
function MyComponent() {
|
|
63
|
+
// 1. 初始化 hook
|
|
64
|
+
const { connectHolder, connectApi } = useConnect();
|
|
65
|
+
|
|
66
|
+
const handleLogin = () => {
|
|
67
|
+
// 2. 使用 API 打开模态框
|
|
68
|
+
connectApi.open({
|
|
69
|
+
action: 'login',
|
|
70
|
+
onSuccess: (result) => {
|
|
71
|
+
console.log('登录成功!', result);
|
|
72
|
+
},
|
|
73
|
+
onClose: () => {
|
|
74
|
+
console.log('模态框被用户关闭。');
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
return (
|
|
80
|
+
<div>
|
|
81
|
+
<button type="button" onClick={handleLogin}>
|
|
82
|
+
Login with DID
|
|
83
|
+
</button>
|
|
84
|
+
|
|
85
|
+
{/* 3. 渲染 holder 组件 */}
|
|
86
|
+
{connectHolder}
|
|
87
|
+
</div>
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export default MyComponent;
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## API 参考
|
|
95
|
+
|
|
96
|
+
`connectApi` 对象提供了以下方法来控制连接过程。
|
|
97
|
+
|
|
98
|
+
### `connectApi.open(options)`
|
|
99
|
+
|
|
100
|
+
这是触发和显示 `DidConnect` 模态框的主要函数。它接受一个 `options` 对象,用于配置连接会话的行为、外观和回调。
|
|
101
|
+
|
|
102
|
+
**参数 (`options`)**
|
|
103
|
+
|
|
104
|
+
<x-field-group>
|
|
105
|
+
<x-field data-name="action" data-type="string" data-required="true">
|
|
106
|
+
<x-field-desc markdown>连接请求的目的,例如 `login`、`claim`、`sign` 等。这决定了钱包内的操作流程。</x-field-desc>
|
|
107
|
+
</x-field>
|
|
108
|
+
<x-field data-name="onSuccess" data-type="(result: object) => void" data-required="false">
|
|
109
|
+
<x-field-desc markdown>当用户在钱包中成功完成操作时执行的回调函数。`result` 对象包含来自钱包的响应。</x-field-desc>
|
|
110
|
+
</x-field>
|
|
111
|
+
<x-field data-name="onClose" data-type="() => void" data-required="false">
|
|
112
|
+
<x-field-desc markdown>当用户在未完成操作的情况下手动关闭模态框时执行的回调函数。</x-field-desc>
|
|
113
|
+
</x-field>
|
|
114
|
+
<x-field data-name="onError" data-type="(error: any) => void" data-required="false">
|
|
115
|
+
<x-field-desc markdown>如果在过程中发生错误(例如超时或网络问题)时执行的回调函数。</x-field-desc>
|
|
116
|
+
</x-field>
|
|
117
|
+
<x-field data-name="messages" data-type="ConnectMessages" data-required="false">
|
|
118
|
+
<x-field-desc markdown>一个用于自定义模态框中显示文本的对象。有关详细信息,请参阅 `ConnectMessages` 类型。</x-field-desc>
|
|
119
|
+
<x-field data-name="title" data-type="string" data-desc="模态框标题。"></x-field>
|
|
120
|
+
<x-field data-name="scan" data-type="string" data-desc="二维码附带的文本。"></x-field>
|
|
121
|
+
<x-field data-name="confirm" data-type="string" data-desc="确认步骤的文本。"></x-field>
|
|
122
|
+
<x-field data-name="success" data-type="ReactNode" data-desc="成功屏幕的内容。"></x-field>
|
|
123
|
+
</x-field>
|
|
124
|
+
<x-field data-name="popup" data-type="boolean" data-default="true">
|
|
125
|
+
<x-field-desc markdown>如果为 `true`,UI 将作为模态对话框显示。如果为 `false`,它将内联渲染,需要设置 `containerEl`。</x-field-desc>
|
|
126
|
+
</x-field>
|
|
127
|
+
<x-field data-name="containerEl" data-type="Element" data-required="false">
|
|
128
|
+
<x-field-desc markdown>当 `popup` 设置为 `false` 时,`DidConnect` 组件将在此 DOM 元素中渲染。</x-field-desc>
|
|
129
|
+
</x-field>
|
|
130
|
+
<x-field data-name="closeTimeout" data-type="number" data-default="2000">
|
|
131
|
+
<x-field-desc markdown>成功连接后,模态框自动关闭前的延迟时间(以毫秒为单位)。</x-field-desc>
|
|
132
|
+
</x-field>
|
|
133
|
+
<x-field data-name="checkInterval" data-type="number" data-default="2000">
|
|
134
|
+
<x-field-desc markdown>轮询服务器以检查会话状态的间隔时间(以毫秒为单位)。</x-field-desc>
|
|
135
|
+
</x-field>
|
|
136
|
+
<x-field data-name="checkTimeout" data-type="number" data-default="300000">
|
|
137
|
+
<x-field-desc markdown>连接尝试超时的总时间(以毫秒为单位)。</x-field-desc>
|
|
138
|
+
</x-field>
|
|
139
|
+
<x-field data-name="extraParams" data-type="object" data-default="{}">
|
|
140
|
+
<x-field-desc markdown>要包含在会话创建请求中的任何额外参数,这些参数可以在服务器端检索。</x-field-desc>
|
|
141
|
+
</x-field>
|
|
142
|
+
</x-field-group>
|
|
143
|
+
|
|
144
|
+
### `connectApi.close()`
|
|
145
|
+
|
|
146
|
+
手动关闭 `DidConnect` 模态框。模态框在成功或用户取消时会自动关闭,但如果需要因其他原因以编程方式关闭它,可以调用此函数。
|
|
147
|
+
|
|
148
|
+
```javascript icon=lucide:x-circle
|
|
149
|
+
// 示例:在成功回调中短暂延迟后关闭模态框
|
|
150
|
+
connectApi.open({
|
|
151
|
+
action: 'login',
|
|
152
|
+
onSuccess: () => {
|
|
153
|
+
showTemporarySuccessMessage();
|
|
154
|
+
setTimeout(() => {
|
|
155
|
+
connectApi.close();
|
|
156
|
+
}, 1500);
|
|
157
|
+
},
|
|
158
|
+
closeTimeout: 999999 // 防止自动关闭
|
|
159
|
+
});
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
### `connectApi.openPopup(params, options)`
|
|
163
|
+
|
|
164
|
+
此函数提供了一种替代的连接方法,它会在新的浏览器弹出窗口中打开 DID Connect 流程,而不是在当前页面内打开模态框。这对于某些类似 OAuth 的流程或在无法注入模态框的网站上进行集成时非常有用。
|
|
165
|
+
|
|
166
|
+
**参数**
|
|
167
|
+
|
|
168
|
+
<x-field-group>
|
|
169
|
+
<x-field data-name="params" data-type="ConnectProps" data-required="true">
|
|
170
|
+
<x-field-desc markdown>与 `open` 方法的选项类似。一个关键区别是,`params.extraParams.provider` 是**必需的**,用于标识认证方法(例如 'github'、'google')。</x-field-desc>
|
|
171
|
+
</x-field>
|
|
172
|
+
<x-field data-name="options" data-type="object" data-required="false">
|
|
173
|
+
<x-field-desc markdown>弹出窗口本身的配置。</x-field-desc>
|
|
174
|
+
<x-field data-name="baseUrl" data-type="string" data-desc="用于构建弹出窗口 URL 的基础 URL。"></x-field>
|
|
175
|
+
<x-field data-name="locale" data-type="string" data-default="en" data-desc="弹出窗口内容的区域设置。"></x-field>
|
|
176
|
+
<x-field data-name="popupOptions" data-type="object" data-desc="直接传递给 `window.open()` 的选项,用于自定义弹出窗口的大小和功能。"></x-field>
|
|
177
|
+
</x-field>
|
|
178
|
+
</x-field-group>
|
|
179
|
+
|
|
180
|
+
**用法**
|
|
181
|
+
|
|
182
|
+
`openPopup` 函数返回一个 `Promise`,它在成功时解析为认证数据,在失败或取消时拒绝。
|
|
183
|
+
|
|
184
|
+
```javascript icon=lucide:external-link
|
|
185
|
+
const handleGithubLogin = async () => {
|
|
186
|
+
try {
|
|
187
|
+
const result = await connectApi.openPopup({
|
|
188
|
+
action: 'login',
|
|
189
|
+
onSuccess: () => console.log('此回调也会被触发'),
|
|
190
|
+
extraParams: { provider: 'github' } // 这是必需的
|
|
191
|
+
});
|
|
192
|
+
console.log('通过弹出窗口登录成功:', result);
|
|
193
|
+
} catch (err) {
|
|
194
|
+
if (err.message === 'Popup closed') {
|
|
195
|
+
console.log('用户关闭了弹出窗口。');
|
|
196
|
+
} else {
|
|
197
|
+
console.error('发生错误:', err);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
};
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
### `connectApi.loginOAuth(options)`
|
|
204
|
+
|
|
205
|
+
这是一个辅助函数,用于直接启动 OAuth 登录流程。它是一个较低级别的实用工具,在库的其他部分内部使用。
|
|
206
|
+
|
|
207
|
+
**参数 (`options`)**
|
|
208
|
+
|
|
209
|
+
<x-field-group>
|
|
210
|
+
<x-field data-name="provider" data-type="string" data-required="true" data-desc="要使用的 OAuth 提供商,例如 'github'、'google'。"></x-field>
|
|
211
|
+
<x-field data-name="action" data-type="string" data-required="true" data-desc="操作,通常是 'login'。"></x-field>
|
|
212
|
+
<x-field data-name="extraParams" data-type="object" data-required="false" data-desc="OAuth 流程的附加参数。"></x-field>
|
|
213
|
+
<x-field data-name="onLogin" data-type="function" data-required="true" data-desc="成功登录后执行的回调函数。"></x-field>
|
|
214
|
+
</x-field-group>
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
# useDid
|
|
2
|
+
|
|
3
|
+
`useDid`フックは、`session.user`オブジェクトから特定のDID関連情報を簡単に抽出して操作するための便利なユーティリティです。手動での処理なしに、永続DID、ウォレット情報、接続済みアカウントなどの一般的に必要なデータを取得するためにユーザーオブジェクトの解析を簡素化します。
|
|
4
|
+
|
|
5
|
+
このフックは、セッションコンテキストにアクセスできるように、`SessionProvider`の子であるコンポーネント内で使用する必要があります。
|
|
6
|
+
|
|
7
|
+
## 使用方法
|
|
8
|
+
|
|
9
|
+
このフックを使用するには、`useSession`と一緒にインポートします。`useSession`を呼び出して現在のセッション状態を取得し、その`session`オブジェクトを`useDid`に渡します。
|
|
10
|
+
|
|
11
|
+
```javascript Basic Usage icon=logos:react
|
|
12
|
+
import { useSession } from '@arcblock/did-connect-react';
|
|
13
|
+
import { useDid } from '@arcblock/did-connect-react/lib/User';
|
|
14
|
+
|
|
15
|
+
function UserProfile() {
|
|
16
|
+
const { session } = useSession();
|
|
17
|
+
const {
|
|
18
|
+
did,
|
|
19
|
+
walletDid,
|
|
20
|
+
connectedDids,
|
|
21
|
+
sourceProvider,
|
|
22
|
+
connectedAccounts,
|
|
23
|
+
} = useDid({ session });
|
|
24
|
+
|
|
25
|
+
if (!session.user) {
|
|
26
|
+
return <p>DID情報を表示するにはログインしてください。</p>;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return (
|
|
30
|
+
<div>
|
|
31
|
+
<h2>ユーザーDID情報</h2>
|
|
32
|
+
<p><strong>永続DID:</strong> {did}</p>
|
|
33
|
+
<p><strong>ウォレットDID:</strong> {walletDid || 'N/A'}</p>
|
|
34
|
+
<p><strong>ログイン方法:</strong> {sourceProvider}</p>
|
|
35
|
+
<h3>接続済みアカウント ({connectedAccounts.length})</h3>
|
|
36
|
+
<ul>
|
|
37
|
+
{connectedAccounts.map((account) => (
|
|
38
|
+
<li key={account.did}>
|
|
39
|
+
{account.provider}: {account.did}
|
|
40
|
+
</li>
|
|
41
|
+
))}
|
|
42
|
+
</ul>
|
|
43
|
+
</div>
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## パラメータ
|
|
49
|
+
|
|
50
|
+
`useDid`フックは、単一のオブジェクトをパラメータとして受け取ります。
|
|
51
|
+
|
|
52
|
+
<x-field data-name="options" data-type="object" data-required="true">
|
|
53
|
+
<x-field data-name="session" data-type="object" data-required="true" data-desc="`useSession`フックから取得したセッションオブジェクト。このフックは`session.user`が存在する場合にのみデータを計算します。">
|
|
54
|
+
<x-field data-name="user" data-type="object" data-required="false" data-desc="ログイン成功後にセッションコンテキストによって提供されるユーザーオブジェクト。"></x-field>
|
|
55
|
+
</x-field>
|
|
56
|
+
</x-field>
|
|
57
|
+
|
|
58
|
+
## 戻り値
|
|
59
|
+
|
|
60
|
+
このフックは、派生したさまざまなDID情報を含む状態オブジェクトを返します。このオブジェクトは、`session.user`オブジェクトが変更されるたびに更新されます。
|
|
61
|
+
|
|
62
|
+
<x-field-group>
|
|
63
|
+
<x-field data-name="did" data-type="string" data-desc="ユーザーの永続DID。これは`permanentDid`のエイリアスです。"></x-field>
|
|
64
|
+
<x-field data-name="wallet" data-type="object" data-desc="プロバイダーが'wallet'である接続済みアカウントオブジェクト。ウォレットが接続されていない場合は`undefined`を返します。">
|
|
65
|
+
<x-field data-name="did" data-type="string" data-desc="ウォレットアカウントのDID。"></x-field>
|
|
66
|
+
<x-field data-name="provider" data-type="string" data-desc="プロバイダー名。'wallet'になります。"></x-field>
|
|
67
|
+
</x-field>
|
|
68
|
+
<x-field data-name="walletDid" data-type="string" data-desc="ユーザーの接続済みウォレットのDID。ウォレットが接続されていない場合は`undefined`を返します。"></x-field>
|
|
69
|
+
<x-field data-name="permanentDid" data-type="string" data-desc="ユーザーの永続DIDで、`user.did`からのプライマリ識別子です。"></x-field>
|
|
70
|
+
<x-field data-name="connectedDids" data-type="string[]" data-desc="ユーザーの接続済みアカウントのすべてのDIDを含む配列。"></x-field>
|
|
71
|
+
<x-field data-name="connectedAccounts" data-type="object[]" data-desc="接続済みアカウントオブジェクトの完全な配列。">
|
|
72
|
+
<x-field data-name="did" data-type="string" data-desc="接続済みアカウントのDID。"></x-field>
|
|
73
|
+
<x-field data-name="provider" data-type="string" data-desc="アカウントのプロバイダー(例:'wallet'、'github')。"></x-field>
|
|
74
|
+
</x-field>
|
|
75
|
+
<x-field data-name="sourceProvider" data-type="string" data-desc="ユーザーが最初にログインに使用したプロバイダー(例:'wallet'、'github')。デフォルトは'wallet'です。"></x-field>
|
|
76
|
+
<x-field data-name="sourceProviders" data-type="string[]" data-desc="ユーザーの接続済みアカウントのすべてのプロバイダー名の配列。"></x-field>
|
|
77
|
+
</x-field-group>
|
|
78
|
+
|
|
79
|
+
## スタンドアロンのヘルパー関数
|
|
80
|
+
|
|
81
|
+
`useDid`フックは、エクスポートされた一連のヘルパー関数に基づいて構築されています。フックのリアクティブな状態管理が不要で、単にユーザーオブジェクトから特定の情報を抽出したい場合は、これらの関数を直接インポートして使用できます。
|
|
82
|
+
|
|
83
|
+
| Function | Description |
|
|
84
|
+
| ------------------------ | ------------------------------------------------------------ |
|
|
85
|
+
| `getPermanentDid(user)` | ユーザーオブジェクトから永続DIDを抽出します。 |
|
|
86
|
+
| `getWalletDid(user)` | 接続済みウォレットのDIDを抽出します。 |
|
|
87
|
+
| `getConnectedAccounts(user)` | 接続済みアカウントの配列を取得します。 |
|
|
88
|
+
| `getConnectedDids(user)` | すべての接続済みアカウントからDIDの配列を取得します。 |
|
|
89
|
+
| `getSourceProvider(user)` | 初回ログインに使用されたプロバイダーを取得します。 |
|
|
90
|
+
| `getSourceProviders(user)` | 接続されているすべてのプロバイダー名の配列を取得します。 |
|
|
91
|
+
| `getWallet(user)` | 完全なウォレットアカウントオブジェクトを検索して返します。 |
|
|
92
|
+
|
|
93
|
+
### 例:ヘルパー関数の使用
|
|
94
|
+
|
|
95
|
+
```javascript Direct Function Usage icon=logos:javascript
|
|
96
|
+
import { getWalletDid } from '@arcblock/did-connect-react/lib/User/use-did';
|
|
97
|
+
import { useSession } from '@arcblock/did-connect-react';
|
|
98
|
+
|
|
99
|
+
// このアプローチは、イベントハンドラーやその他の非React関数内で役立ちます
|
|
100
|
+
function handleUserAction() {
|
|
101
|
+
const { session } = useSession();
|
|
102
|
+
if (session.user) {
|
|
103
|
+
const walletDid = getWalletDid(session.user);
|
|
104
|
+
console.log('ユーザーのウォレットDID:', walletDid);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
```
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
# useDid
|
|
2
|
+
|
|
3
|
+
The `useDid` hook is a convenient utility for easily extracting and working with specific DID-related information from the `session.user` object. It simplifies parsing the user object to get commonly needed data like the permanent DID, wallet information, and connected accounts without manual processing.
|
|
4
|
+
|
|
5
|
+
This hook should be used within a component that is a child of the `SessionProvider` to ensure it has access to the session context.
|
|
6
|
+
|
|
7
|
+
## How to Use
|
|
8
|
+
|
|
9
|
+
To use the hook, import it along with `useSession`. Call `useSession` to get the current session state, and then pass the `session` object to `useDid`.
|
|
10
|
+
|
|
11
|
+
```javascript Basic Usage icon=logos:react
|
|
12
|
+
import { useSession } from '@arcblock/did-connect-react';
|
|
13
|
+
import { useDid } from '@arcblock/did-connect-react/lib/User';
|
|
14
|
+
|
|
15
|
+
function UserProfile() {
|
|
16
|
+
const { session } = useSession();
|
|
17
|
+
const {
|
|
18
|
+
did,
|
|
19
|
+
walletDid,
|
|
20
|
+
connectedDids,
|
|
21
|
+
sourceProvider,
|
|
22
|
+
connectedAccounts,
|
|
23
|
+
} = useDid({ session });
|
|
24
|
+
|
|
25
|
+
if (!session.user) {
|
|
26
|
+
return <p>Please log in to view your DID information.</p>;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return (
|
|
30
|
+
<div>
|
|
31
|
+
<h2>User DID Information</h2>
|
|
32
|
+
<p><strong>Permanent DID:</strong> {did}</p>
|
|
33
|
+
<p><strong>Wallet DID:</strong> {walletDid || 'N/A'}</p>
|
|
34
|
+
<p><strong>Login Method:</strong> {sourceProvider}</p>
|
|
35
|
+
<h3>Connected Accounts ({connectedAccounts.length})</h3>
|
|
36
|
+
<ul>
|
|
37
|
+
{connectedAccounts.map((account) => (
|
|
38
|
+
<li key={account.did}>
|
|
39
|
+
{account.provider}: {account.did}
|
|
40
|
+
</li>
|
|
41
|
+
))}
|
|
42
|
+
</ul>
|
|
43
|
+
</div>
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Parameters
|
|
49
|
+
|
|
50
|
+
The `useDid` hook accepts a single object as its parameter.
|
|
51
|
+
|
|
52
|
+
<x-field data-name="options" data-type="object" data-required="true">
|
|
53
|
+
<x-field data-name="session" data-type="object" data-required="true" data-desc="The session object obtained from the `useSession` hook. The hook will only compute data when `session.user` is present.">
|
|
54
|
+
<x-field data-name="user" data-type="object" data-required="false" data-desc="The user object provided by the session context after a successful login."></x-field>
|
|
55
|
+
</x-field>
|
|
56
|
+
</x-field>
|
|
57
|
+
|
|
58
|
+
## Return Value
|
|
59
|
+
|
|
60
|
+
The hook returns a state object containing various pieces of derived DID information. This object will be updated whenever the `session.user` object changes.
|
|
61
|
+
|
|
62
|
+
<x-field-group>
|
|
63
|
+
<x-field data-name="did" data-type="string" data-desc="The user's permanent DID. This is an alias for `permanentDid`."></x-field>
|
|
64
|
+
<x-field data-name="wallet" data-type="object" data-desc="The connected account object where the provider is 'wallet'. Returns `undefined` if no wallet is connected.">
|
|
65
|
+
<x-field data-name="did" data-type="string" data-desc="The DID of the wallet account."></x-field>
|
|
66
|
+
<x-field data-name="provider" data-type="string" data-desc="The provider name, which will be 'wallet'."></x-field>
|
|
67
|
+
</x-field>
|
|
68
|
+
<x-field data-name="walletDid" data-type="string" data-desc="The DID of the user's connected wallet. Returns `undefined` if no wallet is connected."></x-field>
|
|
69
|
+
<x-field data-name="permanentDid" data-type="string" data-desc="The user's permanent DID, which is the primary identifier from `user.did`."></x-field>
|
|
70
|
+
<x-field data-name="connectedDids" data-type="string[]" data-desc="An array of all DIDs from the user's connected accounts."></x-field>
|
|
71
|
+
<x-field data-name="connectedAccounts" data-type="object[]" data-desc="The full array of connected account objects.">
|
|
72
|
+
<x-field data-name="did" data-type="string" data-desc="The DID of the connected account."></x-field>
|
|
73
|
+
<x-field data-name="provider" data-type="string" data-desc="The provider of the account (e.g., 'wallet', 'github')."></x-field>
|
|
74
|
+
</x-field>
|
|
75
|
+
<x-field data-name="sourceProvider" data-type="string" data-desc="The provider the user originally used to log in (e.g., 'wallet', 'github'). Defaults to 'wallet'."></x-field>
|
|
76
|
+
<x-field data-name="sourceProviders" data-type="string[]" data-desc="An array of all provider names from the user's connected accounts."></x-field>
|
|
77
|
+
</x-field-group>
|
|
78
|
+
|
|
79
|
+
## Standalone Helper Functions
|
|
80
|
+
|
|
81
|
+
The `useDid` hook is built on a set of exported helper functions. If you don't need the reactive state management of a hook and just want to extract a specific piece of information from a user object, you can import and use these functions directly.
|
|
82
|
+
|
|
83
|
+
| Function | Description |
|
|
84
|
+
| ------------------------ | ------------------------------------------------------------ |
|
|
85
|
+
| `getPermanentDid(user)` | Extracts the permanent DID from the user object. |
|
|
86
|
+
| `getWalletDid(user)` | Extracts the DID of the connected wallet. |
|
|
87
|
+
| `getConnectedAccounts(user)` | Retrieves the array of connected accounts. |
|
|
88
|
+
| `getConnectedDids(user)` | Retrieves an array of DIDs from all connected accounts. |
|
|
89
|
+
| `getSourceProvider(user)` | Gets the provider used for the initial login. |
|
|
90
|
+
| `getSourceProviders(user)` | Gets an array of all connected provider names. |
|
|
91
|
+
| `getWallet(user)` | Finds and returns the complete wallet account object. |
|
|
92
|
+
|
|
93
|
+
### Example: Using a Helper Function
|
|
94
|
+
|
|
95
|
+
```javascript Direct Function Usage icon=logos:javascript
|
|
96
|
+
import { getWalletDid } from '@arcblock/did-connect-react/lib/User/use-did';
|
|
97
|
+
import { useSession } from '@arcblock/did-connect-react';
|
|
98
|
+
|
|
99
|
+
// This approach can be useful inside event handlers or other non-React functions
|
|
100
|
+
function handleUserAction() {
|
|
101
|
+
const { session } = useSession();
|
|
102
|
+
if (session.user) {
|
|
103
|
+
const walletDid = getWalletDid(session.user);
|
|
104
|
+
console.log('User Wallet DID:', walletDid);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
```
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
# useDid
|
|
2
|
+
|
|
3
|
+
`useDid` hook 是一個方便的實用程式,可輕鬆地從 `session.user` 物件中提取和使用特定的 DID 相關資訊。它簡化了使用者物件的解析過程,無需手動處理即可獲取永久 DID、錢包資訊和已連接帳戶等常用資料。
|
|
4
|
+
|
|
5
|
+
此 hook 應在 `SessionProvider` 的子元件中使用,以確保它能存取 session 上下文。
|
|
6
|
+
|
|
7
|
+
## 如何使用
|
|
8
|
+
|
|
9
|
+
若要使用此 hook,請將其與 `useSession` 一起匯入。呼叫 `useSession` 以取得目前的 session 狀態,然後將 `session` 物件傳遞給 `useDid`。
|
|
10
|
+
|
|
11
|
+
```javascript Basic Usage icon=logos:react
|
|
12
|
+
import { useSession } from '@arcblock/did-connect-react';
|
|
13
|
+
import { useDid } from '@arcblock/did-connect-react/lib/User';
|
|
14
|
+
|
|
15
|
+
function UserProfile() {
|
|
16
|
+
const { session } = useSession();
|
|
17
|
+
const {
|
|
18
|
+
did,
|
|
19
|
+
walletDid,
|
|
20
|
+
connectedDids,
|
|
21
|
+
sourceProvider,
|
|
22
|
+
connectedAccounts,
|
|
23
|
+
} = useDid({ session });
|
|
24
|
+
|
|
25
|
+
if (!session.user) {
|
|
26
|
+
return <p>請登入以查看您的 DID 資訊。</p>;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return (
|
|
30
|
+
<div>
|
|
31
|
+
<h2>使用者 DID 資訊</h2>
|
|
32
|
+
<p><strong>永久 DID:</strong> {did}</p>
|
|
33
|
+
<p><strong>錢包 DID:</strong> {walletDid || 'N/A'}</p>
|
|
34
|
+
<p><strong>登入方式:</strong> {sourceProvider}</p>
|
|
35
|
+
<h3>已連接的帳戶 ({connectedAccounts.length})</h3>
|
|
36
|
+
<ul>
|
|
37
|
+
{connectedAccounts.map((account) => (
|
|
38
|
+
<li key={account.did}>
|
|
39
|
+
{account.provider}: {account.did}
|
|
40
|
+
</li>
|
|
41
|
+
))}
|
|
42
|
+
</ul>
|
|
43
|
+
</div>
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## 參數
|
|
49
|
+
|
|
50
|
+
`useDid` hook 接受一個物件作為其參數。
|
|
51
|
+
|
|
52
|
+
<x-field data-name="options" data-type="object" data-required="true">
|
|
53
|
+
<x-field data-name="session" data-type="object" data-required="true" data-desc="從 `useSession` hook 取得的 session 物件。只有當 `session.user` 存在時,此 hook 才會計算資料。">
|
|
54
|
+
<x-field data-name="user" data-type="object" data-required="false" data-desc="成功登入後由 session 上下文提供的使用者物件。"></x-field>
|
|
55
|
+
</x-field>
|
|
56
|
+
</x-field>
|
|
57
|
+
|
|
58
|
+
## 傳回值
|
|
59
|
+
|
|
60
|
+
此 hook 會傳回一個狀態物件,其中包含各種衍生的 DID 資訊。每當 `session.user` 物件變更時,此物件都會更新。
|
|
61
|
+
|
|
62
|
+
<x-field-group>
|
|
63
|
+
<x-field data-name="did" data-type="string" data-desc="使用者的永久 DID。這是 `permanentDid` 的別名。"></x-field>
|
|
64
|
+
<x-field data-name="wallet" data-type="object" data-desc="提供者為 'wallet' 的已連接帳戶物件。如果沒有連接錢包,則傳回 `undefined`。">
|
|
65
|
+
<x-field data-name="did" data-type="string" data-desc="錢包帳戶的 DID。"></x-field>
|
|
66
|
+
<x-field data-name="provider" data-type="string" data-desc="提供者名稱,此處為 'wallet'。"></x-field>
|
|
67
|
+
</x-field>
|
|
68
|
+
<x-field data-name="walletDid" data-type="string" data-desc="使用者已連接錢包的 DID。如果沒有連接錢包,則傳回 `undefined`。"></x-field>
|
|
69
|
+
<x-field data-name="permanentDid" data-type="string" data-desc="使用者的永久 DID,即來自 `user.did` 的主要識別碼。"></x-field>
|
|
70
|
+
<x-field data-name="connectedDids" data-type="string[]" data-desc="一個包含使用者所有已連接帳戶 DID 的陣列。"></x-field>
|
|
71
|
+
<x-field data-name="connectedAccounts" data-type="object[]" data-desc="完整的已連接帳戶物件陣列。">
|
|
72
|
+
<x-field data-name="did" data-type="string" data-desc="已連接帳戶的 DID。"></x-field>
|
|
73
|
+
<x-field data-name="provider" data-type="string" data-desc="帳戶的提供者(例如 'wallet'、'github')。"></x-field>
|
|
74
|
+
</x-field>
|
|
75
|
+
<x-field data-name="sourceProvider" data-type="string" data-desc="使用者最初用於登入的提供者(例如 'wallet'、'github')。預設為 'wallet'。"></x-field>
|
|
76
|
+
<x-field data-name="sourceProviders" data-type="string[]" data-desc="一個包含使用者所有已連接帳戶提供者名稱的陣列。"></x-field>
|
|
77
|
+
</x-field-group>
|
|
78
|
+
|
|
79
|
+
## 獨立的輔助函數
|
|
80
|
+
|
|
81
|
+
`useDid` hook 建立在一組匯出的輔助函數之上。如果您不需要 hook 的響應式狀態管理,而只想從使用者物件中提取特定資訊,可以直接匯入並使用這些函數。
|
|
82
|
+
|
|
83
|
+
| Function | Description |
|
|
84
|
+
| ------------------------ | ------------------------------------------------------------ |
|
|
85
|
+
| `getPermanentDid(user)` | 從使用者物件中提取永久 DID。 |
|
|
86
|
+
| `getWalletDid(user)` | 提取已連接錢包的 DID。 |
|
|
87
|
+
| `getConnectedAccounts(user)` | 擷取已連接帳戶的陣列。 |
|
|
88
|
+
| `getConnectedDids(user)` | 擷取一個包含所有已連接帳戶 DID 的陣列。 |
|
|
89
|
+
| `getSourceProvider(user)` | 取得用於初始登入的提供者。 |
|
|
90
|
+
| `getSourceProviders(user)` | 取得一個包含所有已連接提供者名稱的陣列。 |
|
|
91
|
+
| `getWallet(user)` | 尋找並傳回完整的錢包帳戶物件。 |
|
|
92
|
+
|
|
93
|
+
### 範例:使用輔助函數
|
|
94
|
+
|
|
95
|
+
```javascript Direct Function Usage icon=logos:javascript
|
|
96
|
+
import { getWalletDid } from '@arcblock/did-connect-react/lib/User/use-did';
|
|
97
|
+
import { useSession } from '@arcblock/did-connect-react';
|
|
98
|
+
|
|
99
|
+
// 這種方法在事件處理常式或其他非 React 函數中可能很有用
|
|
100
|
+
function handleUserAction() {
|
|
101
|
+
const { session } = useSession();
|
|
102
|
+
if (session.user) {
|
|
103
|
+
const walletDid = getWalletDid(session.user);
|
|
104
|
+
console.log('使用者錢包 DID:', walletDid);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
```
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
# useDid
|
|
2
|
+
|
|
3
|
+
`useDid` Hook 是一个便捷的实用程序,可以轻松地从 `session.user` 对象中提取和使用特定的 DID 相关信息。它简化了用户对象的解析过程,无需手动处理即可获取永久 DID、钱包信息和已连接账户等常用数据。
|
|
4
|
+
|
|
5
|
+
此 Hook 应在 `SessionProvider` 的子组件中使用,以确保其能够访问会话上下文。
|
|
6
|
+
|
|
7
|
+
## 如何使用
|
|
8
|
+
|
|
9
|
+
要使用此 Hook,请将其与 `useSession` 一起导入。调用 `useSession` 获取当前会话状态,然后将 `session` 对象传递给 `useDid`。
|
|
10
|
+
|
|
11
|
+
```javascript Basic Usage icon=logos:react
|
|
12
|
+
import { useSession } from '@arcblock/did-connect-react';
|
|
13
|
+
import { useDid } from '@arcblock/did-connect-react/lib/User';
|
|
14
|
+
|
|
15
|
+
function UserProfile() {
|
|
16
|
+
const { session } = useSession();
|
|
17
|
+
const {
|
|
18
|
+
did,
|
|
19
|
+
walletDid,
|
|
20
|
+
connectedDids,
|
|
21
|
+
sourceProvider,
|
|
22
|
+
connectedAccounts,
|
|
23
|
+
} = useDid({ session });
|
|
24
|
+
|
|
25
|
+
if (!session.user) {
|
|
26
|
+
return <p>请登录以查看您的 DID 信息。</p>;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return (
|
|
30
|
+
<div>
|
|
31
|
+
<h2>用户 DID 信息</h2>
|
|
32
|
+
<p><strong>永久 DID:</strong> {did}</p>
|
|
33
|
+
<p><strong>钱包 DID:</strong> {walletDid || 'N/A'}</p>
|
|
34
|
+
<p><strong>登录方法:</strong> {sourceProvider}</p>
|
|
35
|
+
<h3>已连接账户 ({connectedAccounts.length})</h3>
|
|
36
|
+
<ul>
|
|
37
|
+
{connectedAccounts.map((account) => (
|
|
38
|
+
<li key={account.did}>
|
|
39
|
+
{account.provider}: {account.did}
|
|
40
|
+
</li>
|
|
41
|
+
))}
|
|
42
|
+
</ul>
|
|
43
|
+
</div>
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## 参数
|
|
49
|
+
|
|
50
|
+
`useDid` Hook 接受一个对象作为其唯一参数。
|
|
51
|
+
|
|
52
|
+
<x-field data-name="options" data-type="object" data-required="true">
|
|
53
|
+
<x-field data-name="session" data-type="object" data-required="true" data-desc="从 `useSession` Hook 获取的会话对象。只有当 `session.user` 存在时,该 Hook 才会计算数据。">
|
|
54
|
+
<x-field data-name="user" data-type="object" data-required="false" data-desc="成功登录后由会话上下文提供的用户对象。"></x-field>
|
|
55
|
+
</x-field>
|
|
56
|
+
</x-field>
|
|
57
|
+
|
|
58
|
+
## 返回值
|
|
59
|
+
|
|
60
|
+
该 Hook 返回一个状态对象,其中包含各种派生的 DID 信息。每当 `session.user` 对象发生变化时,此对象都会更新。
|
|
61
|
+
|
|
62
|
+
<x-field-group>
|
|
63
|
+
<x-field data-name="did" data-type="string" data-desc="用户的永久 DID。这是 `permanentDid` 的别名。"></x-field>
|
|
64
|
+
<x-field data-name="wallet" data-type="object" data-desc="提供者为 'wallet' 的已连接账户对象。如果没有连接钱包,则返回 `undefined`。">
|
|
65
|
+
<x-field data-name="did" data-type="string" data-desc="钱包账户的 DID。"></x-field>
|
|
66
|
+
<x-field data-name="provider" data-type="string" data-desc="提供者名称,即 'wallet'。"></x-field>
|
|
67
|
+
</x-field>
|
|
68
|
+
<x-field data-name="walletDid" data-type="string" data-desc="用户已连接钱包的 DID。如果没有连接钱包,则返回 `undefined`。"></x-field>
|
|
69
|
+
<x-field data-name="permanentDid" data-type="string" data-desc="用户的永久 DID,即来自 `user.did` 的主标识符。"></x-field>
|
|
70
|
+
<x-field data-name="connectedDids" data-type="string[]" data-desc="一个包含用户所有已连接账户 DID 的数组。"></x-field>
|
|
71
|
+
<x-field data-name="connectedAccounts" data-type="object[]" data-desc="包含所有已连接账户对象的完整数组。">
|
|
72
|
+
<x-field data-name="did" data-type="string" data-desc="已连接账户的 DID。"></x-field>
|
|
73
|
+
<x-field data-name="provider" data-type="string" data-desc="账户的提供者(例如 'wallet'、'github')。"></x-field>
|
|
74
|
+
</x-field>
|
|
75
|
+
<x-field data-name="sourceProvider" data-type="string" data-desc="用户最初用于登录的提供者(例如 'wallet'、'github')。默认为 'wallet'。"></x-field>
|
|
76
|
+
<x-field data-name="sourceProviders" data-type="string[]" data-desc="一个包含用户所有已连接账户提供者名称的数组。"></x-field>
|
|
77
|
+
</x-field-group>
|
|
78
|
+
|
|
79
|
+
## 独立的辅助函数
|
|
80
|
+
|
|
81
|
+
`useDid` Hook 构建在一组导出的辅助函数之上。如果您不需要 Hook 的响应式状态管理,而只想从用户对象中提取特定信息,则可以直接导入并使用这些函数。
|
|
82
|
+
|
|
83
|
+
| Function | Description |
|
|
84
|
+
| ------------------------ | ------------------------------------------------------------ |
|
|
85
|
+
| `getPermanentDid(user)` | 从用户对象中提取永久 DID。 |
|
|
86
|
+
| `getWalletDid(user)` | 提取已连接钱包的 DID。 |
|
|
87
|
+
| `getConnectedAccounts(user)` | 检索已连接账户的数组。 |
|
|
88
|
+
| `getConnectedDids(user)` | 从所有已连接账户中检索一个 DID 数组。 |
|
|
89
|
+
| `getSourceProvider(user)` | 获取用于初始登录的提供者。 |
|
|
90
|
+
| `getSourceProviders(user)` | 获取所有已连接提供者名称的数组。 |
|
|
91
|
+
| `getWallet(user)` | 查找并返回完整的钱包账户对象。 |
|
|
92
|
+
|
|
93
|
+
### 示例:使用辅助函数
|
|
94
|
+
|
|
95
|
+
```javascript Direct Function Usage icon=logos:javascript
|
|
96
|
+
import { getWalletDid } from '@arcblock/did-connect-react/lib/User/use-did';
|
|
97
|
+
import { useSession } from '@arcblock/did-connect-react';
|
|
98
|
+
|
|
99
|
+
// 这种方法在事件处理程序或其他非 React 函数中可能很有用
|
|
100
|
+
function handleUserAction() {
|
|
101
|
+
const { session } = useSession();
|
|
102
|
+
if (session.user) {
|
|
103
|
+
const walletDid = getWalletDid(session.user);
|
|
104
|
+
console.log('User Wallet DID:', walletDid);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
```
|