shogun-button-react 1.3.5 → 1.3.9
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/README.md +113 -33
- package/dist/components/ShogunButton.d.ts +20 -3
- package/dist/components/ShogunButton.js +261 -113
- package/dist/connector.js +29 -11
- package/dist/index.d.ts +4 -0
- package/dist/index.js +5 -0
- package/dist/plugins/GunAdvancedPlugin.d.ts +79 -0
- package/dist/plugins/GunAdvancedPlugin.js +498 -0
- package/dist/types/connector-options.d.ts +8 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -8,7 +8,8 @@ A React component library for seamless integration of Shogun authentication into
|
|
|
8
8
|
- 🎨 Customizable UI components
|
|
9
9
|
- 🔒 Secure authentication flow
|
|
10
10
|
- 🌓 Dark mode support
|
|
11
|
-
- 🔌 Multiple authentication methods (Username/Password, MetaMask, WebAuthn)
|
|
11
|
+
- 🔌 Multiple authentication methods (Username/Password, MetaMask, WebAuthn, Nostr, OAuth)
|
|
12
|
+
- 🔑 Account backup and recovery (Gun pair export/import)
|
|
12
13
|
- 📱 Responsive design
|
|
13
14
|
- 🌍 TypeScript support
|
|
14
15
|
|
|
@@ -20,8 +21,8 @@ import {
|
|
|
20
21
|
ShogunButton,
|
|
21
22
|
ShogunButtonProvider,
|
|
22
23
|
shogunConnector,
|
|
23
|
-
} from "
|
|
24
|
-
import "
|
|
24
|
+
} from "shogun-button-react";
|
|
25
|
+
import "shogun-button-react/styles.css";
|
|
25
26
|
|
|
26
27
|
function App() {
|
|
27
28
|
const { sdk, options, setProvider } = shogunConnector({
|
|
@@ -66,62 +67,113 @@ The provider component that supplies Shogun context to your application.
|
|
|
66
67
|
|
|
67
68
|
| Name | Type | Description |
|
|
68
69
|
| --------------- | ------------------------ | ---------------------------------------------- |
|
|
69
|
-
| sdk |
|
|
70
|
+
| sdk | ShogunCore | Shogun SDK instance created by shogunConnector |
|
|
70
71
|
| options | Object | Configuration options |
|
|
71
|
-
| onLoginSuccess | (data:
|
|
72
|
-
| onSignupSuccess | (data:
|
|
72
|
+
| onLoginSuccess | (data: { userPub: string; username: string; password?: string; authMethod?: string }) => void | Callback fired on successful login |
|
|
73
|
+
| onSignupSuccess | (data: { userPub: string; username: string; password?: string; authMethod?: string }) => void | Callback fired on successful signup |
|
|
73
74
|
| onError | (error: Error) => void | Callback fired when an error occurs |
|
|
74
75
|
|
|
75
76
|
### ShogunButton
|
|
76
77
|
|
|
77
|
-
The main button component for triggering Shogun authentication.
|
|
78
|
-
|
|
79
|
-
#### Custom Button
|
|
80
|
-
|
|
81
|
-
You can customize the button appearance using `ShogunButton.Custom`:
|
|
82
|
-
|
|
83
|
-
```tsx
|
|
84
|
-
<ShogunButton.Custom>
|
|
85
|
-
{({ ready, authenticate }) => (
|
|
86
|
-
<button
|
|
87
|
-
className="my-custom-button"
|
|
88
|
-
disabled={!ready}
|
|
89
|
-
onClick={authenticate}
|
|
90
|
-
>
|
|
91
|
-
Connect with Shogun
|
|
92
|
-
</button>
|
|
93
|
-
)}
|
|
94
|
-
</ShogunButton.Custom>
|
|
95
|
-
```
|
|
78
|
+
The main button component for triggering Shogun authentication. The component provides a complete authentication UI with modal dialogs for login and signup.
|
|
96
79
|
|
|
97
80
|
### useShogun Hook
|
|
98
81
|
|
|
99
82
|
A hook to access Shogun authentication state and functions.
|
|
100
83
|
|
|
101
84
|
```tsx
|
|
102
|
-
import {
|
|
85
|
+
import React, { useEffect } from "react";
|
|
86
|
+
import { useShogun } from "shogun-button-react";
|
|
103
87
|
|
|
104
88
|
function Profile() {
|
|
105
89
|
const {
|
|
106
|
-
|
|
107
|
-
|
|
90
|
+
isLoggedIn,
|
|
91
|
+
userPub,
|
|
92
|
+
username,
|
|
108
93
|
login,
|
|
109
94
|
signup,
|
|
110
95
|
logout,
|
|
111
|
-
connectWithMetaMask,
|
|
112
|
-
connectWithWebAuthn,
|
|
113
96
|
setProvider,
|
|
97
|
+
hasPlugin,
|
|
98
|
+
getPlugin,
|
|
99
|
+
exportGunPair,
|
|
100
|
+
importGunPair,
|
|
101
|
+
observe,
|
|
114
102
|
} = useShogun();
|
|
115
103
|
|
|
104
|
+
const handleLogin = async () => {
|
|
105
|
+
// Login with username/password
|
|
106
|
+
await login("password", "username", "password");
|
|
107
|
+
|
|
108
|
+
// Or login with MetaMask
|
|
109
|
+
await login("web3");
|
|
110
|
+
|
|
111
|
+
// Or login with WebAuthn
|
|
112
|
+
await login("webauthn", "username");
|
|
113
|
+
|
|
114
|
+
// Or login with Nostr
|
|
115
|
+
await login("nostr");
|
|
116
|
+
|
|
117
|
+
// Or login with OAuth
|
|
118
|
+
await login("oauth", "google");
|
|
119
|
+
|
|
120
|
+
// Or login with Gun pair (for account recovery)
|
|
121
|
+
const pairData = { /* Gun pair object */ };
|
|
122
|
+
await login("pair", pairData);
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
const handleSignUp = async () => {
|
|
126
|
+
// Sign up with username/password
|
|
127
|
+
await signup("password", "newusername", "newpassword");
|
|
128
|
+
|
|
129
|
+
// Or sign up with other methods (similar to login)
|
|
130
|
+
await signup("web3");
|
|
131
|
+
await signup("webauthn", "newusername");
|
|
132
|
+
};
|
|
133
|
+
|
|
116
134
|
const switchToCustomNetwork = () => {
|
|
117
135
|
setProvider('https://my-custom-rpc.example.com');
|
|
118
136
|
};
|
|
119
137
|
|
|
120
|
-
|
|
138
|
+
const handleExportPair = async () => {
|
|
139
|
+
try {
|
|
140
|
+
const pairData = await exportGunPair("optional-encryption-password");
|
|
141
|
+
console.log("Exported pair:", pairData);
|
|
142
|
+
// Save this data securely for account recovery
|
|
143
|
+
} catch (error) {
|
|
144
|
+
console.error("Export failed:", error);
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
const handleImportPair = async () => {
|
|
149
|
+
try {
|
|
150
|
+
const success = await importGunPair(savedPairData, "optional-password");
|
|
151
|
+
if (success) {
|
|
152
|
+
console.log("Pair imported successfully");
|
|
153
|
+
}
|
|
154
|
+
} catch (error) {
|
|
155
|
+
console.error("Import failed:", error);
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
// Observe reactive data changes
|
|
160
|
+
useEffect(() => {
|
|
161
|
+
if (isLoggedIn) {
|
|
162
|
+
const subscription = observe<any>('user/profile').subscribe(data => {
|
|
163
|
+
console.log('Profile data updated:', data);
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
return () => subscription.unsubscribe();
|
|
167
|
+
}
|
|
168
|
+
}, [isLoggedIn, observe]);
|
|
169
|
+
|
|
170
|
+
return isLoggedIn ? (
|
|
121
171
|
<div>
|
|
122
|
-
<h2>Welcome, {
|
|
172
|
+
<h2>Welcome, {username}!</h2>
|
|
173
|
+
<p>User Public Key: {userPub}</p>
|
|
123
174
|
<button onClick={logout}>Logout</button>
|
|
124
175
|
<button onClick={switchToCustomNetwork}>Switch Network</button>
|
|
176
|
+
<button onClick={handleExportPair}>Export Account</button>
|
|
125
177
|
</div>
|
|
126
178
|
) : (
|
|
127
179
|
<div>Please login to continue</div>
|
|
@@ -135,17 +187,43 @@ The `shogunConnector` accepts the following options:
|
|
|
135
187
|
|
|
136
188
|
```typescript
|
|
137
189
|
interface ShogunConnectorOptions {
|
|
190
|
+
// App information
|
|
138
191
|
appName: string;
|
|
139
192
|
appDescription?: string;
|
|
140
193
|
appUrl?: string;
|
|
141
194
|
appIcon?: string;
|
|
195
|
+
|
|
196
|
+
// Feature toggles
|
|
142
197
|
showMetamask?: boolean;
|
|
143
198
|
showWebauthn?: boolean;
|
|
199
|
+
showNostr?: boolean;
|
|
200
|
+
showOauth?: boolean;
|
|
144
201
|
darkMode?: boolean;
|
|
202
|
+
|
|
203
|
+
// Network configuration
|
|
145
204
|
websocketSecure?: boolean;
|
|
146
|
-
didRegistryAddress?: string | null;
|
|
147
205
|
providerUrl?: string | null;
|
|
148
206
|
peers?: string[];
|
|
207
|
+
authToken?: string;
|
|
208
|
+
gunInstance?: IGunInstance<any>;
|
|
209
|
+
|
|
210
|
+
// Advanced options
|
|
211
|
+
logging?: {
|
|
212
|
+
enabled: boolean;
|
|
213
|
+
level: "error" | "warning" | "info" | "debug";
|
|
214
|
+
};
|
|
215
|
+
timeouts?: {
|
|
216
|
+
login?: number;
|
|
217
|
+
signup?: number;
|
|
218
|
+
operation?: number;
|
|
219
|
+
};
|
|
220
|
+
oauth?: {
|
|
221
|
+
providers: Record<string, {
|
|
222
|
+
clientId: string;
|
|
223
|
+
clientSecret?: string;
|
|
224
|
+
redirectUri?: string;
|
|
225
|
+
}>
|
|
226
|
+
}
|
|
149
227
|
}
|
|
150
228
|
```
|
|
151
229
|
|
|
@@ -157,6 +235,8 @@ interface ShogunConnectorResult {
|
|
|
157
235
|
options: ShogunConnectorOptions;
|
|
158
236
|
setProvider: (provider: string | EthersProvider) => boolean;
|
|
159
237
|
getCurrentProviderUrl: () => string | null;
|
|
238
|
+
registerPlugin: (plugin: any) => boolean;
|
|
239
|
+
hasPlugin: (name: string) => boolean;
|
|
160
240
|
}
|
|
161
241
|
```
|
|
162
242
|
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import React from "react";
|
|
2
2
|
import { ShogunCore } from "shogun-core";
|
|
3
3
|
import { Observable } from "rxjs";
|
|
4
|
+
import { GunAdvancedPlugin } from "../plugins/GunAdvancedPlugin";
|
|
4
5
|
import "../types/index.js";
|
|
5
6
|
import "../styles/index.css";
|
|
6
7
|
type ShogunContextType = {
|
|
7
|
-
|
|
8
|
+
core: ShogunCore | null;
|
|
8
9
|
options: any;
|
|
9
10
|
isLoggedIn: boolean;
|
|
10
11
|
userPub: string | null;
|
|
@@ -18,11 +19,27 @@ type ShogunContextType = {
|
|
|
18
19
|
getPlugin: <T>(name: string) => T | undefined;
|
|
19
20
|
exportGunPair: (password?: string) => Promise<string>;
|
|
20
21
|
importGunPair: (pairData: string, password?: string) => Promise<boolean>;
|
|
22
|
+
gunPlugin: GunAdvancedPlugin | null;
|
|
23
|
+
useGunState: <T>(path: string, defaultValue?: T) => any;
|
|
24
|
+
useGunCollection: <T>(path: string, options?: any) => any;
|
|
25
|
+
useGunConnection: (path: string) => {
|
|
26
|
+
isConnected: boolean;
|
|
27
|
+
lastSeen: Date | null;
|
|
28
|
+
error: string | null;
|
|
29
|
+
};
|
|
30
|
+
useGunDebug: (path: string, enabled?: boolean) => void;
|
|
31
|
+
useGunRealtime: <T>(path: string, callback?: (data: T, key: string) => void) => {
|
|
32
|
+
data: T | null;
|
|
33
|
+
key: string | null;
|
|
34
|
+
};
|
|
35
|
+
put: (path: string, data: any) => Promise<void>;
|
|
36
|
+
get: (path: string) => any;
|
|
37
|
+
remove: (path: string) => Promise<void>;
|
|
21
38
|
};
|
|
22
39
|
export declare const useShogun: () => ShogunContextType;
|
|
23
40
|
type ShogunButtonProviderProps = {
|
|
24
41
|
children: React.ReactNode;
|
|
25
|
-
|
|
42
|
+
core: ShogunCore;
|
|
26
43
|
options: any;
|
|
27
44
|
onLoginSuccess?: (data: {
|
|
28
45
|
userPub: string;
|
|
@@ -38,7 +55,7 @@ type ShogunButtonProviderProps = {
|
|
|
38
55
|
}) => void;
|
|
39
56
|
onError?: (error: string) => void;
|
|
40
57
|
};
|
|
41
|
-
export declare function ShogunButtonProvider({ children,
|
|
58
|
+
export declare function ShogunButtonProvider({ children, core, options, onLoginSuccess, onSignupSuccess, onError, }: ShogunButtonProviderProps): React.JSX.Element;
|
|
42
59
|
type ShogunButtonComponent = React.FC & {
|
|
43
60
|
Provider: typeof ShogunButtonProvider;
|
|
44
61
|
useShogun: typeof useShogun;
|