@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,239 @@
|
|
|
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
|
+
```d2 SessionProvider Architecture icon=mdi:react
|
|
14
|
+
direction: down
|
|
15
|
+
|
|
16
|
+
SessionProvider: {
|
|
17
|
+
label: "SessionProvider"
|
|
18
|
+
shape: rectangle
|
|
19
|
+
style: {
|
|
20
|
+
fill: "#f0f9ff"
|
|
21
|
+
stroke: "#0284c7"
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
Session-Context: {
|
|
25
|
+
label: "セッションコンテキスト\n{ user, login(), logout() ... }"
|
|
26
|
+
shape: cylinder
|
|
27
|
+
style.fill: "#ecfdf5"
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
Your-App: {
|
|
31
|
+
label: "あなたのアプリケーション"
|
|
32
|
+
shape: rectangle
|
|
33
|
+
|
|
34
|
+
Header: {
|
|
35
|
+
label: "ヘッダーコンポーネント"
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
ProfilePage: {
|
|
39
|
+
label: "プロフィールページ"
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
LoginButton: {
|
|
43
|
+
label: "ログインボタン"
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
Session-Context -> Your-App: 提供
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
Your-App.Header: 利用
|
|
51
|
+
Your-App.ProfilePage: 利用
|
|
52
|
+
Your-App.LoginButton: 利用とアクション
|
|
53
|
+
|
|
54
|
+
Your-App.Header -> Session-Context
|
|
55
|
+
Your-App.ProfilePage -> Session-Context
|
|
56
|
+
Your-App.LoginButton -> Session-Context
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## はじめに
|
|
60
|
+
|
|
61
|
+
セッション管理機能を使用するには、まず`SessionProvider`をインポートしてアプリケーションをラップする必要があります。このライブラリは、設定済みのプロバイダーを作成するためのファクトリー関数をエクスポートします。
|
|
62
|
+
|
|
63
|
+
### 基本的なセットアップ
|
|
64
|
+
|
|
65
|
+
プロバイダーをセットアップする最も一般的な方法は、`createAuthServiceSessionContext`を使用することです。これは、ArcBlockエコシステム(Blocklets)内で実行されるアプリケーション向けに事前設定されています。
|
|
66
|
+
|
|
67
|
+
```javascript App.js icon=logos:javascript
|
|
68
|
+
import React from 'react';
|
|
69
|
+
import { createAuthServiceSessionContext } from '@arcblock/did-connect-react/lib/Session';
|
|
70
|
+
import AuthButton from './AuthButton';
|
|
71
|
+
|
|
72
|
+
// SessionProviderとコンテキストにアクセスするためのフックを作成
|
|
73
|
+
const { SessionProvider, SessionContext } = createAuthServiceSessionContext();
|
|
74
|
+
|
|
75
|
+
export const useSession = () => React.useContext(SessionContext);
|
|
76
|
+
|
|
77
|
+
export default function App() {
|
|
78
|
+
return (
|
|
79
|
+
<SessionProvider>
|
|
80
|
+
<div className="App">
|
|
81
|
+
<h1>Welcome to DID Connect</h1>
|
|
82
|
+
<AuthButton />
|
|
83
|
+
</div>
|
|
84
|
+
</SessionProvider>
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
```javascript AuthButton.js icon=logos:javascript
|
|
90
|
+
import React from 'react';
|
|
91
|
+
import { useSession } from './App';
|
|
92
|
+
|
|
93
|
+
export default function AuthButton() {
|
|
94
|
+
const { session } = useSession();
|
|
95
|
+
|
|
96
|
+
if (session.loading) {
|
|
97
|
+
return <div>Loading...</div>;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (session.user) {
|
|
101
|
+
return (
|
|
102
|
+
<div>
|
|
103
|
+
<p>Welcome, {session.user.fullName || session.user.did}</p>
|
|
104
|
+
<button onClick={() => session.logout()}>Logout</button>
|
|
105
|
+
</div>
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return <button onClick={() => session.login()}>Login</button>;
|
|
110
|
+
}
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
この例では、`createAuthServiceSessionContext`が`SessionProvider`を生成します。`App`をそれでラップすると、`AuthButton`のような子コンポーネントは、`useSession`フックを使用してセッションデータと関数にアクセスできるようになります。
|
|
114
|
+
|
|
115
|
+
## SessionProviderのProps
|
|
116
|
+
|
|
117
|
+
`SessionProvider`の動作は、さまざまなpropsを渡すことで設定できます。
|
|
118
|
+
|
|
119
|
+
<x-field-group>
|
|
120
|
+
<x-field data-name="serviceHost" data-type="string" data-desc="認証サービスバックエンドのベースURL。デフォルトは現在のBlockletのプレフィックスまたは'/'です。"></x-field>
|
|
121
|
+
<x-field data-name="autoConnect" data-type="boolean" data-default="false" data-desc="trueの場合、ページロード時にユーザーセッションが見つからないと、DID Connectダイアログが自動的に開きます。"></x-field>
|
|
122
|
+
<x-field data-name="autoDisconnect" data-type="boolean" data-default="true" data-desc="trueの場合、アプリケーションIDがセッションクッキーに保存されているものと一致しない場合、セッションは自動的にクリアされます。"></x-field>
|
|
123
|
+
<x-field data-name="protectedRoutes" data-type="string[]" data-default='["*"]' data-desc="保護するルートパターンの配列。未認証のユーザーがこれらのルートにアクセスしようとすると、セッションが解決されるまで読み込みスピナーが表示されます。ワイルドカード(*)がサポートされています。"></x-field>
|
|
124
|
+
<x-field data-name="locale" data-type="string" data-desc="DID Connect UIの表示言語を設定します。デフォルトはブラウザの言語です。"></x-field>
|
|
125
|
+
<x-field data-name="webWalletUrl" data-type="string" data-desc="接続プロセスで使用するウェブベースのDID WalletのURL。"></x-field>
|
|
126
|
+
<x-field data-name="timeout" data-type="number" data-default="120000" data-desc="接続プロセス中にDID Walletからの応答を待つためのタイムアウト(ミリ秒単位)。"></x-field>
|
|
127
|
+
<x-field data-name="useSocket" data-type="boolean" data-default="true" data-desc="ウォレットとのリアルタイム通信にWebSocketを使用するかどうかを決定し、より高速なユーザーエクスペリエンスを提供します。"></x-field>
|
|
128
|
+
<x-field data-name="extraParams" data-type="object" data-desc="バックエンドへのすべての認証リクエストとともに送信される追加パラメーターのオブジェクト。"></x-field>
|
|
129
|
+
</x-field-group>
|
|
130
|
+
|
|
131
|
+
## セッションコンテキストオブジェクト
|
|
132
|
+
|
|
133
|
+
`SessionContext`を利用すると、セッションの状態とAPIを含むオブジェクトを取得できます。主要なプロパティは`session`で、これには対話するすべてのデータとメソッドが含まれています。
|
|
134
|
+
|
|
135
|
+
### 状態プロパティ
|
|
136
|
+
|
|
137
|
+
これらは、ユーザーセッションの現在の状態を記述する読み取り専用のプロパティです。
|
|
138
|
+
|
|
139
|
+
<x-field-group>
|
|
140
|
+
<x-field data-name="user" data-type="User | null" data-desc="セッションが存在する場合、認証されたユーザーオブジェクト。それ以外の場合はnull。DID、名前、メール、アバターなどの詳細が含まれます。"></x-field>
|
|
141
|
+
<x-field data-name="loading" data-type="boolean" data-desc="セッションが初期化中、更新中、またはログイン/ログアウトプロセス中である場合はtrue。"></x-field>
|
|
142
|
+
<x-field data-name="initialized" data-type="boolean" data-desc="ページロード時に最初のセッションチェックが完了するとtrueになります。"></x-field>
|
|
143
|
+
<x-field data-name="open" data-type="boolean" data-desc="DID Connectダイアログが現在開いている場合はtrue。"></x-field>
|
|
144
|
+
<x-field data-name="error" data-type="string" data-desc="最後のセッション操作が失敗した場合、エラーメッセージが含まれます。"></x-field>
|
|
145
|
+
<x-field data-name="provider" data-type="string" data-desc="最後のログインに使用されたプロバイダー(例:'wallet'、'github'、'google')。"></x-field>
|
|
146
|
+
<x-field data-name="walletOS" data-type="string" data-desc="接続されたDID Walletのオペレーティングシステム(例:'ios'、'android'、'web')。"></x-field>
|
|
147
|
+
<x-field data-name="unReadCount" data-type="number" data-desc="現在のユーザーの未読通知の数。"></x-field>
|
|
148
|
+
</x-field-group>
|
|
149
|
+
|
|
150
|
+
### アクションメソッド
|
|
151
|
+
|
|
152
|
+
これらは、認証フローを開始したり、セッションを管理したりするために呼び出すことができる関数です。
|
|
153
|
+
|
|
154
|
+
<x-field-group>
|
|
155
|
+
<x-field data-name="login()" data-type="function">
|
|
156
|
+
<x-field-desc markdown>DID Connect UIを開いてユーザーのログインプロセスを開始します。オプションのコールバックとパラメーターを受け入れます。</x-field-desc>
|
|
157
|
+
</x-field>
|
|
158
|
+
<x-field data-name="logout()" data-type="function">
|
|
159
|
+
<x-field-desc markdown>現在のユーザーをログアウトさせ、ストレージからセッションをクリアします。完了時に実行されるオプションのコールバック関数を受け入れます。</x-field-desc>
|
|
160
|
+
</x-field>
|
|
161
|
+
<x-field data-name="switchDid()" data-type="function">
|
|
162
|
+
<x-field-desc markdown>ログイン中のユーザーが別のアカウントに切り替えることを許可します。アカウント切り替え用のコンテキストでDID Connect UIを再度開きます。</x-field-desc>
|
|
163
|
+
</x-field>
|
|
164
|
+
<x-field data-name="bindWallet()" data-type="function">
|
|
165
|
+
<x-field-desc markdown>ソーシャルプロバイダー(OAuth)またはパスキー経由でログインしたユーザーのために、この関数は既存のアカウントにDID Walletを接続するフローを開始します。</x-field-desc>
|
|
166
|
+
</x-field>
|
|
167
|
+
<x-field data-name="refresh()" data-type="function">
|
|
168
|
+
<x-field-desc markdown>サーバーからユーザーのセッションデータを手動で更新します。更新されたプロフィール情報や権限を取得するのに便利です。</x-field-desc>
|
|
169
|
+
</x-field>
|
|
170
|
+
<x-field data-name="switchProfile()" data-type="function">
|
|
171
|
+
<x-field-desc markdown>DID Connect UIを開き、ユーザーが同じDIDアカウント内の異なるプロフィール(例:個人用、仕事用)を切り替えられるようにします。</x-field-desc>
|
|
172
|
+
</x-field>
|
|
173
|
+
<x-field data-name="switchPassport()" data-type="function">
|
|
174
|
+
<x-field-desc markdown>DID Connect UIを開き、ユーザーが異なるパスポート(異なる資格情報や役割のセットを表す場合がある)を切り替えられるようにします。</x-field-desc>
|
|
175
|
+
</x-field>
|
|
176
|
+
<x-field data-name="connectToDidSpaceForFullAccess()" data-type="function">
|
|
177
|
+
<x-field-desc markdown>ユーザーのDID Spaceへの接続を開始し、フルアクセス権限を要求します。これは、ユーザーの個人データストアにデータを読み書きする必要があるアプリケーションでしばしば必要とされます。</x-field-desc>
|
|
178
|
+
</x-field>
|
|
179
|
+
<x-field data-name="withSecondaryAuth()" data-type="function">
|
|
180
|
+
<x-field-desc markdown>別の関数をラップする高階関数で、ラップされた関数が実行される前に、ユーザーが二次認証ステップ(例:パスワードの再入力、パスキーの使用)を完了する必要があります。これは機密性の高いアクションを保護するのに理想的です。</x-field-desc>
|
|
181
|
+
</x-field>
|
|
182
|
+
</x-field-group>
|
|
183
|
+
|
|
184
|
+
### イベントの購読
|
|
185
|
+
|
|
186
|
+
コンテキストは`events`オブジェクトも提供します。これは`EventEmitter3`のインスタンスです。これを使用して、セッションのさまざまなライフサイクルイベントを購読できます。
|
|
187
|
+
|
|
188
|
+
```javascript EventListener.js icon=logos:javascript
|
|
189
|
+
import React, { useEffect } from 'react';
|
|
190
|
+
import { useSession } from './App';
|
|
191
|
+
|
|
192
|
+
function EventListener() {
|
|
193
|
+
const { events } = useSession();
|
|
194
|
+
|
|
195
|
+
useEffect(() => {
|
|
196
|
+
const handleLogin = (result) => {
|
|
197
|
+
console.log('User logged in:', result.user.did);
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
const handleLogout = () => {
|
|
201
|
+
console.log('User logged out');
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
events.on('login', handleLogin);
|
|
205
|
+
events.on('logout', handleLogout);
|
|
206
|
+
|
|
207
|
+
// コンポーネントのアンマウント時にリスナーをクリーンアップ
|
|
208
|
+
return () => {
|
|
209
|
+
events.off('login', handleLogin);
|
|
210
|
+
events.off('logout', handleLogout);
|
|
211
|
+
};
|
|
212
|
+
}, [events]);
|
|
213
|
+
|
|
214
|
+
return null; // このコンポーネントは何もレンダリングしません
|
|
215
|
+
}
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
利用可能なイベントには、`login`、`login-failed`、`logout`、`change`、`bind-wallet`、`switch-passport`などがあります。
|
|
219
|
+
|
|
220
|
+
## 高度なカスタマイズ
|
|
221
|
+
|
|
222
|
+
異なるストレージメカニズムやより詳細な制御が必要なシナリオでは、汎用の`createSessionContext`ファクトリーを使用できます。
|
|
223
|
+
|
|
224
|
+
```javascript customSession.js icon=logos:javascript
|
|
225
|
+
import { createSessionContext } from '@arcblock/did-connect-react/lib/Session';
|
|
226
|
+
|
|
227
|
+
const { SessionProvider, SessionContext } = createSessionContext(
|
|
228
|
+
'my_app_session_token', // カスタムストレージキー
|
|
229
|
+
'ls', // cookieの代わりにlocalStorageを使用
|
|
230
|
+
{},
|
|
231
|
+
{
|
|
232
|
+
rolling: false, // 自動トークン更新を無効化
|
|
233
|
+
}
|
|
234
|
+
);
|
|
235
|
+
|
|
236
|
+
// ... 必要に応じてエクスポートして使用
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
これにより、ストレージエンジンを`localStorage`('ls')や`cookie`に変更したり、トークン更新ポリシーを調整したりするなど、セッションの動作を微調整できます。
|
|
@@ -0,0 +1,239 @@
|
|
|
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
|
+
```d2 SessionProvider Architecture icon=mdi:react
|
|
14
|
+
direction: down
|
|
15
|
+
|
|
16
|
+
SessionProvider: {
|
|
17
|
+
label: "SessionProvider"
|
|
18
|
+
shape: rectangle
|
|
19
|
+
style: {
|
|
20
|
+
fill: "#f0f9ff"
|
|
21
|
+
stroke: "#0284c7"
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
Session-Context: {
|
|
25
|
+
label: "Session Context\n{ user, login(), logout() ... }"
|
|
26
|
+
shape: cylinder
|
|
27
|
+
style.fill: "#ecfdf5"
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
Your-App: {
|
|
31
|
+
label: "Your Application"
|
|
32
|
+
shape: rectangle
|
|
33
|
+
|
|
34
|
+
Header: {
|
|
35
|
+
label: "Header Component"
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
ProfilePage: {
|
|
39
|
+
label: "Profile Page"
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
LoginButton: {
|
|
43
|
+
label: "Login Button"
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
Session-Context -> Your-App: Provides
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
Your-App.Header: Consumes
|
|
51
|
+
Your-App.ProfilePage: Consumes
|
|
52
|
+
Your-App.LoginButton: Consumes & Actions
|
|
53
|
+
|
|
54
|
+
Your-App.Header -> Session-Context
|
|
55
|
+
Your-App.ProfilePage -> Session-Context
|
|
56
|
+
Your-App.LoginButton -> Session-Context
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## Getting Started
|
|
60
|
+
|
|
61
|
+
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.
|
|
62
|
+
|
|
63
|
+
### Basic Setup
|
|
64
|
+
|
|
65
|
+
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).
|
|
66
|
+
|
|
67
|
+
```javascript App.js icon=logos:javascript
|
|
68
|
+
import React from 'react';
|
|
69
|
+
import { createAuthServiceSessionContext } from '@arcblock/did-connect-react/lib/Session';
|
|
70
|
+
import AuthButton from './AuthButton';
|
|
71
|
+
|
|
72
|
+
// Create the SessionProvider and a hook to access the context
|
|
73
|
+
const { SessionProvider, SessionContext } = createAuthServiceSessionContext();
|
|
74
|
+
|
|
75
|
+
export const useSession = () => React.useContext(SessionContext);
|
|
76
|
+
|
|
77
|
+
export default function App() {
|
|
78
|
+
return (
|
|
79
|
+
<SessionProvider>
|
|
80
|
+
<div className="App">
|
|
81
|
+
<h1>Welcome to DID Connect</h1>
|
|
82
|
+
<AuthButton />
|
|
83
|
+
</div>
|
|
84
|
+
</SessionProvider>
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
```javascript AuthButton.js icon=logos:javascript
|
|
90
|
+
import React from 'react';
|
|
91
|
+
import { useSession } from './App';
|
|
92
|
+
|
|
93
|
+
export default function AuthButton() {
|
|
94
|
+
const { session } = useSession();
|
|
95
|
+
|
|
96
|
+
if (session.loading) {
|
|
97
|
+
return <div>Loading...</div>;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (session.user) {
|
|
101
|
+
return (
|
|
102
|
+
<div>
|
|
103
|
+
<p>Welcome, {session.user.fullName || session.user.did}</p>
|
|
104
|
+
<button onClick={() => session.logout()}>Logout</button>
|
|
105
|
+
</div>
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return <button onClick={() => session.login()}>Login</button>;
|
|
110
|
+
}
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
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.
|
|
114
|
+
|
|
115
|
+
## SessionProvider Props
|
|
116
|
+
|
|
117
|
+
You can configure the `SessionProvider`'s behavior by passing it various props.
|
|
118
|
+
|
|
119
|
+
<x-field-group>
|
|
120
|
+
<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>
|
|
121
|
+
<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>
|
|
122
|
+
<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>
|
|
123
|
+
<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>
|
|
124
|
+
<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>
|
|
125
|
+
<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>
|
|
126
|
+
<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>
|
|
127
|
+
<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>
|
|
128
|
+
<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>
|
|
129
|
+
</x-field-group>
|
|
130
|
+
|
|
131
|
+
## The Session Context Object
|
|
132
|
+
|
|
133
|
+
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.
|
|
134
|
+
|
|
135
|
+
### State Properties
|
|
136
|
+
|
|
137
|
+
These are read-only properties that describe the current state of the user session.
|
|
138
|
+
|
|
139
|
+
<x-field-group>
|
|
140
|
+
<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>
|
|
141
|
+
<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>
|
|
142
|
+
<x-field data-name="initialized" data-type="boolean" data-desc="True once the initial session check has completed on page load."></x-field>
|
|
143
|
+
<x-field data-name="open" data-type="boolean" data-desc="True if the DID Connect dialog is currently open."></x-field>
|
|
144
|
+
<x-field data-name="error" data-type="string" data-desc="Contains an error message if the last session operation failed."></x-field>
|
|
145
|
+
<x-field data-name="provider" data-type="string" data-desc="The provider used for the last login (e.g., 'wallet', 'github', 'google')."></x-field>
|
|
146
|
+
<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>
|
|
147
|
+
<x-field data-name="unReadCount" data-type="number" data-desc="The number of unread notifications for the current user."></x-field>
|
|
148
|
+
</x-field-group>
|
|
149
|
+
|
|
150
|
+
### Action Methods
|
|
151
|
+
|
|
152
|
+
These are functions you can call to initiate authentication flows or manage the session.
|
|
153
|
+
|
|
154
|
+
<x-field-group>
|
|
155
|
+
<x-field data-name="login()" data-type="function">
|
|
156
|
+
<x-field-desc markdown>Initiates the user login process by opening the DID Connect UI. It accepts optional callbacks and parameters.</x-field-desc>
|
|
157
|
+
</x-field>
|
|
158
|
+
<x-field data-name="logout()" data-type="function">
|
|
159
|
+
<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>
|
|
160
|
+
</x-field>
|
|
161
|
+
<x-field data-name="switchDid()" data-type="function">
|
|
162
|
+
<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>
|
|
163
|
+
</x-field>
|
|
164
|
+
<x-field data-name="bindWallet()" data-type="function">
|
|
165
|
+
<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>
|
|
166
|
+
</x-field>
|
|
167
|
+
<x-field data-name="refresh()" data-type="function">
|
|
168
|
+
<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>
|
|
169
|
+
</x-field>
|
|
170
|
+
<x-field data-name="switchProfile()" data-type="function">
|
|
171
|
+
<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>
|
|
172
|
+
</x-field>
|
|
173
|
+
<x-field data-name="switchPassport()" data-type="function">
|
|
174
|
+
<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>
|
|
175
|
+
</x-field>
|
|
176
|
+
<x-field data-name="connectToDidSpaceForFullAccess()" data-type="function">
|
|
177
|
+
<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>
|
|
178
|
+
</x-field>
|
|
179
|
+
<x-field data-name="withSecondaryAuth()" data-type="function">
|
|
180
|
+
<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>
|
|
181
|
+
</x-field>
|
|
182
|
+
</x-field-group>
|
|
183
|
+
|
|
184
|
+
### Event Subscription
|
|
185
|
+
|
|
186
|
+
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.
|
|
187
|
+
|
|
188
|
+
```javascript EventListener.js icon=logos:javascript
|
|
189
|
+
import React, { useEffect } from 'react';
|
|
190
|
+
import { useSession } from './App';
|
|
191
|
+
|
|
192
|
+
function EventListener() {
|
|
193
|
+
const { events } = useSession();
|
|
194
|
+
|
|
195
|
+
useEffect(() => {
|
|
196
|
+
const handleLogin = (result) => {
|
|
197
|
+
console.log('User logged in:', result.user.did);
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
const handleLogout = () => {
|
|
201
|
+
console.log('User logged out');
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
events.on('login', handleLogin);
|
|
205
|
+
events.on('logout', handleLogout);
|
|
206
|
+
|
|
207
|
+
// Cleanup listeners on component unmount
|
|
208
|
+
return () => {
|
|
209
|
+
events.off('login', handleLogin);
|
|
210
|
+
events.off('logout', handleLogout);
|
|
211
|
+
};
|
|
212
|
+
}, [events]);
|
|
213
|
+
|
|
214
|
+
return null; // This component doesn't render anything
|
|
215
|
+
}
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
Available events include `login`, `login-failed`, `logout`, `change`, `bind-wallet`, `switch-passport`, and more.
|
|
219
|
+
|
|
220
|
+
## Advanced Customization
|
|
221
|
+
|
|
222
|
+
For scenarios that require different storage mechanisms or more granular control, you can use the generic `createSessionContext` factory.
|
|
223
|
+
|
|
224
|
+
```javascript customSession.js icon=logos:javascript
|
|
225
|
+
import { createSessionContext } from '@arcblock/did-connect-react/lib/Session';
|
|
226
|
+
|
|
227
|
+
const { SessionProvider, SessionContext } = createSessionContext(
|
|
228
|
+
'my_app_session_token', // Custom storage key
|
|
229
|
+
'ls', // Use localStorage instead of cookies
|
|
230
|
+
{},
|
|
231
|
+
{
|
|
232
|
+
rolling: false, // Disable automatic token refresh
|
|
233
|
+
}
|
|
234
|
+
);
|
|
235
|
+
|
|
236
|
+
// ... export and use as needed
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
This allows you to fine-tune session behavior, such as changing the storage engine to `localStorage` ('ls') or `cookie`, or adjusting token refresh policies.
|