@arcblock/did-connect-react 3.4.14 → 3.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/standalone/did-connect-react.css +1 -0
- package/dist/standalone/index.js +133700 -0
- package/lib/Connect/components/login-item/connect-choose-list.js +111 -111
- package/lib/Connect/components/login-item/connect-provider-list.js +180 -180
- package/lib/Connect/components/login-item/mobile-login-item.js +56 -56
- package/lib/Connect/components/login-item/passkey-login-item.js +27 -29
- package/lib/Connect/components/login-item/web-login-item.js +31 -29
- package/lib/Connect/connect.js +202 -197
- package/lib/Connect/contexts/state.js +19 -17
- package/lib/Connect/hooks/provider-list.js +33 -33
- package/lib/Connect/plugins/email/list-item.js +14 -14
- package/lib/Connect/plugins/email/placeholder.js +77 -76
- package/lib/package.json.js +1 -1
- package/package.json +14 -9
- package/.aigne/doc-smith/config.yaml +0 -85
- package/.aigne/doc-smith/history.yaml +0 -6
- package/.aigne/doc-smith/output/structure-plan.json +0 -204
- package/.aigne/doc-smith/translation-cache.yaml +0 -11
- package/.aigne/doc-smith/upload-cache.yaml +0 -213
- package/docs/_sidebar.md +0 -18
- package/docs/advanced-authentication-methods.ja.md +0 -261
- package/docs/advanced-authentication-methods.md +0 -261
- package/docs/advanced-authentication-methods.zh-TW.md +0 -261
- package/docs/advanced-authentication-methods.zh.md +0 -261
- package/docs/advanced-utilities.ja.md +0 -132
- package/docs/advanced-utilities.md +0 -132
- package/docs/advanced-utilities.zh-TW.md +0 -132
- package/docs/advanced-utilities.zh.md +0 -132
- package/docs/advanced.ja.md +0 -95
- package/docs/advanced.md +0 -95
- package/docs/advanced.zh-TW.md +0 -95
- package/docs/advanced.zh.md +0 -95
- package/docs/api-reference.ja.md +0 -178
- package/docs/api-reference.md +0 -178
- package/docs/api-reference.zh-TW.md +0 -178
- package/docs/api-reference.zh.md +0 -178
- package/docs/assets/diagram/core-components-session-provider-01.ja.jpg +0 -0
- package/docs/assets/diagram/core-components-session-provider-01.jpg +0 -0
- package/docs/assets/diagram/core-components-session-provider-01.zh-TW.jpg +0 -0
- package/docs/assets/diagram/core-components-session-provider-01.zh.jpg +0 -0
- package/docs/assets/diagram/did-connect-diagram-0.ja.jpg +0 -0
- package/docs/assets/diagram/did-connect-diagram-0.jpg +0 -0
- package/docs/assets/diagram/did-connect-diagram-0.zh-TW.jpg +0 -0
- package/docs/assets/diagram/did-connect-diagram-0.zh.jpg +0 -0
- package/docs/assets/diagram/overview-01.ja.jpg +0 -0
- package/docs/assets/diagram/overview-01.jpg +0 -0
- package/docs/assets/diagram/overview-01.zh-TW.jpg +0 -0
- package/docs/assets/diagram/overview-01.zh.jpg +0 -0
- package/docs/assets/diagram/use-connect-diagram-0.ja.jpg +0 -0
- package/docs/assets/diagram/use-connect-diagram-0.jpg +0 -0
- package/docs/assets/diagram/use-connect-diagram-0.zh-TW.jpg +0 -0
- package/docs/assets/diagram/use-connect-diagram-0.zh.jpg +0 -0
- package/docs/core-components-did-connect.ja.md +0 -166
- package/docs/core-components-did-connect.md +0 -166
- package/docs/core-components-did-connect.zh-TW.md +0 -166
- package/docs/core-components-did-connect.zh.md +0 -166
- package/docs/core-components-session-provider.ja.md +0 -197
- package/docs/core-components-session-provider.md +0 -197
- package/docs/core-components-session-provider.zh-TW.md +0 -197
- package/docs/core-components-session-provider.zh.md +0 -197
- package/docs/core-components.ja.md +0 -16
- package/docs/core-components.md +0 -16
- package/docs/core-components.zh-TW.md +0 -16
- package/docs/core-components.zh.md +0 -16
- package/docs/getting-started.ja.md +0 -138
- package/docs/getting-started.md +0 -138
- package/docs/getting-started.zh-TW.md +0 -138
- package/docs/getting-started.zh.md +0 -138
- package/docs/hooks-use-connect.ja.md +0 -178
- package/docs/hooks-use-connect.md +0 -178
- package/docs/hooks-use-connect.zh-TW.md +0 -178
- package/docs/hooks-use-connect.zh.md +0 -178
- package/docs/hooks-use-did.ja.md +0 -107
- package/docs/hooks-use-did.md +0 -107
- package/docs/hooks-use-did.zh-TW.md +0 -107
- package/docs/hooks-use-did.zh.md +0 -107
- package/docs/hooks-use-oauth-passkey.ja.md +0 -188
- package/docs/hooks-use-oauth-passkey.md +0 -188
- package/docs/hooks-use-oauth-passkey.zh-TW.md +0 -188
- package/docs/hooks-use-oauth-passkey.zh.md +0 -188
- package/docs/hooks.ja.md +0 -23
- package/docs/hooks.md +0 -23
- package/docs/hooks.zh-TW.md +0 -23
- package/docs/hooks.zh.md +0 -23
- package/docs/overview.ja.md +0 -119
- package/docs/overview.md +0 -119
- package/docs/overview.zh-TW.md +0 -119
- package/docs/overview.zh.md +0 -119
- package/docs/ui-components-address.ja.md +0 -121
- package/docs/ui-components-address.md +0 -121
- package/docs/ui-components-address.zh-TW.md +0 -121
- package/docs/ui-components-address.zh.md +0 -121
- package/docs/ui-components-avatar.ja.md +0 -65
- package/docs/ui-components-avatar.md +0 -65
- package/docs/ui-components-avatar.zh-TW.md +0 -65
- package/docs/ui-components-avatar.zh.md +0 -65
- package/docs/ui-components-button.ja.md +0 -99
- package/docs/ui-components-button.md +0 -99
- package/docs/ui-components-button.zh-TW.md +0 -99
- package/docs/ui-components-button.zh.md +0 -99
- package/docs/ui-components-logo.ja.md +0 -52
- package/docs/ui-components-logo.md +0 -52
- package/docs/ui-components-logo.zh-TW.md +0 -52
- package/docs/ui-components-logo.zh.md +0 -52
- package/docs/ui-components.ja.md +0 -57
- package/docs/ui-components.md +0 -57
- package/docs/ui-components.zh-TW.md +0 -57
- package/docs/ui-components.zh.md +0 -57
- package/glossary.md +0 -1
- package/src/Address/index.jsx +0 -2
- package/src/Avatar/index.jsx +0 -2
- package/src/Button/Button.stories.jsx +0 -7
- package/src/Button/index.jsx +0 -21
- package/src/Connect/Connect.stories.jsx +0 -34
- package/src/Connect/assets/locale.js +0 -149
- package/src/Connect/assets/login-bg.png +0 -0
- package/src/Connect/assets/login-slogan.js +0 -7
- package/src/Connect/components/action-button.jsx +0 -22
- package/src/Connect/components/app-tips.jsx +0 -156
- package/src/Connect/components/auto-height.jsx +0 -38
- package/src/Connect/components/back-button.jsx +0 -24
- package/src/Connect/components/connect-status.jsx +0 -259
- package/src/Connect/components/did-connect-title.jsx +0 -107
- package/src/Connect/components/download-tips.jsx +0 -55
- package/src/Connect/components/loading.jsx +0 -25
- package/src/Connect/components/login-item/connect-choose-list.jsx +0 -317
- package/src/Connect/components/login-item/connect-provider-list.jsx +0 -462
- package/src/Connect/components/login-item/login-method-item.jsx +0 -139
- package/src/Connect/components/login-item/mobile-login-item.jsx +0 -181
- package/src/Connect/components/login-item/passkey-login-item.jsx +0 -54
- package/src/Connect/components/login-item/wallet-login-options.jsx +0 -129
- package/src/Connect/components/login-item/web-login-item.jsx +0 -157
- package/src/Connect/components/mask-overlay.jsx +0 -32
- package/src/Connect/components/refresh-overlay.jsx +0 -52
- package/src/Connect/components/switch-app.jsx +0 -69
- package/src/Connect/connect.jsx +0 -617
- package/src/Connect/contexts/state.jsx +0 -234
- package/src/Connect/fallback-connect.jsx +0 -47
- package/src/Connect/fullpage.jsx +0 -3
- package/src/Connect/hooks/auth-url.js +0 -31
- package/src/Connect/hooks/method-list.js +0 -121
- package/src/Connect/hooks/page-show.js +0 -24
- package/src/Connect/hooks/provider-list.js +0 -165
- package/src/Connect/hooks/security.js +0 -40
- package/src/Connect/hooks/token.js +0 -627
- package/src/Connect/hooks/use-apps.js +0 -69
- package/src/Connect/hooks/use-quick-connect.js +0 -119
- package/src/Connect/index.jsx +0 -21
- package/src/Connect/landing-page.jsx +0 -3
- package/src/Connect/plugins/email/index.jsx +0 -85
- package/src/Connect/plugins/email/list-item.jsx +0 -34
- package/src/Connect/plugins/email/placeholder.jsx +0 -365
- package/src/Connect/plugins/index.js +0 -2
- package/src/Connect/use-connect.jsx +0 -321
- package/src/Connect/with-blocklet.jsx +0 -26
- package/src/Connect/with-bridge-call.jsx +0 -138
- package/src/Federated/context.jsx +0 -93
- package/src/Federated/index.jsx +0 -1
- package/src/Logo/index.jsx +0 -2
- package/src/OAuth/bind-conflict-alert.jsx +0 -37
- package/src/OAuth/context.jsx +0 -407
- package/src/OAuth/guest.svg +0 -20
- package/src/OAuth/index.jsx +0 -1
- package/src/OAuth/passport-switcher.jsx +0 -2
- package/src/Passkey/actions.jsx +0 -217
- package/src/Passkey/constants.js +0 -2
- package/src/Passkey/context.jsx +0 -395
- package/src/Passkey/dialog.jsx +0 -401
- package/src/Passkey/icon.jsx +0 -10
- package/src/Passkey/index.jsx +0 -2
- package/src/Service/index.jsx +0 -96
- package/src/Session/assets/did-spaces-guide-cover.svg +0 -1
- package/src/Session/assets/did-spaces-guide-icon.svg +0 -7
- package/src/Session/context.jsx +0 -7
- package/src/Session/did-spaces-guide.jsx +0 -173
- package/src/Session/handler.jsx +0 -98
- package/src/Session/hooks/use-federated.js +0 -91
- package/src/Session/hooks/use-mobile.jsx +0 -6
- package/src/Session/hooks/use-protected-routes.js +0 -16
- package/src/Session/hooks/use-session-token.js +0 -400
- package/src/Session/hooks/use-verify.jsx +0 -76
- package/src/Session/index.jsx +0 -1789
- package/src/Session/libs/constants.js +0 -17
- package/src/Session/libs/did-spaces.js +0 -38
- package/src/Session/libs/federated.js +0 -82
- package/src/Session/libs/index.js +0 -5
- package/src/Session/libs/locales.js +0 -160
- package/src/Session/libs/login-mobile.js +0 -80
- package/src/Session/window-focus-aware.jsx +0 -28
- package/src/SessionManager/index.jsx +0 -2
- package/src/Storage/engine/cookie.js +0 -25
- package/src/Storage/engine/local-storage.js +0 -57
- package/src/Storage/index.js +0 -25
- package/src/User/index.js +0 -4
- package/src/User/use-did.js +0 -80
- package/src/User/wrap-did.jsx +0 -18
- package/src/WebWalletSWKeeper/index.jsx +0 -3
- package/src/components/PassportSwitcher.jsx +0 -160
- package/src/constant.js +0 -27
- package/src/error.js +0 -6
- package/src/hooks/use-locale.jsx +0 -6
- package/src/index.js +0 -32
- package/src/locales/en.jsx +0 -15
- package/src/locales/index.jsx +0 -13
- package/src/locales/zh.jsx +0 -15
- package/src/types.d.ts +0 -355
- package/src/utils.js +0 -413
- package/vite.config.mjs +0 -29
|
@@ -1,138 +0,0 @@
|
|
|
1
|
-
# 入門指南
|
|
2
|
-
|
|
3
|
-
本指南將引導您完成使用 `@arcblock/did-connect-react` 將去中心化身分(DID)驗證整合到您的 React 應用程式中的基本步驟。只需幾分鐘,您就可以擁有一個功能齊全的登入和登出流程。
|
|
4
|
-
|
|
5
|
-
### 前提條件
|
|
6
|
-
|
|
7
|
-
在開始之前,請確保您的開發環境符合以下標準:
|
|
8
|
-
|
|
9
|
-
- 您已設定好一個 React 專案。
|
|
10
|
-
- 您有一個正在執行的 Blocklet Server 實例,它將處理驗證請求。該函式庫旨在與由 Blocklet Server 驅動的後端無縫協作。
|
|
11
|
-
|
|
12
|
-
## 步驟 1:安裝函式庫
|
|
13
|
-
|
|
14
|
-
首先,將 `@arcblock/did-connect-react` 套件新增到您的專案中。您可以使用 npm 或 yarn。
|
|
15
|
-
|
|
16
|
-
```bash Install with npm icon=logos:npm
|
|
17
|
-
npm install @arcblock/did-connect-react
|
|
18
|
-
```
|
|
19
|
-
|
|
20
|
-
```bash Install with yarn icon=logos:yarn
|
|
21
|
-
yarn add @arcblock/did-connect-react
|
|
22
|
-
```
|
|
23
|
-
|
|
24
|
-
## 步驟 2:設定 SessionProvider
|
|
25
|
-
|
|
26
|
-
`SessionProvider` 是該函式庫的核心。它是一個 context provider,負責管理使用者的會話狀態(如登入狀態和使用者資訊),並將其提供給所有被它包裹的元件。您應該將它放在應用程式的根部,例如,在您的 `App.js` 檔案中。
|
|
27
|
-
|
|
28
|
-
對於基於 Blocklet 建構的應用程式,建議使用 `createAuthServiceSessionContext` 工廠函數來初始化 provider。
|
|
29
|
-
|
|
30
|
-
```javascript App.js icon=logos:react
|
|
31
|
-
import React from 'react';
|
|
32
|
-
import { createAuthServiceSessionContext } from '@arcblock/did-connect-react/lib/Session';
|
|
33
|
-
import Home from './Home'; // 我們將在下一步建立此元件
|
|
34
|
-
|
|
35
|
-
// createAuthServiceSessionContext 專為 Blocklet 環境進行了最佳化
|
|
36
|
-
const { SessionProvider } = createAuthServiceSessionContext();
|
|
37
|
-
|
|
38
|
-
function App() {
|
|
39
|
-
return (
|
|
40
|
-
// serviceHost 屬性指向您後端驗證服務的基礎路徑。
|
|
41
|
-
// 對於標準的 Blocklet,這通常是 blocklet 的前綴或 '/'。
|
|
42
|
-
<SessionProvider serviceHost="/">
|
|
43
|
-
<Home />
|
|
44
|
-
</SessionProvider>
|
|
45
|
-
);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export default App;
|
|
49
|
-
```
|
|
50
|
-
|
|
51
|
-
## 步驟 3:實作登入和登出
|
|
52
|
-
|
|
53
|
-
現在,讓我們建立一個允許使用者登入和登出的元件。我們可以使用 React 的 `useContext` hook 搭配 `SessionContext` 來存取會話資料和函數。
|
|
54
|
-
|
|
55
|
-
`session` 物件為您提供了所需的一切:目前的 `user`、`loading` 狀態,以及像 `login()` 和 `logout()` 這樣的函數。
|
|
56
|
-
|
|
57
|
-
```javascript Home.js icon=logos:react
|
|
58
|
-
import React, { useContext } from 'react';
|
|
59
|
-
import { SessionContext } from '@arcblock/did-connect-react/lib/Session';
|
|
60
|
-
import ConnectButton from '@arcblock/did-connect-react/lib/Button';
|
|
61
|
-
import Button from '@arcblock/ux/lib/Button'; // 一個通用按鈕
|
|
62
|
-
import CircularProgress from '@mui/material/CircularProgress';
|
|
63
|
-
|
|
64
|
-
function Home() {
|
|
65
|
-
// 存取會話 context
|
|
66
|
-
const { session } = useContext(SessionContext);
|
|
67
|
-
|
|
68
|
-
// 解構以便於存取
|
|
69
|
-
const { user, loading, login, logout } = session;
|
|
70
|
-
|
|
71
|
-
// 在會話初始化期間顯示載入指示器
|
|
72
|
-
if (loading) {
|
|
73
|
-
return <CircularProgress />;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
return (
|
|
77
|
-
<div style={{ padding: '20px', textAlign: 'center' }}>
|
|
78
|
-
{user ? (
|
|
79
|
-
// 如果使用者已登入,顯示他們的 DID 和一個登出按鈕
|
|
80
|
-
<div>
|
|
81
|
-
<h1>歡迎!</h1>
|
|
82
|
-
<p>您的 DID: {user.did}</p>
|
|
83
|
-
<Button variant="contained" color="secondary" onClick={logout}>
|
|
84
|
-
登出
|
|
85
|
-
</Button>
|
|
86
|
-
</div>
|
|
87
|
-
) : (
|
|
88
|
-
// 如果未登入,顯示登入按鈕
|
|
89
|
-
<div>
|
|
90
|
-
<h1>請登入</h1>
|
|
91
|
-
<ConnectButton onClick={() => login()}>
|
|
92
|
-
連接錢包
|
|
93
|
-
</ConnectButton>
|
|
94
|
-
</div>
|
|
95
|
-
)}
|
|
96
|
-
</div>
|
|
97
|
-
);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
export default Home;
|
|
101
|
-
```
|
|
102
|
-
|
|
103
|
-
當使用者點擊 `ConnectButton` 時,會呼叫 `login()` 函數。此函數負責打開 DID Connect 彈出視窗,該視窗會顯示一個 QR code 供使用者使用他們的 DID Wallet 掃描,並管理整個驗證過程。
|
|
104
|
-
|
|
105
|
-
## 步驟 4:存取使用者資料
|
|
106
|
-
|
|
107
|
-
一旦使用者成功登入,`session.user` 物件將會被填入資料。此物件包含從使用者的 DID Wallet 中檢索到的個人資料資訊。
|
|
108
|
-
|
|
109
|
-
由 context 提供的 `session` 物件是您與使用者驗證狀態互動的主要介面。以下是其關鍵屬性:
|
|
110
|
-
|
|
111
|
-
<x-field-group>
|
|
112
|
-
<x-field data-name="user" data-type="object | null" data-desc="已驗證的使用者物件。如果沒有使用者登入,則為 `null`。">
|
|
113
|
-
<x-field data-name="did" data-type="string" data-desc="使用者的去中心化身分識別碼(DID)。"></x-field>
|
|
114
|
-
<x-field data-name="fullName" data-type="string" data-desc="來自使用者錢包個人資料的全名。"></x-field>
|
|
115
|
-
<x-field data-name="email" data-type="string" data-desc="來自使用者錢包個人資料的電子郵件。"></x-field>
|
|
116
|
-
</x-field>
|
|
117
|
-
<x-field data-name="loading" data-type="boolean" data-desc="在會話初始化或刷新期間為 `true`,否則為 `false`。"></x-field>
|
|
118
|
-
<x-field data-name="login" data-type="function" data-desc="一個啟動登入過程的函數,它會打開 DID Connect 使用者介面。"></x-field>
|
|
119
|
-
<x-field data-name="logout" data-type="function" data-desc="一個將使用者登出並清除會話的函數。"></x-field>
|
|
120
|
-
<x-field data-name="initialized" data-type="boolean" data-desc="初始會話檢查完成後為 `true`。"></x-field>
|
|
121
|
-
</x-field-group>
|
|
122
|
-
|
|
123
|
-
## 你已完成所有設定!
|
|
124
|
-
|
|
125
|
-
恭喜!您已成功在您的 React 應用程式中實作了一個完整的基於 DID 的驗證流程。使用者現在可以使用他們的 DID Wallet 登入,您的應用程式也可以安全地存取他們的會話資訊。
|
|
126
|
-
|
|
127
|
-
### 後續步驟
|
|
128
|
-
|
|
129
|
-
要深入了解並探索更多進階功能,請查看以下部分:
|
|
130
|
-
|
|
131
|
-
<x-cards>
|
|
132
|
-
<x-card data-title="會話管理 (SessionProvider)" data-icon="lucide:key-round" data-href="/core-components/session-provider">
|
|
133
|
-
了解 SessionProvider 的所有設定選項,以自訂會話行為、逾時等。
|
|
134
|
-
</x-card>
|
|
135
|
-
<x-card data-title="連接 UI (DidConnect)" data-icon="lucide:qr-code" data-href="/core-components/did-connect">
|
|
136
|
-
探索 DidConnect 元件,以一種宣告式的方式將連接 UI 直接嵌入到您的頁面中。
|
|
137
|
-
</x-card>
|
|
138
|
-
</x-cards>
|
|
@@ -1,138 +0,0 @@
|
|
|
1
|
-
# 快速入门
|
|
2
|
-
|
|
3
|
-
本指南将引导你完成使用 `@arcblock/did-connect-react` 将去中心化身份(DID)验证集成到你的 React 应用中的基本步骤。只需几分钟,你就能拥有一个功能齐全的登录和登出流程。
|
|
4
|
-
|
|
5
|
-
### 前提条件
|
|
6
|
-
|
|
7
|
-
在开始之前,请确保你的开发环境满足以下条件:
|
|
8
|
-
|
|
9
|
-
- 你已建立一个 React 项目。
|
|
10
|
-
- 你有一个正在运行的 Blocklet Server 实例,用于处理验证请求。该库旨在与由 Blocklet Server 提供支持的后端无缝协作。
|
|
11
|
-
|
|
12
|
-
## 第 1 步:安装库
|
|
13
|
-
|
|
14
|
-
首先,将 `@arcblock/did-connect-react` 包添加到你的项目中。你可以使用 npm 或 yarn。
|
|
15
|
-
|
|
16
|
-
```bash Install with npm icon=logos:npm
|
|
17
|
-
npm install @arcblock/did-connect-react
|
|
18
|
-
```
|
|
19
|
-
|
|
20
|
-
```bash Install with yarn icon=logos:yarn
|
|
21
|
-
yarn add @arcblock/did-connect-react
|
|
22
|
-
```
|
|
23
|
-
|
|
24
|
-
## 第 2 步:设置 SessionProvider
|
|
25
|
-
|
|
26
|
-
`SessionProvider` 是该库的核心。它是一个上下文提供程序,用于管理用户的会话状态(如登录状态和用户信息),并使其对所有包裹在其中的组件可用。你应该将其放置在应用的根部,例如,在你的 `App.js` 文件中。
|
|
27
|
-
|
|
28
|
-
对于基于 Blocklet 构建的应用,推荐的初始化提供程序的方法是使用 `createAuthServiceSessionContext` 工厂函数。
|
|
29
|
-
|
|
30
|
-
```javascript App.js icon=logos:react
|
|
31
|
-
import React from 'react';
|
|
32
|
-
import { createAuthServiceSessionContext } from '@arcblock/did-connect-react/lib/Session';
|
|
33
|
-
import Home from './Home'; // 我们将在下一步创建此组件
|
|
34
|
-
|
|
35
|
-
// createAuthServiceSessionContext 针对 Blocklet 环境进行了优化
|
|
36
|
-
const { SessionProvider } = createAuthServiceSessionContext();
|
|
37
|
-
|
|
38
|
-
function App() {
|
|
39
|
-
return (
|
|
40
|
-
// serviceHost prop 指向后端验证服务的基础路径。
|
|
41
|
-
// 对于标准 Blocklet,这通常是 blocklet 的前缀或“/”。
|
|
42
|
-
<SessionProvider serviceHost="/">
|
|
43
|
-
<Home />
|
|
44
|
-
</SessionProvider>
|
|
45
|
-
);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export default App;
|
|
49
|
-
```
|
|
50
|
-
|
|
51
|
-
## 第 3 步:实现登录和登出
|
|
52
|
-
|
|
53
|
-
现在,让我们创建一个允许用户登录和登出的组件。我们可以使用 React 的 `useContext` 钩子和 `SessionContext` 来访问会话数据和函数。
|
|
54
|
-
|
|
55
|
-
`session` 对象为你提供了所需的一切:当前的 `user`、`loading` 状态,以及像 `login()` 和 `logout()` 这样的函数。
|
|
56
|
-
|
|
57
|
-
```javascript Home.js icon=logos:react
|
|
58
|
-
import React, { useContext } from 'react';
|
|
59
|
-
import { SessionContext } from '@arcblock/did-connect-react/lib/Session';
|
|
60
|
-
import ConnectButton from '@arcblock/did-connect-react/lib/Button';
|
|
61
|
-
import Button from '@arcblock/ux/lib/Button'; // 一个通用按钮
|
|
62
|
-
import CircularProgress from '@mui/material/CircularProgress';
|
|
63
|
-
|
|
64
|
-
function Home() {
|
|
65
|
-
// 访问会话上下文
|
|
66
|
-
const { session } = useContext(SessionContext);
|
|
67
|
-
|
|
68
|
-
// 解构以便更轻松地访问
|
|
69
|
-
const { user, loading, login, logout } = session;
|
|
70
|
-
|
|
71
|
-
// 在会话初始化期间显示加载指示器
|
|
72
|
-
if (loading) {
|
|
73
|
-
return <CircularProgress />;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
return (
|
|
77
|
-
<div style={{ padding: '20px', textAlign: 'center' }}>
|
|
78
|
-
{user ? (
|
|
79
|
-
// 如果用户已登录,则显示其 DID 和登出按钮
|
|
80
|
-
<div>
|
|
81
|
-
<h1>Welcome!</h1>
|
|
82
|
-
<p>Your DID: {user.did}</p>
|
|
83
|
-
<Button variant="contained" color="secondary" onClick={logout}>
|
|
84
|
-
Logout
|
|
85
|
-
</Button>
|
|
86
|
-
</div>
|
|
87
|
-
) : (
|
|
88
|
-
// 如果未登录,则显示登录按钮
|
|
89
|
-
<div>
|
|
90
|
-
<h1>Please Sign In</h1>
|
|
91
|
-
<ConnectButton onClick={() => login()}>
|
|
92
|
-
Connect Wallet
|
|
93
|
-
</ConnectButton>
|
|
94
|
-
</div>
|
|
95
|
-
)}
|
|
96
|
-
</div>
|
|
97
|
-
);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
export default Home;
|
|
101
|
-
```
|
|
102
|
-
|
|
103
|
-
当用户点击 `ConnectButton` 时,`login()` 函数被调用。该函数负责打开 DID Connect 弹窗,显示一个二维码供用户使用其 DID 钱包扫描,并管理整个验证过程。
|
|
104
|
-
|
|
105
|
-
## 第 4 步:访问用户数据
|
|
106
|
-
|
|
107
|
-
用户成功登录后,`session.user` 对象将被填充。该对象包含从用户 DID 钱包中检索到的用户个人资料信息。
|
|
108
|
-
|
|
109
|
-
由上下文提供的 `session` 对象是你与用户验证状态交互的主要接口。以下是其关键属性:
|
|
110
|
-
|
|
111
|
-
<x-field-group>
|
|
112
|
-
<x-field data-name="user" data-type="object | null" data-desc="已验证的用户对象。如果没有用户登录,则为 `null`。">
|
|
113
|
-
<x-field data-name="did" data-type="string" data-desc="用户的去中心化身份标识(DID)。"></x-field>
|
|
114
|
-
<x-field data-name="fullName" data-type="string" data-desc="来自用户钱包个人资料的全名。"></x-field>
|
|
115
|
-
<x-field data-name="email" data-type="string" data-desc="来自用户钱包个人资料的电子邮件。"></x-field>
|
|
116
|
-
</x-field>
|
|
117
|
-
<x-field data-name="loading" data-type="boolean" data-desc="在会话初始化或刷新期间为 `true`,否则为 `false`。"></x-field>
|
|
118
|
-
<x-field data-name="login" data-type="function" data-desc="一个启动登录过程的函数,它会打开 DID Connect UI。"></x-field>
|
|
119
|
-
<x-field data-name="logout" data-type="function" data-desc="一个用于登出用户并清除会话的函数。"></x-field>
|
|
120
|
-
<x-field data-name="initialized" data-type="boolean" data-desc="初始会话检查完成后为 `true`。"></x-field>
|
|
121
|
-
</x-field-group>
|
|
122
|
-
|
|
123
|
-
## 你已准备就绪!
|
|
124
|
-
|
|
125
|
-
恭喜!你已成功在你的 React 应用中实现了一个完整的基于 DID 的验证流程。用户现在可以使用他们的 DID 钱包登录,并且你的应用可以安全地访问他们的会话信息。
|
|
126
|
-
|
|
127
|
-
### 后续步骤
|
|
128
|
-
|
|
129
|
-
要深入了解并探索更多高级功能,请查看以下部分:
|
|
130
|
-
|
|
131
|
-
<x-cards>
|
|
132
|
-
<x-card data-title="会话管理 (SessionProvider)" data-icon="lucide:key-round" data-href="/core-components/session-provider">
|
|
133
|
-
了解 `SessionProvider` 的所有配置选项,以自定义会话行为、超时等。
|
|
134
|
-
</x-card>
|
|
135
|
-
<x-card data-title="连接 UI (DidConnect)" data-icon="lucide:qr-code" data-href="/core-components/did-connect">
|
|
136
|
-
探索 `DidConnect` 组件,以声明方式将连接 UI 直接嵌入到你的页面中。
|
|
137
|
-
</x-card>
|
|
138
|
-
</x-cards>
|
|
@@ -1,178 +0,0 @@
|
|
|
1
|
-
# useConnect
|
|
2
|
-
|
|
3
|
-
`useConnect`フックは、`DidConnect` UIモーダルをプログラムで開閉・管理するための強力な方法を提供します。宣言的な`Button`コンポーネントが提供するものよりも接続フローをより詳細に制御する必要がある場合、例えばカスタムUI要素、メニュー項目、またはアプリケーションイベントに応じてモーダルをトリガーする場合に理想的なソリューションです。
|
|
4
|
-
|
|
5
|
-
このフックは`Button`コンポーネントの背後にあるエンジンであり、同じコア機能への直接アクセスを提供します。
|
|
6
|
-
|
|
7
|
-
## 仕組み
|
|
8
|
-
|
|
9
|
-
`useConnect`フックは、主に2つの要素を返します:
|
|
10
|
-
|
|
11
|
-
1. **`connectHolder`**:コンポーネントツリーにレンダリングする必要があるReact要素です。この要素は、`DidConnect`モーダルがアクティブ化されたときにそれをレンダリングする責任があります。`open`関数が呼び出されるまで非表示のままです。
|
|
12
|
-
2. **`connectApi`**:モーダルのライフサイクルを制御するために使用する関数(`open`、`close`、`openPopup`、`loginOAuth`)を含むオブジェクトです。
|
|
13
|
-
|
|
14
|
-
<!-- DIAGRAM_IMAGE_START:flowchart:4:3:1765962229 -->
|
|
15
|
-

|
|
16
|
-
<!-- DIAGRAM_IMAGE_END -->
|
|
17
|
-
|
|
18
|
-
## セットアップ
|
|
19
|
-
|
|
20
|
-
フックを使用するには、それをインポートし、コンポーネント内で呼び出します。次に、アプリケーションのどこか、できればトップレベルのコンポーネントで`connectHolder`をレンダリングして、モーダルが他のすべてのコンテンツの上にオーバーレイできるようにします。
|
|
21
|
-
|
|
22
|
-
```javascript Basic Setup icon=logos:javascript
|
|
23
|
-
import React from 'react';
|
|
24
|
-
import { useConnect } from '@arcblock/did-connect-react';
|
|
25
|
-
|
|
26
|
-
function MyComponent() {
|
|
27
|
-
// 1. フックを初期化する
|
|
28
|
-
const { connectHolder, connectApi } = useConnect();
|
|
29
|
-
|
|
30
|
-
const handleLogin = () => {
|
|
31
|
-
// 2. API を使用してモーダルを開く
|
|
32
|
-
connectApi.open({
|
|
33
|
-
action: 'login',
|
|
34
|
-
onSuccess: (result) => {
|
|
35
|
-
console.log('ログイン成功!', result);
|
|
36
|
-
},
|
|
37
|
-
onClose: () => {
|
|
38
|
-
console.log('モーダルがユーザーによって閉じられました。');
|
|
39
|
-
}
|
|
40
|
-
});
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
return (
|
|
44
|
-
<div>
|
|
45
|
-
<button type="button" onClick={handleLogin}>
|
|
46
|
-
DIDでログイン
|
|
47
|
-
</button>
|
|
48
|
-
|
|
49
|
-
{/* 3. ホルダーコンポーネントをレンダリングする */}
|
|
50
|
-
{connectHolder}
|
|
51
|
-
</div>
|
|
52
|
-
);
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export default MyComponent;
|
|
56
|
-
```
|
|
57
|
-
|
|
58
|
-
## APIリファレンス
|
|
59
|
-
|
|
60
|
-
`connectApi`オブジェクトは、接続プロセスを制御するための以下のメソッドを提供します。
|
|
61
|
-
|
|
62
|
-
### `connectApi.open(options)`
|
|
63
|
-
|
|
64
|
-
これは`DidConnect`モーダルをトリガーして表示するための主要な関数です。接続セッションの動作、外観、およびコールバックを設定するための単一の`options`オブジェクトを受け入れます。
|
|
65
|
-
|
|
66
|
-
**パラメータ (`options`)**
|
|
67
|
-
|
|
68
|
-
<x-field-group>
|
|
69
|
-
<x-field data-name="action" data-type="string" data-required="true">
|
|
70
|
-
<x-field-desc markdown>接続リクエストの目的、例:`login`、`claim`、`sign`など。これはウォレット内のワークフローを決定します。</x-field-desc>
|
|
71
|
-
</x-field>
|
|
72
|
-
<x-field data-name="onSuccess" data-type="(result: object) => void" data-required="false">
|
|
73
|
-
<x-field-desc markdown>ユーザーがウォレットでアクションを正常に完了したときに実行されるコールバック関数。`result`オブジェクトにはウォレットからのレスポンスが含まれます。</x-field-desc>
|
|
74
|
-
</x-field>
|
|
75
|
-
<x-field data-name="onClose" data-type="() => void" data-required="false">
|
|
76
|
-
<x-field-desc markdown>ユーザーがアクションを完了せずに手動でモーダルを閉じたときに実行されるコールバック関数。</x-field-desc>
|
|
77
|
-
</x-field>
|
|
78
|
-
<x-field data-name="onError" data-type="(error: any) => void" data-required="false">
|
|
79
|
-
<x-field-desc markdown>タイムアウトやネットワークの問題など、プロセス中にエラーが発生した場合に実行されるコールバック関数。</x-field-desc>
|
|
80
|
-
</x-field>
|
|
81
|
-
<x-field data-name="messages" data-type="ConnectMessages" data-required="false">
|
|
82
|
-
<x-field-desc markdown>モーダルに表示されるテキストをカスタマイズするためのオブジェクト。詳細は`ConnectMessages`型を参照してください。</x-field-desc>
|
|
83
|
-
<x-field data-name="title" data-type="string" data-desc="モーダルのタイトル。"></x-field>
|
|
84
|
-
<x-field data-name="scan" data-type="string" data-desc="QRコードに付随するテキスト。"></x-field>
|
|
85
|
-
<x-field data-name="confirm" data-type="string" data-desc="確認ステップのテキスト。"></x-field>
|
|
86
|
-
<x-field data-name="success" data-type="ReactNode" data-desc="成功画面のコンテンツ。"></x-field>
|
|
87
|
-
</x-field>
|
|
88
|
-
<x-field data-name="popup" data-type="boolean" data-default="true">
|
|
89
|
-
<x-field-desc markdown>`true`の場合、UIはモーダルダイアログとして表示されます。`false`の場合、インラインでレンダリングされ、`containerEl`の設定が必要です。</x-field-desc>
|
|
90
|
-
</x-field>
|
|
91
|
-
<x-field data-name="containerEl" data-type="Element" data-required="false">
|
|
92
|
-
<x-field-desc markdown>`popup`が`false`に設定されている場合に`DidConnect`コンポーネントがレンダリングされるDOM要素。</x-field-desc>
|
|
93
|
-
</x-field>
|
|
94
|
-
<x-field data-name="closeTimeout" data-type="number" data-default="2000">
|
|
95
|
-
<x-field-desc markdown>接続成功後、モーダルが自動的に閉じるまでの遅延時間(ミリ秒)。</x-field-desc>
|
|
96
|
-
</x-field>
|
|
97
|
-
<x-field data-name="checkInterval" data-type="number" data-default="2000">
|
|
98
|
-
<x-field-desc markdown>セッションステータスを確認するためにサーバーをポーリングする間隔(ミリ秒)。</x-field-desc>
|
|
99
|
-
</x-field>
|
|
100
|
-
<x-field data-name="checkTimeout" data-type="number" data-default="300000">
|
|
101
|
-
<x-field-desc markdown>接続試行がタイムアウトするまでの合計時間(ミリ秒)。</x-field-desc>
|
|
102
|
-
</x-field>
|
|
103
|
-
<x-field data-name="extraParams" data-type="object" data-default="{}">
|
|
104
|
-
<x-field-desc markdown>セッション作成リクエストに含める追加のパラメータで、サーバー側で取得できます。</x-field-desc>
|
|
105
|
-
</x-field>
|
|
106
|
-
</x-field-group>
|
|
107
|
-
|
|
108
|
-
### `connectApi.close()`
|
|
109
|
-
|
|
110
|
-
`DidConnect`モーダルを手動で閉じます。モーダルは成功時またはユーザーによるキャンセル時に自動的に閉じますが、他の理由でプログラムから閉じる必要がある場合はこの関数を呼び出すことができます。
|
|
111
|
-
|
|
112
|
-
```javascript icon=lucide:x-circle
|
|
113
|
-
// 例:成功コールバックで短い遅延の後にモーダルを閉じる
|
|
114
|
-
connectApi.open({
|
|
115
|
-
action: 'login',
|
|
116
|
-
onSuccess: () => {
|
|
117
|
-
showTemporarySuccessMessage();
|
|
118
|
-
setTimeout(() => {
|
|
119
|
-
connectApi.close();
|
|
120
|
-
}, 1500);
|
|
121
|
-
},
|
|
122
|
-
closeTimeout: 999999 // 自動クローズを防止
|
|
123
|
-
});
|
|
124
|
-
```
|
|
125
|
-
|
|
126
|
-
### `connectApi.openPopup(params, options)`
|
|
127
|
-
|
|
128
|
-
この関数は、現在のページ内のモーダルの代わりに、新しいブラウザのポップアップウィンドウでDID Connectフローを開く代替の接続方法を提供します。これは、特定のOAuthのようなフローや、モーダルを注入できないサイトとの統合に役立ちます。
|
|
129
|
-
|
|
130
|
-
**パラメータ**
|
|
131
|
-
|
|
132
|
-
<x-field-group>
|
|
133
|
-
<x-field data-name="params" data-type="ConnectProps" data-required="true">
|
|
134
|
-
<x-field-desc markdown>`open`メソッドのオプションに似ています。主な違いは、認証方法(例:「github」、「google」)を識別するために`params.extraParams.provider`が**必須**であることです。</x-field-desc>
|
|
135
|
-
</x-field>
|
|
136
|
-
<x-field data-name="options" data-type="object" data-required="false">
|
|
137
|
-
<x-field-desc markdown>ポップアップウィンドウ自体の設定。</x-field-desc>
|
|
138
|
-
<x-field data-name="baseUrl" data-type="string" data-desc="ポップアップURLを構築するためのベースURL。"></x-field>
|
|
139
|
-
<x-field data-name="locale" data-type="string" data-default="en" data-desc="ポップアップコンテンツのロケール。"></x-field>
|
|
140
|
-
<x-field data-name="popupOptions" data-type="object" data-desc="ポップアップのサイズや機能をカスタマイズするために`window.open()`に直接渡されるオプション。"></x-field>
|
|
141
|
-
</x-field>
|
|
142
|
-
</x-field-group>
|
|
143
|
-
|
|
144
|
-
**使用法**
|
|
145
|
-
|
|
146
|
-
`openPopup`関数は、成功時に認証データで解決されるか、失敗またはキャンセル時に拒否される`Promise`を返します。
|
|
147
|
-
|
|
148
|
-
```javascript icon=lucide:external-link
|
|
149
|
-
const handleGithubLogin = async () => {
|
|
150
|
-
try {
|
|
151
|
-
const result = await connectApi.openPopup({
|
|
152
|
-
action: 'login',
|
|
153
|
-
onSuccess: () => console.log('このコールバックもトリガーされます'),
|
|
154
|
-
extraParams: { provider: 'github' } // これは必須です
|
|
155
|
-
});
|
|
156
|
-
console.log('ポップアップ経由でのログイン成功:', result);
|
|
157
|
-
} catch (err) {
|
|
158
|
-
if (err.message === 'Popup closed') {
|
|
159
|
-
console.log('ユーザーがポップアップウィンドウを閉じました。');
|
|
160
|
-
} else {
|
|
161
|
-
console.error('エラーが発生しました:', err);
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
};
|
|
165
|
-
```
|
|
166
|
-
|
|
167
|
-
### `connectApi.loginOAuth(options)`
|
|
168
|
-
|
|
169
|
-
これはOAuthログインフローを直接開始するためのヘルパー関数です。ライブラリの他の部分で内部的に使用される低レベルのユーティリティです。
|
|
170
|
-
|
|
171
|
-
**パラメータ (`options`)**
|
|
172
|
-
|
|
173
|
-
<x-field-group>
|
|
174
|
-
<x-field data-name="provider" data-type="string" data-required="true" data-desc="使用するOAuthプロバイダー、例:「github」、「google」。"></x-field>
|
|
175
|
-
<x-field data-name="action" data-type="string" data-required="true" data-desc="アクション、通常は「login」。"></x-field>
|
|
176
|
-
<x-field data-name="extraParams" data-type="object" data-required="false" data-desc="OAuthフローの追加パラメータ。"></x-field>
|
|
177
|
-
<x-field data-name="onLogin" data-type="function" data-required="true" data-desc="ログイン成功時に実行されるコールバック関数。"></x-field>
|
|
178
|
-
</x-field-group>
|
|
@@ -1,178 +0,0 @@
|
|
|
1
|
-
# useConnect
|
|
2
|
-
|
|
3
|
-
The `useConnect` hook provides a powerful, programmatic way to open, close, and manage the `DidConnect` UI modal. It's the ideal solution when you need more control over the connection flow than the declarative `Button` component offers, such as triggering the modal from a custom UI element, a menu item, or in response to an application event.
|
|
4
|
-
|
|
5
|
-
This hook is the engine behind the `Button` component and gives you direct access to the same core functionalities.
|
|
6
|
-
|
|
7
|
-
## How It Works
|
|
8
|
-
|
|
9
|
-
The `useConnect` hook returns two main elements:
|
|
10
|
-
|
|
11
|
-
1. **`connectHolder`**: A React element that must be rendered in your component tree. This element is responsible for rendering the `DidConnect` modal when it's activated. It remains invisible until the `open` function is called.
|
|
12
|
-
2. **`connectApi`**: An object containing the functions (`open`, `close`, `openPopup`, `loginOAuth`) you'll use to control the modal's lifecycle.
|
|
13
|
-
|
|
14
|
-
<!-- DIAGRAM_IMAGE_START:flowchart:4:3:1765962229 -->
|
|
15
|
-

|
|
16
|
-
<!-- DIAGRAM_IMAGE_END -->
|
|
17
|
-
|
|
18
|
-
## Setup
|
|
19
|
-
|
|
20
|
-
To use the hook, import it and call it within your component. Then, ensure you render the `connectHolder` somewhere in your application, preferably at a top-level component to ensure the modal can overlay all other content.
|
|
21
|
-
|
|
22
|
-
```javascript Basic Setup icon=logos:javascript
|
|
23
|
-
import React from 'react';
|
|
24
|
-
import { useConnect } from '@arcblock/did-connect-react';
|
|
25
|
-
|
|
26
|
-
function MyComponent() {
|
|
27
|
-
// 1. Initialize the hook
|
|
28
|
-
const { connectHolder, connectApi } = useConnect();
|
|
29
|
-
|
|
30
|
-
const handleLogin = () => {
|
|
31
|
-
// 2. Use the API to open the modal
|
|
32
|
-
connectApi.open({
|
|
33
|
-
action: 'login',
|
|
34
|
-
onSuccess: (result) => {
|
|
35
|
-
console.log('Login successful!', result);
|
|
36
|
-
},
|
|
37
|
-
onClose: () => {
|
|
38
|
-
console.log('Modal closed by user.');
|
|
39
|
-
}
|
|
40
|
-
});
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
return (
|
|
44
|
-
<div>
|
|
45
|
-
<button type="button" onClick={handleLogin}>
|
|
46
|
-
Login with DID
|
|
47
|
-
</button>
|
|
48
|
-
|
|
49
|
-
{/* 3. Render the holder component */}
|
|
50
|
-
{connectHolder}
|
|
51
|
-
</div>
|
|
52
|
-
);
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export default MyComponent;
|
|
56
|
-
```
|
|
57
|
-
|
|
58
|
-
## API Reference
|
|
59
|
-
|
|
60
|
-
The `connectApi` object provides the following methods to control the connection process.
|
|
61
|
-
|
|
62
|
-
### `connectApi.open(options)`
|
|
63
|
-
|
|
64
|
-
This is the primary function to trigger and display the `DidConnect` modal. It accepts a single `options` object to configure the behavior, appearance, and callbacks for the connection session.
|
|
65
|
-
|
|
66
|
-
**Parameters (`options`)**
|
|
67
|
-
|
|
68
|
-
<x-field-group>
|
|
69
|
-
<x-field data-name="action" data-type="string" data-required="true">
|
|
70
|
-
<x-field-desc markdown>The purpose of the connection request, e.g., `login`, `claim`, `sign`, etc. This determines the workflow within the wallet.</x-field-desc>
|
|
71
|
-
</x-field>
|
|
72
|
-
<x-field data-name="onSuccess" data-type="(result: object) => void" data-required="false">
|
|
73
|
-
<x-field-desc markdown>Callback function executed when the user successfully completes the action in their wallet. The `result` object contains the response from the wallet.</x-field-desc>
|
|
74
|
-
</x-field>
|
|
75
|
-
<x-field data-name="onClose" data-type="() => void" data-required="false">
|
|
76
|
-
<x-field-desc markdown>Callback function executed when the user manually closes the modal without completing the action.</x-field-desc>
|
|
77
|
-
</x-field>
|
|
78
|
-
<x-field data-name="onError" data-type="(error: any) => void" data-required="false">
|
|
79
|
-
<x-field-desc markdown>Callback function executed if an error occurs during the process, such as a timeout or a network issue.</x-field-desc>
|
|
80
|
-
</x-field>
|
|
81
|
-
<x-field data-name="messages" data-type="ConnectMessages" data-required="false">
|
|
82
|
-
<x-field-desc markdown>An object to customize the text displayed in the modal. See `ConnectMessages` type for details.</x-field-desc>
|
|
83
|
-
<x-field data-name="title" data-type="string" data-desc="Modal title."></x-field>
|
|
84
|
-
<x-field data-name="scan" data-type="string" data-desc="Text accompanying the QR code."></x-field>
|
|
85
|
-
<x-field data-name="confirm" data-type="string" data-desc="Text for the confirmation step."></x-field>
|
|
86
|
-
<x-field data-name="success" data-type="ReactNode" data-desc="Content for the success screen."></x-field>
|
|
87
|
-
</x-field>
|
|
88
|
-
<x-field data-name="popup" data-type="boolean" data-default="true">
|
|
89
|
-
<x-field-desc markdown>If `true`, the UI is displayed as a modal dialog. If `false`, it renders inline, requiring `containerEl` to be set.</x-field-desc>
|
|
90
|
-
</x-field>
|
|
91
|
-
<x-field data-name="containerEl" data-type="Element" data-required="false">
|
|
92
|
-
<x-field-desc markdown>A DOM element where the `DidConnect` component will be rendered if `popup` is set to `false`.</x-field-desc>
|
|
93
|
-
</x-field>
|
|
94
|
-
<x-field data-name="closeTimeout" data-type="number" data-default="2000">
|
|
95
|
-
<x-field-desc markdown>The delay in milliseconds before the modal automatically closes after a successful connection.</x-field-desc>
|
|
96
|
-
</x-field>
|
|
97
|
-
<x-field data-name="checkInterval" data-type="number" data-default="2000">
|
|
98
|
-
<x-field-desc markdown>The interval in milliseconds for polling the server to check the session status.</x-field-desc>
|
|
99
|
-
</x-field>
|
|
100
|
-
<x-field data-name="checkTimeout" data-type="number" data-default="300000">
|
|
101
|
-
<x-field-desc markdown>The total time in milliseconds before the connection attempt times out.</x-field-desc>
|
|
102
|
-
</x-field>
|
|
103
|
-
<x-field data-name="extraParams" data-type="object" data-default="{}">
|
|
104
|
-
<x-field-desc markdown>Any extra parameters to be included in the session creation request, which can be retrieved on the server-side.</x-field-desc>
|
|
105
|
-
</x-field>
|
|
106
|
-
</x-field-group>
|
|
107
|
-
|
|
108
|
-
### `connectApi.close()`
|
|
109
|
-
|
|
110
|
-
Manually closes the `DidConnect` modal. The modal closes automatically on success or user cancellation, but you can call this function if you need to close it programmatically for other reasons.
|
|
111
|
-
|
|
112
|
-
```javascript icon=lucide:x-circle
|
|
113
|
-
// Example: Close the modal after a short delay in the success callback
|
|
114
|
-
connectApi.open({
|
|
115
|
-
action: 'login',
|
|
116
|
-
onSuccess: () => {
|
|
117
|
-
showTemporarySuccessMessage();
|
|
118
|
-
setTimeout(() => {
|
|
119
|
-
connectApi.close();
|
|
120
|
-
}, 1500);
|
|
121
|
-
},
|
|
122
|
-
closeTimeout: 999999 // Prevent auto-close
|
|
123
|
-
});
|
|
124
|
-
```
|
|
125
|
-
|
|
126
|
-
### `connectApi.openPopup(params, options)`
|
|
127
|
-
|
|
128
|
-
This function provides an alternative connection method that opens the DID Connect flow in a new browser popup window instead of a modal within the current page. This is useful for certain OAuth-like flows or when integrating with sites where you cannot inject a modal.
|
|
129
|
-
|
|
130
|
-
**Parameters**
|
|
131
|
-
|
|
132
|
-
<x-field-group>
|
|
133
|
-
<x-field data-name="params" data-type="ConnectProps" data-required="true">
|
|
134
|
-
<x-field-desc markdown>Similar to the `open` method's options. A key difference is that `params.extraParams.provider` is **required** to identify the authentication method (e.g., 'github', 'google').</x-field-desc>
|
|
135
|
-
</x-field>
|
|
136
|
-
<x-field data-name="options" data-type="object" data-required="false">
|
|
137
|
-
<x-field-desc markdown>Configuration for the popup window itself.</x-field-desc>
|
|
138
|
-
<x-field data-name="baseUrl" data-type="string" data-desc="The base URL for constructing the popup URL."></x-field>
|
|
139
|
-
<x-field data-name="locale" data-type="string" data-default="en" data-desc="The locale for the popup content."></x-field>
|
|
140
|
-
<x-field data-name="popupOptions" data-type="object" data-desc="Options passed directly to `window.open()` for customizing the popup's size and features."></x-field>
|
|
141
|
-
</x-field>
|
|
142
|
-
</x-field-group>
|
|
143
|
-
|
|
144
|
-
**Usage**
|
|
145
|
-
|
|
146
|
-
The `openPopup` function returns a `Promise` that resolves with the authentication data on success or rejects on failure or cancellation.
|
|
147
|
-
|
|
148
|
-
```javascript icon=lucide:external-link
|
|
149
|
-
const handleGithubLogin = async () => {
|
|
150
|
-
try {
|
|
151
|
-
const result = await connectApi.openPopup({
|
|
152
|
-
action: 'login',
|
|
153
|
-
onSuccess: () => console.log('This callback is also triggered'),
|
|
154
|
-
extraParams: { provider: 'github' } // This is required
|
|
155
|
-
});
|
|
156
|
-
console.log('Login successful via popup:', result);
|
|
157
|
-
} catch (err) {
|
|
158
|
-
if (err.message === 'Popup closed') {
|
|
159
|
-
console.log('User closed the popup window.');
|
|
160
|
-
} else {
|
|
161
|
-
console.error('An error occurred:', err);
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
};
|
|
165
|
-
```
|
|
166
|
-
|
|
167
|
-
### `connectApi.loginOAuth(options)`
|
|
168
|
-
|
|
169
|
-
This is a helper function to directly initiate an OAuth login flow. It's a lower-level utility that is internally used by other parts of the library.
|
|
170
|
-
|
|
171
|
-
**Parameters (`options`)**
|
|
172
|
-
|
|
173
|
-
<x-field-group>
|
|
174
|
-
<x-field data-name="provider" data-type="string" data-required="true" data-desc="The OAuth provider to use, e.g., 'github', 'google'."></x-field>
|
|
175
|
-
<x-field data-name="action" data-type="string" data-required="true" data-desc="The action, typically 'login'."></x-field>
|
|
176
|
-
<x-field data-name="extraParams" data-type="object" data-required="false" data-desc="Additional parameters for the OAuth flow."></x-field>
|
|
177
|
-
<x-field data-name="onLogin" data-type="function" data-required="true" data-desc="Callback function executed upon successful login."></x-field>
|
|
178
|
-
</x-field-group>
|