@connect-xyz/auth-react 0.12.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/README.md +139 -0
- package/dist/index.d.ts +190 -0
- package/dist/index.js +35 -0
- package/package.json +27 -0
package/README.md
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
# @connect-xyz/auth-react
|
|
2
|
+
|
|
3
|
+
A React SDK that enables frontend React applications to seamlessly integrate with the Connect Auth product.
|
|
4
|
+
|
|
5
|
+
Connect Auth provides a secure, customizable authentication flow for crypto deposits. Learn more in the [Connect Auth documentation](https://docs.zerohash.com/docs/auth).
|
|
6
|
+
|
|
7
|
+
## Requirements
|
|
8
|
+
|
|
9
|
+
- React 18.0.0 or higher
|
|
10
|
+
- React DOM 18.0.0 or higher
|
|
11
|
+
|
|
12
|
+
## Installation
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
npm install @connect-xyz/auth-react
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Getting Started
|
|
19
|
+
|
|
20
|
+
Follow these simple steps to integrate Connect Auth into your React application:
|
|
21
|
+
|
|
22
|
+
### 1. Import the Auth Component
|
|
23
|
+
|
|
24
|
+
```tsx
|
|
25
|
+
import { Auth } from '@connect-xyz/auth-react';
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
### 2. Add the Auth Component to Your App
|
|
29
|
+
|
|
30
|
+
```tsx
|
|
31
|
+
function App() {
|
|
32
|
+
const jwt = 'your-jwt-token'; // Obtain this from your backend authentication
|
|
33
|
+
|
|
34
|
+
return (
|
|
35
|
+
<Auth
|
|
36
|
+
jwt={jwt}
|
|
37
|
+
env="production" // or "sandbox" for testing
|
|
38
|
+
/>
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### 3. Configure Event Callbacks (Optional)
|
|
44
|
+
|
|
45
|
+
Listen to events from the Auth SDK to handle user interactions:
|
|
46
|
+
|
|
47
|
+
```tsx
|
|
48
|
+
function App() {
|
|
49
|
+
const jwt = 'your-jwt-token';
|
|
50
|
+
|
|
51
|
+
const handleError = ({ errorCode, reason }) => {
|
|
52
|
+
console.log('Auth error occurred:', errorCode, 'Reason:', reason);
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const handleClose = () => {
|
|
56
|
+
console.log('Auth widget closed');
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
const handleDeposit = ({ data }) => {
|
|
60
|
+
console.log('Deposit data:', data);
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
const handleEvent = ({ type, data }) => {
|
|
64
|
+
console.log('Auth event:', type, 'Data:', data);
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
return (
|
|
68
|
+
<Auth
|
|
69
|
+
jwt={jwt}
|
|
70
|
+
env="production"
|
|
71
|
+
onError={handleError}
|
|
72
|
+
onClose={handleClose}
|
|
73
|
+
onDeposit={handleDeposit}
|
|
74
|
+
onEvent={handleEvent}
|
|
75
|
+
/>
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
For detailed information about callback events and their payloads, see the [Auth SDK Callbacks documentation](https://docs.zerohash.com/docs/auth-standalone#4-organization-frontend-listens-to-auth-sdk-callbacks).
|
|
81
|
+
|
|
82
|
+
## Complete Example
|
|
83
|
+
|
|
84
|
+
Here's a full example of integrating Connect Auth into your React application:
|
|
85
|
+
|
|
86
|
+
```tsx
|
|
87
|
+
import React from 'react';
|
|
88
|
+
import { Auth } from '@connect-xyz/auth-react';
|
|
89
|
+
|
|
90
|
+
function App() {
|
|
91
|
+
// JWT token should be obtained from your backend
|
|
92
|
+
const jwt = 'your-jwt-token';
|
|
93
|
+
|
|
94
|
+
// Choose environment: "production" for live apps, "sandbox" for testing
|
|
95
|
+
const environment = 'production';
|
|
96
|
+
|
|
97
|
+
return (
|
|
98
|
+
<div className="App">
|
|
99
|
+
<h1>My Crypto App</h1>
|
|
100
|
+
|
|
101
|
+
<Auth
|
|
102
|
+
jwt={jwt}
|
|
103
|
+
env={environment}
|
|
104
|
+
onError={({ errorCode, reason }) => {
|
|
105
|
+
console.log('Error:', errorCode, 'Reason:', reason);
|
|
106
|
+
}}
|
|
107
|
+
onClose={() => {
|
|
108
|
+
console.log('Auth widget closed');
|
|
109
|
+
}}
|
|
110
|
+
onDeposit={({ data }) => {
|
|
111
|
+
console.log('Deposit successful:', data);
|
|
112
|
+
}}
|
|
113
|
+
onEvent={({ type, data }) => {
|
|
114
|
+
console.log('Event type:', type, 'Event data:', data);
|
|
115
|
+
}}
|
|
116
|
+
/>
|
|
117
|
+
</div>
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export default App;
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
## API Reference
|
|
125
|
+
|
|
126
|
+
### Auth Component Props
|
|
127
|
+
|
|
128
|
+
| Prop | Type | Required | Default | Description |
|
|
129
|
+
| ----------- | --------------------------------- | -------- | -------------- | ---------------------------------------------- |
|
|
130
|
+
| `jwt` | `string` | Yes | - | JWT token for authentication with Connect Auth |
|
|
131
|
+
| `env` | `"production" \| "sandbox"` | No | `"production"` | Target environment |
|
|
132
|
+
| `onError` | `({ errorCode, reason }) => void` | No | - | Callback for error events |
|
|
133
|
+
| `onClose` | `() => void` | No | - | Callback when the widget is closed |
|
|
134
|
+
| `onDeposit` | `({ data }) => void` | No | - | Callback for deposit received |
|
|
135
|
+
| `onEvent` | `({ type, data }) => void` | No | - | Callback for general events |
|
|
136
|
+
|
|
137
|
+
## More Information & Support
|
|
138
|
+
|
|
139
|
+
For comprehensive documentation or support about Connect Auth, visit our [ Documentation Page](https://docs.zerohash.com/).
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import { default as default_2 } from 'react';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Generic app event structure
|
|
5
|
+
* @template TType - Event type string
|
|
6
|
+
* @template TData - Event data payload
|
|
7
|
+
*/
|
|
8
|
+
declare type AppEvent<TType extends string = string, TData = Record<string, unknown>> = {
|
|
9
|
+
/** The type of event that occurred */
|
|
10
|
+
type: TType;
|
|
11
|
+
/** Data associated with the event */
|
|
12
|
+
data: TData;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* A React wrapper component for the Connect Auth product.
|
|
17
|
+
*
|
|
18
|
+
* @param props - Component properties
|
|
19
|
+
* @param jwt - JWT token for authentication (required)
|
|
20
|
+
* @param env - Target environment ("sandbox" | "production", defaults to "production")
|
|
21
|
+
* @param onError - Callback function for error handling
|
|
22
|
+
* @param onClose - Callback function for close events
|
|
23
|
+
* @param onDeposit - Callback function for deposit events
|
|
24
|
+
* @param onEvent - Callback function for general events
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```tsx
|
|
28
|
+
* // Basic usage with production environment (default)
|
|
29
|
+
* <Auth jwt="your-jwt-token" />
|
|
30
|
+
*
|
|
31
|
+
* // Using sandbox environment for testing
|
|
32
|
+
* <Auth jwt="your-jwt-token" env="sandbox" />
|
|
33
|
+
*
|
|
34
|
+
* // With callback functions
|
|
35
|
+
* <Auth
|
|
36
|
+
* jwt="your-jwt-token"
|
|
37
|
+
* onError={({ errorCode, reason }) => console.error('Auth error:', errorCode, 'Reason:', reason)}
|
|
38
|
+
* onClose={() => console.log('Auth closed')}
|
|
39
|
+
* onDeposit={({ data }) => console.log('Deposit completed:', data.depositId, 'Status:', data.status.value)}
|
|
40
|
+
* onEvent={({ type, data }) => console.log('Event:', type, 'Deposit ID:', data.depositId)}
|
|
41
|
+
* />
|
|
42
|
+
* ```
|
|
43
|
+
*
|
|
44
|
+
* @returns React component that renders the Connect Auth interface
|
|
45
|
+
*/
|
|
46
|
+
export declare const Auth: default_2.FC<AuthWrapperProps>;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Callback function types for Connect Auth components
|
|
50
|
+
* Extends common callbacks with auth-specific callbacks
|
|
51
|
+
*/
|
|
52
|
+
declare type AuthCallbacks = CommonCallbacks<AuthEvent> & {
|
|
53
|
+
/** Called when a deposit action occurs */
|
|
54
|
+
onDeposit?: (deposit: DepositCompletedPayload) => void;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Auth event structure for auth-specific events
|
|
59
|
+
*/
|
|
60
|
+
declare type AuthEvent = AppEvent<AuthEventType, DepositSubmittedEventData>;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Auth event type literals
|
|
64
|
+
*/
|
|
65
|
+
declare type AuthEventType = 'deposit.submitted';
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Props for the Auth component
|
|
69
|
+
*/
|
|
70
|
+
declare interface AuthWrapperProps extends AuthCallbacks {
|
|
71
|
+
/**
|
|
72
|
+
* JWT token used for authentication with the Connect Auth service.
|
|
73
|
+
* This token should be obtained from your authentication system and
|
|
74
|
+
* contain the necessary claims for user identification and authorization.
|
|
75
|
+
*
|
|
76
|
+
* @example "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
|
|
77
|
+
*/
|
|
78
|
+
jwt: string;
|
|
79
|
+
/**
|
|
80
|
+
* Target environment for the Connect Auth service.
|
|
81
|
+
*
|
|
82
|
+
* @default "production"
|
|
83
|
+
*
|
|
84
|
+
* Available environments:
|
|
85
|
+
* - `"sandbox"` - Certificate/testing environment for integration testing
|
|
86
|
+
* - `"production"` - Live production environment for real applications
|
|
87
|
+
*
|
|
88
|
+
* @example
|
|
89
|
+
* ```tsx
|
|
90
|
+
* // Use production (default)
|
|
91
|
+
* <Auth jwt={token} />
|
|
92
|
+
*
|
|
93
|
+
* // Use sandbox for testing
|
|
94
|
+
* <Auth jwt={token} env="sandbox" />
|
|
95
|
+
* ```
|
|
96
|
+
*/
|
|
97
|
+
env?: Environment;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Common callback function types shared across all Connect apps
|
|
102
|
+
* @template TEvent - The event type for onEvent callback
|
|
103
|
+
*/
|
|
104
|
+
declare type CommonCallbacks<TEvent = AppEvent> = {
|
|
105
|
+
/** Called when an error occurs */
|
|
106
|
+
onError?: (error: ErrorPayload) => void;
|
|
107
|
+
/** Called when the component is closed */
|
|
108
|
+
onClose?: () => void;
|
|
109
|
+
/** Called when a general event occurs */
|
|
110
|
+
onEvent?: (event: TEvent) => void;
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Deposit completed payload structure for deposit callbacks
|
|
115
|
+
*/
|
|
116
|
+
declare type DepositCompletedPayload = {
|
|
117
|
+
/** Data associated with the deposit */
|
|
118
|
+
data: {
|
|
119
|
+
/** Unique identifier for the deposit */
|
|
120
|
+
depositId: string;
|
|
121
|
+
/** Current status of the deposit */
|
|
122
|
+
status: DepositStatus;
|
|
123
|
+
/** Asset identifier (e.g., 'btc', 'eth') */
|
|
124
|
+
assetId: string;
|
|
125
|
+
/** Network identifier (e.g., 'bitcoin', 'ethereum') */
|
|
126
|
+
networkId: string;
|
|
127
|
+
/** Amount being deposited */
|
|
128
|
+
amount: string;
|
|
129
|
+
};
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Deposit status object structure
|
|
134
|
+
*/
|
|
135
|
+
declare type DepositStatus = {
|
|
136
|
+
/** Status value */
|
|
137
|
+
value: string;
|
|
138
|
+
/** Human-readable details about the status */
|
|
139
|
+
details: string;
|
|
140
|
+
/** Timestamp when the status occurred (ISO 8601 format) */
|
|
141
|
+
occurredAt: string;
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Deposit submitted event data
|
|
146
|
+
*/
|
|
147
|
+
declare type DepositSubmittedEventData = {
|
|
148
|
+
/** Unique identifier for the deposit */
|
|
149
|
+
depositId: string;
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Available environments for the Connect Auth service.
|
|
154
|
+
*
|
|
155
|
+
* - `"sandbox"` - Certificate/testing environment for integration and testing
|
|
156
|
+
* - `"production"` - Live production environment for real applications
|
|
157
|
+
*/
|
|
158
|
+
declare type Environment = 'sandbox' | 'production';
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Generic error codes for all Connect applications
|
|
162
|
+
*/
|
|
163
|
+
declare enum ErrorCode {
|
|
164
|
+
/** Network connectivity error */
|
|
165
|
+
NETWORK_ERROR = 'network_error',
|
|
166
|
+
/** Authentication or session expired error */
|
|
167
|
+
AUTH_ERROR = 'auth_error',
|
|
168
|
+
/** Resource not found error */
|
|
169
|
+
NOT_FOUND_ERROR = 'not_found_error',
|
|
170
|
+
/** Validation error from user input */
|
|
171
|
+
VALIDATION_ERROR = 'validation_error',
|
|
172
|
+
/** Server error (5xx) */
|
|
173
|
+
SERVER_ERROR = 'server_error',
|
|
174
|
+
/** Client error (4xx) */
|
|
175
|
+
CLIENT_ERROR = 'client_error',
|
|
176
|
+
/** Unknown or unexpected error */
|
|
177
|
+
UNKNOWN_ERROR = 'unknown_error',
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Generic error payload structure for error callbacks
|
|
182
|
+
*/
|
|
183
|
+
declare type ErrorPayload = {
|
|
184
|
+
/** Error code indicating the type of error */
|
|
185
|
+
errorCode: ErrorCode;
|
|
186
|
+
/** Human-readable reason for the error */
|
|
187
|
+
reason: string;
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
export { }
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import l, { useRef as m, useEffect as p } from "react";
|
|
2
|
+
const h = {
|
|
3
|
+
sandbox: "https://sdk.sandbox.connect.xyz/auth-web/index.js",
|
|
4
|
+
production: "https://sdk.connect.xyz/auth-web/index.js"
|
|
5
|
+
}, x = "connect-auth-script", a = "connect-auth", y = (n = "production") => h[n], R = ({
|
|
6
|
+
jwt: n,
|
|
7
|
+
env: c = "production",
|
|
8
|
+
onError: r,
|
|
9
|
+
onClose: o,
|
|
10
|
+
onDeposit: s,
|
|
11
|
+
onEvent: i,
|
|
12
|
+
...f
|
|
13
|
+
}) => {
|
|
14
|
+
const d = m(null);
|
|
15
|
+
return p(() => {
|
|
16
|
+
const t = d.current;
|
|
17
|
+
t && (r && (t.onError = r), o && (t.onClose = o), s && (t.onDeposit = s), i && (t.onEvent = i));
|
|
18
|
+
}, [r, o, s, i]), p(() => {
|
|
19
|
+
const t = y(c), u = `${x}-${c}`;
|
|
20
|
+
if (document.getElementById(u))
|
|
21
|
+
return;
|
|
22
|
+
const e = document.createElement("script");
|
|
23
|
+
e.id = u, e.src = t, e.type = "module", e.async = !0, e.onerror = () => {
|
|
24
|
+
console.error(`Failed to load the script for ${a} from ${c} environment.`);
|
|
25
|
+
}, document.head.appendChild(e);
|
|
26
|
+
}, [c]), l.createElement(a, {
|
|
27
|
+
ref: d,
|
|
28
|
+
jwt: n,
|
|
29
|
+
env: c,
|
|
30
|
+
...f
|
|
31
|
+
});
|
|
32
|
+
};
|
|
33
|
+
export {
|
|
34
|
+
R as Auth
|
|
35
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@connect-xyz/auth-react",
|
|
3
|
+
"version": "0.12.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
"./package.json": "./package.json",
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist",
|
|
18
|
+
"!**/*.tsbuildinfo"
|
|
19
|
+
],
|
|
20
|
+
"nx": {
|
|
21
|
+
"name": "auth-react"
|
|
22
|
+
},
|
|
23
|
+
"peerDependencies": {
|
|
24
|
+
"react": ">=18.0.0",
|
|
25
|
+
"react-dom": ">=18.0.0"
|
|
26
|
+
}
|
|
27
|
+
}
|