@arcblock/did-connect-react 3.4.15 → 3.5.1
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/package.json.js +1 -1
- package/package.json +11 -6
- 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 -328
- package/src/Connect/components/login-item/connect-provider-list.jsx +0 -473
- package/src/Connect/components/login-item/login-method-item.jsx +0 -139
- package/src/Connect/components/login-item/mobile-login-item.jsx +0 -184
- package/src/Connect/components/login-item/passkey-login-item.jsx +0 -56
- package/src/Connect/components/login-item/wallet-login-options.jsx +0 -129
- package/src/Connect/components/login-item/web-login-item.jsx +0 -159
- 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 -635
- package/src/Connect/contexts/state.jsx +0 -235
- 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 -168
- 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 -35
- package/src/Connect/plugins/email/placeholder.jsx +0 -372
- 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,197 +0,0 @@
|
|
|
1
|
-
# セッション管理 (SessionProvider)
|
|
2
|
-
|
|
3
|
-
`SessionProvider`は`@arcblock/did-connect-react`ライブラリの礎です。これは、セッション関連のすべてのロジック、状態、アクションをカプセル化するReact Context Providerです。アプリケーションを`SessionProvider`でラップすることにより、propsを手動で下に渡すことなく、アプリケーションツリー内のどのコンポーネントでもユーザーのセッション情報と認証メソッドを利用できるようになります。
|
|
4
|
-
|
|
5
|
-
このコンポーネントは、セッショントークンの保存やユーザーデータの管理から、ログイン/ログアウトフローの調整、セッションの自動更新まで、すべてを処理します。
|
|
6
|
-
|
|
7
|
-
## 仕組み
|
|
8
|
-
|
|
9
|
-
`SessionProvider`は、現在のユーザーの状態(`user`オブジェクト、`loading`ステータスなど)を保持するセッションコンテキストを作成し、その状態を変更する関数(例:`login`、`logout`)を提供します。`SessionProvider`内にネストされたどのコンポーネントも、このコンテキストを購読してこのデータにアクセスし、認証アクションをトリガーできます。
|
|
10
|
-
|
|
11
|
-
以下は、基本的なアーキテクチャを示す図です。
|
|
12
|
-
|
|
13
|
-
<!-- DIAGRAM_IMAGE_START:architecture:4:3:1765962229 -->
|
|
14
|
-

|
|
15
|
-
<!-- DIAGRAM_IMAGE_END -->
|
|
16
|
-
|
|
17
|
-
## はじめに
|
|
18
|
-
|
|
19
|
-
セッション管理機能を使用するには、まず`SessionProvider`をインポートしてアプリケーションをラップする必要があります。このライブラリは、設定済みのプロバイダーを作成するためのファクトリー関数をエクスポートします。
|
|
20
|
-
|
|
21
|
-
### 基本的なセットアップ
|
|
22
|
-
|
|
23
|
-
プロバイダーをセットアップする最も一般的な方法は、`createAuthServiceSessionContext`を使用することです。これは、ArcBlockエコシステム(Blocklets)内で実行されるアプリケーション向けに事前設定されています。
|
|
24
|
-
|
|
25
|
-
```javascript App.js icon=logos:javascript
|
|
26
|
-
import React from 'react';
|
|
27
|
-
import { createAuthServiceSessionContext } from '@arcblock/did-connect-react/lib/Session';
|
|
28
|
-
import AuthButton from './AuthButton';
|
|
29
|
-
|
|
30
|
-
// SessionProviderとコンテキストにアクセスするためのフックを作成
|
|
31
|
-
const { SessionProvider, SessionContext } = createAuthServiceSessionContext();
|
|
32
|
-
|
|
33
|
-
export const useSession = () => React.useContext(SessionContext);
|
|
34
|
-
|
|
35
|
-
export default function App() {
|
|
36
|
-
return (
|
|
37
|
-
<SessionProvider>
|
|
38
|
-
<div className="App">
|
|
39
|
-
<h1>Welcome to DID Connect</h1>
|
|
40
|
-
<AuthButton />
|
|
41
|
-
</div>
|
|
42
|
-
</SessionProvider>
|
|
43
|
-
);
|
|
44
|
-
}
|
|
45
|
-
```
|
|
46
|
-
|
|
47
|
-
```javascript AuthButton.js icon=logos:javascript
|
|
48
|
-
import React from 'react';
|
|
49
|
-
import { useSession } from './App';
|
|
50
|
-
|
|
51
|
-
export default function AuthButton() {
|
|
52
|
-
const { session } = useSession();
|
|
53
|
-
|
|
54
|
-
if (session.loading) {
|
|
55
|
-
return <div>Loading...</div>;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
if (session.user) {
|
|
59
|
-
return (
|
|
60
|
-
<div>
|
|
61
|
-
<p>Welcome, {session.user.fullName || session.user.did}</p>
|
|
62
|
-
<button onClick={() => session.logout()}>Logout</button>
|
|
63
|
-
</div>
|
|
64
|
-
);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
return <button onClick={() => session.login()}>Login</button>;
|
|
68
|
-
}
|
|
69
|
-
```
|
|
70
|
-
|
|
71
|
-
この例では、`createAuthServiceSessionContext`が`SessionProvider`を生成します。`App`をそれでラップすると、`AuthButton`のような子コンポーネントは、`useSession`フックを使用してセッションデータと関数にアクセスできるようになります。
|
|
72
|
-
|
|
73
|
-
## SessionProviderのProps
|
|
74
|
-
|
|
75
|
-
`SessionProvider`の動作は、さまざまなpropsを渡すことで設定できます。
|
|
76
|
-
|
|
77
|
-
<x-field-group>
|
|
78
|
-
<x-field data-name="serviceHost" data-type="string" data-desc="認証サービスバックエンドのベースURL。デフォルトは現在のBlockletのプレフィックスまたは'/'です。"></x-field>
|
|
79
|
-
<x-field data-name="autoConnect" data-type="boolean" data-default="false" data-desc="trueの場合、ページロード時にユーザーセッションが見つからないと、DID Connectダイアログが自動的に開きます。"></x-field>
|
|
80
|
-
<x-field data-name="autoDisconnect" data-type="boolean" data-default="true" data-desc="trueの場合、アプリケーションIDがセッションクッキーに保存されているものと一致しない場合、セッションは自動的にクリアされます。"></x-field>
|
|
81
|
-
<x-field data-name="protectedRoutes" data-type="string[]" data-default='["*"]' data-desc="保護するルートパターンの配列。未認証のユーザーがこれらのルートにアクセスしようとすると、セッションが解決されるまで読み込みスピナーが表示されます。ワイルドカード(*)がサポートされています。"></x-field>
|
|
82
|
-
<x-field data-name="locale" data-type="string" data-desc="DID Connect UIの表示言語を設定します。デフォルトはブラウザの言語です。"></x-field>
|
|
83
|
-
<x-field data-name="webWalletUrl" data-type="string" data-desc="接続プロセスで使用するウェブベースのDID WalletのURL。"></x-field>
|
|
84
|
-
<x-field data-name="timeout" data-type="number" data-default="120000" data-desc="接続プロセス中にDID Walletからの応答を待つためのタイムアウト(ミリ秒単位)。"></x-field>
|
|
85
|
-
<x-field data-name="useSocket" data-type="boolean" data-default="true" data-desc="ウォレットとのリアルタイム通信にWebSocketを使用するかどうかを決定し、より高速なユーザーエクスペリエンスを提供します。"></x-field>
|
|
86
|
-
<x-field data-name="extraParams" data-type="object" data-desc="バックエンドへのすべての認証リクエストとともに送信される追加パラメーターのオブジェクト。"></x-field>
|
|
87
|
-
</x-field-group>
|
|
88
|
-
|
|
89
|
-
## セッションコンテキストオブジェクト
|
|
90
|
-
|
|
91
|
-
`SessionContext`を利用すると、セッションの状態とAPIを含むオブジェクトを取得できます。主要なプロパティは`session`で、これには対話するすべてのデータとメソッドが含まれています。
|
|
92
|
-
|
|
93
|
-
### 状態プロパティ
|
|
94
|
-
|
|
95
|
-
これらは、ユーザーセッションの現在の状態を記述する読み取り専用のプロパティです。
|
|
96
|
-
|
|
97
|
-
<x-field-group>
|
|
98
|
-
<x-field data-name="user" data-type="User | null" data-desc="セッションが存在する場合、認証されたユーザーオブジェクト。それ以外の場合はnull。DID、名前、メール、アバターなどの詳細が含まれます。"></x-field>
|
|
99
|
-
<x-field data-name="loading" data-type="boolean" data-desc="セッションが初期化中、更新中、またはログイン/ログアウトプロセス中である場合はtrue。"></x-field>
|
|
100
|
-
<x-field data-name="initialized" data-type="boolean" data-desc="ページロード時に最初のセッションチェックが完了するとtrueになります。"></x-field>
|
|
101
|
-
<x-field data-name="open" data-type="boolean" data-desc="DID Connectダイアログが現在開いている場合はtrue。"></x-field>
|
|
102
|
-
<x-field data-name="error" data-type="string" data-desc="最後のセッション操作が失敗した場合、エラーメッセージが含まれます。"></x-field>
|
|
103
|
-
<x-field data-name="provider" data-type="string" data-desc="最後のログインに使用されたプロバイダー(例:'wallet'、'github'、'google')。"></x-field>
|
|
104
|
-
<x-field data-name="walletOS" data-type="string" data-desc="接続されたDID Walletのオペレーティングシステム(例:'ios'、'android'、'web')。"></x-field>
|
|
105
|
-
<x-field data-name="unReadCount" data-type="number" data-desc="現在のユーザーの未読通知の数。"></x-field>
|
|
106
|
-
</x-field-group>
|
|
107
|
-
|
|
108
|
-
### アクションメソッド
|
|
109
|
-
|
|
110
|
-
これらは、認証フローを開始したり、セッションを管理したりするために呼び出すことができる関数です。
|
|
111
|
-
|
|
112
|
-
<x-field-group>
|
|
113
|
-
<x-field data-name="login()" data-type="function">
|
|
114
|
-
<x-field-desc markdown>DID Connect UIを開いてユーザーのログインプロセスを開始します。オプションのコールバックとパラメーターを受け入れます。</x-field-desc>
|
|
115
|
-
</x-field>
|
|
116
|
-
<x-field data-name="logout()" data-type="function">
|
|
117
|
-
<x-field-desc markdown>現在のユーザーをログアウトさせ、ストレージからセッションをクリアします。完了時に実行されるオプションのコールバック関数を受け入れます。</x-field-desc>
|
|
118
|
-
</x-field>
|
|
119
|
-
<x-field data-name="switchDid()" data-type="function">
|
|
120
|
-
<x-field-desc markdown>ログイン中のユーザーが別のアカウントに切り替えることを許可します。アカウント切り替え用のコンテキストでDID Connect UIを再度開きます。</x-field-desc>
|
|
121
|
-
</x-field>
|
|
122
|
-
<x-field data-name="bindWallet()" data-type="function">
|
|
123
|
-
<x-field-desc markdown>ソーシャルプロバイダー(OAuth)またはパスキー経由でログインしたユーザーのために、この関数は既存のアカウントにDID Walletを接続するフローを開始します。</x-field-desc>
|
|
124
|
-
</x-field>
|
|
125
|
-
<x-field data-name="refresh()" data-type="function">
|
|
126
|
-
<x-field-desc markdown>サーバーからユーザーのセッションデータを手動で更新します。更新されたプロフィール情報や権限を取得するのに便利です。</x-field-desc>
|
|
127
|
-
</x-field>
|
|
128
|
-
<x-field data-name="switchProfile()" data-type="function">
|
|
129
|
-
<x-field-desc markdown>DID Connect UIを開き、ユーザーが同じDIDアカウント内の異なるプロフィール(例:個人用、仕事用)を切り替えられるようにします。</x-field-desc>
|
|
130
|
-
</x-field>
|
|
131
|
-
<x-field data-name="switchPassport()" data-type="function">
|
|
132
|
-
<x-field-desc markdown>DID Connect UIを開き、ユーザーが異なるパスポート(異なる資格情報や役割のセットを表す場合がある)を切り替えられるようにします。</x-field-desc>
|
|
133
|
-
</x-field>
|
|
134
|
-
<x-field data-name="connectToDidSpaceForFullAccess()" data-type="function">
|
|
135
|
-
<x-field-desc markdown>ユーザーのDID Spaceへの接続を開始し、フルアクセス権限を要求します。これは、ユーザーの個人データストアにデータを読み書きする必要があるアプリケーションでしばしば必要とされます。</x-field-desc>
|
|
136
|
-
</x-field>
|
|
137
|
-
<x-field data-name="withSecondaryAuth()" data-type="function">
|
|
138
|
-
<x-field-desc markdown>別の関数をラップする高階関数で、ラップされた関数が実行される前に、ユーザーが二次認証ステップ(例:パスワードの再入力、パスキーの使用)を完了する必要があります。これは機密性の高いアクションを保護するのに理想的です。</x-field-desc>
|
|
139
|
-
</x-field>
|
|
140
|
-
</x-field-group>
|
|
141
|
-
|
|
142
|
-
### イベントの購読
|
|
143
|
-
|
|
144
|
-
コンテキストは`events`オブジェクトも提供します。これは`EventEmitter3`のインスタンスです。これを使用して、セッションのさまざまなライフサイクルイベントを購読できます。
|
|
145
|
-
|
|
146
|
-
```javascript EventListener.js icon=logos:javascript
|
|
147
|
-
import React, { useEffect } from 'react';
|
|
148
|
-
import { useSession } from './App';
|
|
149
|
-
|
|
150
|
-
function EventListener() {
|
|
151
|
-
const { events } = useSession();
|
|
152
|
-
|
|
153
|
-
useEffect(() => {
|
|
154
|
-
const handleLogin = (result) => {
|
|
155
|
-
console.log('User logged in:', result.user.did);
|
|
156
|
-
};
|
|
157
|
-
|
|
158
|
-
const handleLogout = () => {
|
|
159
|
-
console.log('User logged out');
|
|
160
|
-
};
|
|
161
|
-
|
|
162
|
-
events.on('login', handleLogin);
|
|
163
|
-
events.on('logout', handleLogout);
|
|
164
|
-
|
|
165
|
-
// コンポーネントのアンマウント時にリスナーをクリーンアップ
|
|
166
|
-
return () => {
|
|
167
|
-
events.off('login', handleLogin);
|
|
168
|
-
events.off('logout', handleLogout);
|
|
169
|
-
};
|
|
170
|
-
}, [events]);
|
|
171
|
-
|
|
172
|
-
return null; // このコンポーネントは何もレンダリングしません
|
|
173
|
-
}
|
|
174
|
-
```
|
|
175
|
-
|
|
176
|
-
利用可能なイベントには、`login`、`login-failed`、`logout`、`change`、`bind-wallet`、`switch-passport`などがあります。
|
|
177
|
-
|
|
178
|
-
## 高度なカスタマイズ
|
|
179
|
-
|
|
180
|
-
異なるストレージメカニズムやより詳細な制御が必要なシナリオでは、汎用の`createSessionContext`ファクトリーを使用できます。
|
|
181
|
-
|
|
182
|
-
```javascript customSession.js icon=logos:javascript
|
|
183
|
-
import { createSessionContext } from '@arcblock/did-connect-react/lib/Session';
|
|
184
|
-
|
|
185
|
-
const { SessionProvider, SessionContext } = createSessionContext(
|
|
186
|
-
'my_app_session_token', // カスタムストレージキー
|
|
187
|
-
'ls', // cookieの代わりにlocalStorageを使用
|
|
188
|
-
{},
|
|
189
|
-
{
|
|
190
|
-
rolling: false, // 自動トークン更新を無効化
|
|
191
|
-
}
|
|
192
|
-
);
|
|
193
|
-
|
|
194
|
-
// ... 必要に応じてエクスポートして使用
|
|
195
|
-
```
|
|
196
|
-
|
|
197
|
-
これにより、ストレージエンジンを`localStorage`('ls')や`cookie`に変更したり、トークン更新ポリシーを調整したりするなど、セッションの動作を微調整できます。
|
|
@@ -1,197 +0,0 @@
|
|
|
1
|
-
# Session Management (SessionProvider)
|
|
2
|
-
|
|
3
|
-
The `SessionProvider` is the cornerstone of the `@arcblock/did-connect-react` library. It's a React Context Provider that encapsulates all session-related logic, state, and actions. By wrapping your application with `SessionProvider`, you make the user's session information and authentication methods available to any component in your application tree, without having to pass props down manually.
|
|
4
|
-
|
|
5
|
-
This component handles everything from storing session tokens and managing user data to orchestrating login/logout flows and refreshing sessions automatically.
|
|
6
|
-
|
|
7
|
-
## How It Works
|
|
8
|
-
|
|
9
|
-
`SessionProvider` creates a session context that holds the current user's state (`user` object, `loading` status, etc.) and provides functions to modify that state (e.g., `login`, `logout`). Any component nested within `SessionProvider` can subscribe to this context to access this data and trigger authentication actions.
|
|
10
|
-
|
|
11
|
-
Here is a diagram illustrating the basic architecture:
|
|
12
|
-
|
|
13
|
-
<!-- DIAGRAM_IMAGE_START:architecture:4:3:1765962229 -->
|
|
14
|
-

|
|
15
|
-
<!-- DIAGRAM_IMAGE_END -->
|
|
16
|
-
|
|
17
|
-
## Getting Started
|
|
18
|
-
|
|
19
|
-
To use the session management features, you first need to import and wrap your application with `SessionProvider`. The library exports factory functions to create a configured provider.
|
|
20
|
-
|
|
21
|
-
### Basic Setup
|
|
22
|
-
|
|
23
|
-
The most common way to set up the provider is by using `createAuthServiceSessionContext`, which is pre-configured for applications running within the ArcBlock ecosystem (Blocklets).
|
|
24
|
-
|
|
25
|
-
```javascript App.js icon=logos:javascript
|
|
26
|
-
import React from 'react';
|
|
27
|
-
import { createAuthServiceSessionContext } from '@arcblock/did-connect-react/lib/Session';
|
|
28
|
-
import AuthButton from './AuthButton';
|
|
29
|
-
|
|
30
|
-
// Create the SessionProvider and a hook to access the context
|
|
31
|
-
const { SessionProvider, SessionContext } = createAuthServiceSessionContext();
|
|
32
|
-
|
|
33
|
-
export const useSession = () => React.useContext(SessionContext);
|
|
34
|
-
|
|
35
|
-
export default function App() {
|
|
36
|
-
return (
|
|
37
|
-
<SessionProvider>
|
|
38
|
-
<div className="App">
|
|
39
|
-
<h1>Welcome to DID Connect</h1>
|
|
40
|
-
<AuthButton />
|
|
41
|
-
</div>
|
|
42
|
-
</SessionProvider>
|
|
43
|
-
);
|
|
44
|
-
}
|
|
45
|
-
```
|
|
46
|
-
|
|
47
|
-
```javascript AuthButton.js icon=logos:javascript
|
|
48
|
-
import React from 'react';
|
|
49
|
-
import { useSession } from './App';
|
|
50
|
-
|
|
51
|
-
export default function AuthButton() {
|
|
52
|
-
const { session } = useSession();
|
|
53
|
-
|
|
54
|
-
if (session.loading) {
|
|
55
|
-
return <div>Loading...</div>;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
if (session.user) {
|
|
59
|
-
return (
|
|
60
|
-
<div>
|
|
61
|
-
<p>Welcome, {session.user.fullName || session.user.did}</p>
|
|
62
|
-
<button onClick={() => session.logout()}>Logout</button>
|
|
63
|
-
</div>
|
|
64
|
-
);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
return <button onClick={() => session.login()}>Login</button>;
|
|
68
|
-
}
|
|
69
|
-
```
|
|
70
|
-
|
|
71
|
-
In this example, `createAuthServiceSessionContext` generates a `SessionProvider`. We wrap our `App` with it, and now any child component, like `AuthButton`, can use the `useSession` hook to access session data and functions.
|
|
72
|
-
|
|
73
|
-
## SessionProvider Props
|
|
74
|
-
|
|
75
|
-
You can configure the `SessionProvider`'s behavior by passing it various props.
|
|
76
|
-
|
|
77
|
-
<x-field-group>
|
|
78
|
-
<x-field data-name="serviceHost" data-type="string" data-desc="The base URL for the authentication service backend. Defaults to the current Blocklet's prefix or '/'."></x-field>
|
|
79
|
-
<x-field data-name="autoConnect" data-type="boolean" data-default="false" data-desc="If true, the DID Connect dialog will automatically open if no user session is found on page load."></x-field>
|
|
80
|
-
<x-field data-name="autoDisconnect" data-type="boolean" data-default="true" data-desc="If true, the session will be automatically cleared if the application ID mismatches the one stored in the session cookie."></x-field>
|
|
81
|
-
<x-field data-name="protectedRoutes" data-type="string[]" data-default='["*"]' data-desc="An array of route patterns to protect. If a non-authenticated user tries to access these routes, they will see a loading spinner until the session is resolved. Wildcards (*) are supported."></x-field>
|
|
82
|
-
<x-field data-name="locale" data-type="string" data-desc="Sets the display language for the DID Connect UI. Defaults to the browser's language."></x-field>
|
|
83
|
-
<x-field data-name="webWalletUrl" data-type="string" data-desc="The URL of the web-based DID Wallet to use for the connection process."></x-field>
|
|
84
|
-
<x-field data-name="timeout" data-type="number" data-default="120000" data-desc="The timeout in milliseconds for waiting for a response from DID Wallet during the connection process."></x-field>
|
|
85
|
-
<x-field data-name="useSocket" data-type="boolean" data-default="true" data-desc="Determines whether to use WebSockets for real-time communication with the wallet, providing a faster user experience."></x-field>
|
|
86
|
-
<x-field data-name="extraParams" data-type="object" data-desc="An object of extra parameters to be sent with every authentication request to the backend."></x-field>
|
|
87
|
-
</x-field-group>
|
|
88
|
-
|
|
89
|
-
## The Session Context Object
|
|
90
|
-
|
|
91
|
-
When you consume the `SessionContext`, you get an object containing the session state and API. The primary property is `session`, which holds all the data and methods you'll interact with.
|
|
92
|
-
|
|
93
|
-
### State Properties
|
|
94
|
-
|
|
95
|
-
These are read-only properties that describe the current state of the user session.
|
|
96
|
-
|
|
97
|
-
<x-field-group>
|
|
98
|
-
<x-field data-name="user" data-type="User | null" data-desc="The authenticated user object if a session exists, otherwise null. Contains details like DID, name, email, avatar, etc."></x-field>
|
|
99
|
-
<x-field data-name="loading" data-type="boolean" data-desc="True when the session is being initialized, refreshed, or during a login/logout process."></x-field>
|
|
100
|
-
<x-field data-name="initialized" data-type="boolean" data-desc="True once the initial session check has completed on page load."></x-field>
|
|
101
|
-
<x-field data-name="open" data-type="boolean" data-desc="True if the DID Connect dialog is currently open."></x-field>
|
|
102
|
-
<x-field data-name="error" data-type="string" data-desc="Contains an error message if the last session operation failed."></x-field>
|
|
103
|
-
<x-field data-name="provider" data-type="string" data-desc="The provider used for the last login (e.g., 'wallet', 'github', 'google')."></x-field>
|
|
104
|
-
<x-field data-name="walletOS" data-type="string" data-desc="The operating system of the connected DID Wallet (e.g., 'ios', 'android', 'web')."></x-field>
|
|
105
|
-
<x-field data-name="unReadCount" data-type="number" data-desc="The number of unread notifications for the current user."></x-field>
|
|
106
|
-
</x-field-group>
|
|
107
|
-
|
|
108
|
-
### Action Methods
|
|
109
|
-
|
|
110
|
-
These are functions you can call to initiate authentication flows or manage the session.
|
|
111
|
-
|
|
112
|
-
<x-field-group>
|
|
113
|
-
<x-field data-name="login()" data-type="function">
|
|
114
|
-
<x-field-desc markdown>Initiates the user login process by opening the DID Connect UI. It accepts optional callbacks and parameters.</x-field-desc>
|
|
115
|
-
</x-field>
|
|
116
|
-
<x-field data-name="logout()" data-type="function">
|
|
117
|
-
<x-field-desc markdown>Logs the current user out, clearing the session from storage. It accepts an optional callback function to be executed upon completion.</x-field-desc>
|
|
118
|
-
</x-field>
|
|
119
|
-
<x-field data-name="switchDid()" data-type="function">
|
|
120
|
-
<x-field-desc markdown>Allows a logged-in user to switch to a different DID account. It re-opens the DID Connect UI with a context for switching accounts.</x-field-desc>
|
|
121
|
-
</x-field>
|
|
122
|
-
<x-field data-name="bindWallet()" data-type="function">
|
|
123
|
-
<x-field-desc markdown>For users logged in via social providers (OAuth) or Passkeys, this function initiates a flow to connect a DID Wallet to their existing account.</x-field-desc>
|
|
124
|
-
</x-field>
|
|
125
|
-
<x-field data-name="refresh()" data-type="function">
|
|
126
|
-
<x-field-desc markdown>Manually triggers a refresh of the user's session data from the server. Useful for fetching updated profile information or permissions.</x-field-desc>
|
|
127
|
-
</x-field>
|
|
128
|
-
<x-field data-name="switchProfile()" data-type="function">
|
|
129
|
-
<x-field-desc markdown>Opens the DID Connect UI to allow the user to switch between different profiles within the same DID account (e.g., personal, work).</x-field-desc>
|
|
130
|
-
</x-field>
|
|
131
|
-
<x-field data-name="switchPassport()" data-type="function">
|
|
132
|
-
<x-field-desc markdown>Opens the DID Connect UI to allow the user to switch between different passports, which may represent different sets of credentials or roles.</x-field-desc>
|
|
133
|
-
</x-field>
|
|
134
|
-
<x-field data-name="connectToDidSpaceForFullAccess()" data-type="function">
|
|
135
|
-
<x-field-desc markdown>Initiates a connection to the user's DID Space, requesting full access permissions. This is often required for applications that need to read and write data to the user's personal data store.</x-field-desc>
|
|
136
|
-
</x-field>
|
|
137
|
-
<x-field data-name="withSecondaryAuth()" data-type="function">
|
|
138
|
-
<x-field-desc markdown>A higher-order function that wraps another function, requiring the user to complete a secondary authentication step (e.g., re-enter password, use passkey) before the wrapped function is executed. This is ideal for protecting sensitive actions.</x-field-desc>
|
|
139
|
-
</x-field>
|
|
140
|
-
</x-field-group>
|
|
141
|
-
|
|
142
|
-
### Event Subscription
|
|
143
|
-
|
|
144
|
-
The context also provides an `events` object, which is an instance of `EventEmitter3`. You can use it to subscribe to various lifecycle events of the session.
|
|
145
|
-
|
|
146
|
-
```javascript EventListener.js icon=logos:javascript
|
|
147
|
-
import React, { useEffect } from 'react';
|
|
148
|
-
import { useSession } from './App';
|
|
149
|
-
|
|
150
|
-
function EventListener() {
|
|
151
|
-
const { events } = useSession();
|
|
152
|
-
|
|
153
|
-
useEffect(() => {
|
|
154
|
-
const handleLogin = (result) => {
|
|
155
|
-
console.log('User logged in:', result.user.did);
|
|
156
|
-
};
|
|
157
|
-
|
|
158
|
-
const handleLogout = () => {
|
|
159
|
-
console.log('User logged out');
|
|
160
|
-
};
|
|
161
|
-
|
|
162
|
-
events.on('login', handleLogin);
|
|
163
|
-
events.on('logout', handleLogout);
|
|
164
|
-
|
|
165
|
-
// Cleanup listeners on component unmount
|
|
166
|
-
return () => {
|
|
167
|
-
events.off('login', handleLogin);
|
|
168
|
-
events.off('logout', handleLogout);
|
|
169
|
-
};
|
|
170
|
-
}, [events]);
|
|
171
|
-
|
|
172
|
-
return null; // This component doesn't render anything
|
|
173
|
-
}
|
|
174
|
-
```
|
|
175
|
-
|
|
176
|
-
Available events include `login`, `login-failed`, `logout`, `change`, `bind-wallet`, `switch-passport`, and more.
|
|
177
|
-
|
|
178
|
-
## Advanced Customization
|
|
179
|
-
|
|
180
|
-
For scenarios that require different storage mechanisms or more granular control, you can use the generic `createSessionContext` factory.
|
|
181
|
-
|
|
182
|
-
```javascript customSession.js icon=logos:javascript
|
|
183
|
-
import { createSessionContext } from '@arcblock/did-connect-react/lib/Session';
|
|
184
|
-
|
|
185
|
-
const { SessionProvider, SessionContext } = createSessionContext(
|
|
186
|
-
'my_app_session_token', // Custom storage key
|
|
187
|
-
'ls', // Use localStorage instead of cookies
|
|
188
|
-
{},
|
|
189
|
-
{
|
|
190
|
-
rolling: false, // Disable automatic token refresh
|
|
191
|
-
}
|
|
192
|
-
);
|
|
193
|
-
|
|
194
|
-
// ... export and use as needed
|
|
195
|
-
```
|
|
196
|
-
|
|
197
|
-
This allows you to fine-tune session behavior, such as changing the storage engine to `localStorage` ('ls') or `cookie`, or adjusting token refresh policies.
|
|
@@ -1,197 +0,0 @@
|
|
|
1
|
-
# Session 管理 (SessionProvider)
|
|
2
|
-
|
|
3
|
-
`SessionProvider` 是 `@arcblock/did-connect-react` 函式庫的基石。它是一個 React Context Provider,封裝了所有與 session 相關的邏輯、狀態和操作。透過用 `SessionProvider` 包裹您的應用程式,您可以將使用者的 session 資訊和身份驗證方法提供給應用程式樹中的任何元件,而無需手動向下傳遞 props。
|
|
4
|
-
|
|
5
|
-
該元件處理從儲存 session token 和管理使用者資料到協調登入/登出流程以及自動刷新 session 的所有事務。
|
|
6
|
-
|
|
7
|
-
## 運作原理
|
|
8
|
-
|
|
9
|
-
`SessionProvider` 建立一個 session context,其中包含目前使用者的狀態(`user` 物件、`loading` 狀態等),並提供修改該狀態的函式(例如,`login`、`logout`)。任何巢狀在 `SessionProvider` 內的元件都可以訂閱此 context 以存取這些資料並觸發身份驗證操作。
|
|
10
|
-
|
|
11
|
-
這是一個說明基本架構的圖表:
|
|
12
|
-
|
|
13
|
-
<!-- DIAGRAM_IMAGE_START:architecture:4:3:1765962229 -->
|
|
14
|
-

|
|
15
|
-
<!-- DIAGRAM_IMAGE_END -->
|
|
16
|
-
|
|
17
|
-
## 入門指南
|
|
18
|
-
|
|
19
|
-
要使用 session 管理功能,您首先需要匯入 `SessionProvider` 並用它包裹您的應用程式。該函式庫匯出工廠函式以建立一個已配置的 provider。
|
|
20
|
-
|
|
21
|
-
### 基本設定
|
|
22
|
-
|
|
23
|
-
設定 provider 最常見的方法是使用 `createAuthServiceSessionContext`,它已為在 ArcBlock 生態系統(Blocklets)內執行的應用程式預先配置。
|
|
24
|
-
|
|
25
|
-
```javascript App.js icon=logos:javascript
|
|
26
|
-
import React from 'react';
|
|
27
|
-
import { createAuthServiceSessionContext } from '@arcblock/did-connect-react/lib/Session';
|
|
28
|
-
import AuthButton from './AuthButton';
|
|
29
|
-
|
|
30
|
-
// 建立 SessionProvider 和一個用於存取 context 的 hook
|
|
31
|
-
const { SessionProvider, SessionContext } = createAuthServiceSessionContext();
|
|
32
|
-
|
|
33
|
-
export const useSession = () => React.useContext(SessionContext);
|
|
34
|
-
|
|
35
|
-
export default function App() {
|
|
36
|
-
return (
|
|
37
|
-
<SessionProvider>
|
|
38
|
-
<div className="App">
|
|
39
|
-
<h1>歡迎使用 DID Connect</h1>
|
|
40
|
-
<AuthButton />
|
|
41
|
-
</div>
|
|
42
|
-
</SessionProvider>
|
|
43
|
-
);
|
|
44
|
-
}
|
|
45
|
-
```
|
|
46
|
-
|
|
47
|
-
```javascript AuthButton.js icon=logos:javascript
|
|
48
|
-
import React from 'react';
|
|
49
|
-
import { useSession } from './App';
|
|
50
|
-
|
|
51
|
-
export default function AuthButton() {
|
|
52
|
-
const { session } = useSession();
|
|
53
|
-
|
|
54
|
-
if (session.loading) {
|
|
55
|
-
return <div>載入中...</div>;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
if (session.user) {
|
|
59
|
-
return (
|
|
60
|
-
<div>
|
|
61
|
-
<p>歡迎,{session.user.fullName || session.user.did}</p>
|
|
62
|
-
<button onClick={() => session.logout()}>登出</button>
|
|
63
|
-
</div>
|
|
64
|
-
);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
return <button onClick={() => session.login()}>登入</button>;
|
|
68
|
-
}
|
|
69
|
-
```
|
|
70
|
-
|
|
71
|
-
在此範例中,`createAuthServiceSessionContext` 產生一個 `SessionProvider`。我們用它包裹 `App`,現在任何子元件,例如 `AuthButton`,都可以使用 `useSession` hook 來存取 session 資料和函式。
|
|
72
|
-
|
|
73
|
-
## SessionProvider Props
|
|
74
|
-
|
|
75
|
-
您可以透過傳遞各種 props 來設定 `SessionProvider` 的行為。
|
|
76
|
-
|
|
77
|
-
<x-field-group>
|
|
78
|
-
<x-field data-name="serviceHost" data-type="string" data-desc="驗證服務後端的基礎 URL。預設為目前 Blocklet 的前綴或 '/'。"></x-field>
|
|
79
|
-
<x-field data-name="autoConnect" data-type="boolean" data-default="false" data-desc="若為 true,如果在頁面載入時未找到使用者 session,DID Connect 對話方塊將自動開啟。"></x-field>
|
|
80
|
-
<x-field data-name="autoDisconnect" data-type="boolean" data-default="true" data-desc="若為 true,如果應用程式 ID 與 session cookie 中儲存的 ID 不符,session 將被自動清除。"></x-field>
|
|
81
|
-
<x-field data-name="protectedRoutes" data-type="string[]" data-default='["*"]' data-desc="一個用於保護路由模式的陣列。如果未經身份驗證的使用者嘗試存取這些路由,他們將看到一個載入指示器,直到 session 解析完成。支援萬用字元(*)。"></x-field>
|
|
82
|
-
<x-field data-name="locale" data-type="string" data-desc="設定 DID Connect UI 的顯示語言。預設為瀏覽器的語言。"></x-field>
|
|
83
|
-
<x-field data-name="webWalletUrl" data-type="string" data-desc="用於連接過程的網頁版 DID Wallet 的 URL。"></x-field>
|
|
84
|
-
<x-field data-name="timeout" data-type="number" data-default="120000" data-desc="在連接過程中等待 DID Wallet 回應的逾時時間(毫秒)。"></x-field>
|
|
85
|
-
<x-field data-name="useSocket" data-type="boolean" data-default="true" data-desc="決定是否使用 WebSocket 與錢包進行即時通訊,以提供更快的用戶體驗。"></x-field>
|
|
86
|
-
<x-field data-name="extraParams" data-type="object" data-desc="一個包含額外參數的物件,這些參數將隨每次驗證請求發送到後端。"></x-field>
|
|
87
|
-
</x-field-group>
|
|
88
|
-
|
|
89
|
-
## Session Context 物件
|
|
90
|
-
|
|
91
|
-
當您使用 `SessionContext` 時,您會得到一個包含 session 狀態和 API 的物件。主要屬性是 `session`,它包含您將與之互動的所有資料和方法。
|
|
92
|
-
|
|
93
|
-
### 狀態屬性
|
|
94
|
-
|
|
95
|
-
這些是唯讀屬性,描述了使用者 session 的目前狀態。
|
|
96
|
-
|
|
97
|
-
<x-field-group>
|
|
98
|
-
<x-field data-name="user" data-type="User | null" data-desc="如果 session 存在,則為經過身份驗證的使用者物件,否則為 null。包含 DID、姓名、電子郵件、頭像等詳細資訊。"></x-field>
|
|
99
|
-
<x-field data-name="loading" data-type="boolean" data-desc="當 session 正在初始化、刷新或在登入/登出過程中為 true。"></x-field>
|
|
100
|
-
<x-field data-name="initialized" data-type="boolean" data-desc="頁面載入時,初始 session 檢查完成後為 true。"></x-field>
|
|
101
|
-
<x-field data-name="open" data-type="boolean" data-desc="如果 DID Connect 對話方塊目前處於開啟狀態,則為 true。"></x-field>
|
|
102
|
-
<x-field data-name="error" data-type="string" data-desc="如果上一次 session 操作失敗,則包含錯誤訊息。"></x-field>
|
|
103
|
-
<x-field data-name="provider" data-type="string" data-desc="上次登入使用的提供者(例如 'wallet'、'github'、'google')。"></x-field>
|
|
104
|
-
<x-field data-name="walletOS" data-type="string" data-desc="已連接的 DID Wallet 的作業系統(例如 'ios'、'android'、'web')。"></x-field>
|
|
105
|
-
<x-field data-name="unReadCount" data-type="number" data-desc="目前使用者的未讀通知數量。"></x-field>
|
|
106
|
-
</x-field-group>
|
|
107
|
-
|
|
108
|
-
### 操作方法
|
|
109
|
-
|
|
110
|
-
這些是您可以呼叫以啟動身份驗證流程或管理 session 的函式。
|
|
111
|
-
|
|
112
|
-
<x-field-group>
|
|
113
|
-
<x-field data-name="login()" data-type="function">
|
|
114
|
-
<x-field-desc markdown>透過開啟 DID Connect UI 來啟動使用者登入流程。它接受可選的回呼函式和參數。</x-field-desc>
|
|
115
|
-
</x-field>
|
|
116
|
-
<x-field data-name="logout()" data-type="function">
|
|
117
|
-
<x-field-desc markdown>將目前使用者登出,並從儲存空間中清除 session。它接受一個可選的回呼函式,在完成後執行。</x-field-desc>
|
|
118
|
-
</x-field>
|
|
119
|
-
<x-field data-name="switchDid()" data-type="function">
|
|
120
|
-
<x-field-desc markdown>允許已登入的使用者切換到不同的 DID 帳戶。它會重新開啟 DID Connect UI,並提供切換帳戶的上下文。</x-field-desc>
|
|
121
|
-
</x-field>
|
|
122
|
-
<x-field data-name="bindWallet()" data-type="function">
|
|
123
|
-
<x-field-desc markdown>對於透過社群提供者(OAuth)或 Passkeys 登入的使用者,此函式會啟動一個流程,將 DID Wallet 連接到他們現有的帳戶。</x-field-desc>
|
|
124
|
-
</x-field>
|
|
125
|
-
<x-field data-name="refresh()" data-type="function">
|
|
126
|
-
<x-field-desc markdown>手動觸發從伺服器刷新使用者 session 資料。適用於獲取更新的個人資料資訊或權限。</x-field-desc>
|
|
127
|
-
</x-field>
|
|
128
|
-
<x-field data-name="switchProfile()" data-type="function">
|
|
129
|
-
<x-field-desc markdown>開啟 DID Connect UI,允許使用者在同一個 DID 帳戶內切換不同的個人資料(例如,個人、工作)。</x-field-desc>
|
|
130
|
-
</x-field>
|
|
131
|
-
<x-field data-name="switchPassport()" data-type="function">
|
|
132
|
-
<x-field-desc markdown>開啟 DID Connect UI,允許使用者在不同的通行證之間切換,這些通行證可能代表不同的憑證集或角色。</x-field-desc>
|
|
133
|
-
</x-field>
|
|
134
|
-
<x-field data-name="connectToDidSpaceForFullAccess()" data-type="function">
|
|
135
|
-
<x-field-desc markdown>啟動與使用者 DID Space 的連接,請求完全存取權限。這通常是需要讀取和寫入使用者個人資料儲存庫的應用程式所必需的。</x-field-desc>
|
|
136
|
-
</x-field>
|
|
137
|
-
<x-field data-name="withSecondaryAuth()" data-type="function">
|
|
138
|
-
<x-field-desc markdown>一個高階函式,它包裹另一個函式,要求使用者在執行被包裹的函式之前完成二次身份驗證步驟(例如,重新輸入密碼、使用 passkey)。這是保護敏感操作的理想選擇。</x-field-desc>
|
|
139
|
-
</x-field>
|
|
140
|
-
</x-field-group>
|
|
141
|
-
|
|
142
|
-
### 事件訂閱
|
|
143
|
-
|
|
144
|
-
context 還提供一個 `events` 物件,它是 `EventEmitter3` 的一個實例。您可以使用它來訂閱 session 的各種生命週期事件。
|
|
145
|
-
|
|
146
|
-
```javascript EventListener.js icon=logos:javascript
|
|
147
|
-
import React, { useEffect } from 'react';
|
|
148
|
-
import { useSession } from './App';
|
|
149
|
-
|
|
150
|
-
function EventListener() {
|
|
151
|
-
const { events } = useSession();
|
|
152
|
-
|
|
153
|
-
useEffect(() => {
|
|
154
|
-
const handleLogin = (result) => {
|
|
155
|
-
console.log('使用者已登入:', result.user.did);
|
|
156
|
-
};
|
|
157
|
-
|
|
158
|
-
const handleLogout = () => {
|
|
159
|
-
console.log('使用者已登出');
|
|
160
|
-
};
|
|
161
|
-
|
|
162
|
-
events.on('login', handleLogin);
|
|
163
|
-
events.on('logout', handleLogout);
|
|
164
|
-
|
|
165
|
-
// 在元件卸載時清除監聽器
|
|
166
|
-
return () => {
|
|
167
|
-
events.off('login', handleLogin);
|
|
168
|
-
events.off('logout', handleLogout);
|
|
169
|
-
};
|
|
170
|
-
}, [events]);
|
|
171
|
-
|
|
172
|
-
return null; // 此元件不渲染任何內容
|
|
173
|
-
}
|
|
174
|
-
```
|
|
175
|
-
|
|
176
|
-
可用的事件包括 `login`、`login-failed`、`logout`、`change`、`bind-wallet`、`switch-passport` 等。
|
|
177
|
-
|
|
178
|
-
## 進階自訂
|
|
179
|
-
|
|
180
|
-
對於需要不同儲存機制或更精細控制的場景,您可以使用通用的 `createSessionContext` 工廠函式。
|
|
181
|
-
|
|
182
|
-
```javascript customSession.js icon=logos:javascript
|
|
183
|
-
import { createSessionContext } from '@arcblock/did-connect-react/lib/Session';
|
|
184
|
-
|
|
185
|
-
const { SessionProvider, SessionContext } = createSessionContext(
|
|
186
|
-
'my_app_session_token', // 自訂儲存金鑰
|
|
187
|
-
'ls', // 使用 localStorage 而非 cookie
|
|
188
|
-
{},
|
|
189
|
-
{
|
|
190
|
-
rolling: false, // 停用自動 token 刷新
|
|
191
|
-
}
|
|
192
|
-
);
|
|
193
|
-
|
|
194
|
-
// ... 根據需要匯出和使用
|
|
195
|
-
```
|
|
196
|
-
|
|
197
|
-
這允許您微調 session 行為,例如將儲存引擎更改為 `localStorage`('ls')或 `cookie`,或調整 token 刷新策略。
|