@connect-xyz/auth-js 1.0.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 +245 -0
- package/dist/index.d.ts +242 -0
- package/dist/index.js +158 -0
- package/dist/index.umd.cjs +1 -0
- package/package.json +28 -0
package/README.md
ADDED
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
# @connect-xyz/auth-js
|
|
2
|
+
|
|
3
|
+
A JavaScript SDK that enables frontend 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
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
### Via NPM (recommended)
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install @connect-xyz/auth-js
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
```javascript
|
|
16
|
+
import { Auth } from '@connect-xyz/auth-js';
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
### Via CDN
|
|
20
|
+
|
|
21
|
+
You can import the script in your index file and use the Auth class provided by it.
|
|
22
|
+
|
|
23
|
+
```html
|
|
24
|
+
<script
|
|
25
|
+
type="module"
|
|
26
|
+
src="https://sdk.connect.xyz/auth-js/index.js"
|
|
27
|
+
></script>
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Or you can directly import in your javascript code.
|
|
31
|
+
|
|
32
|
+
```javascript
|
|
33
|
+
import { Auth } from 'https://sdk.connect.xyz/auth-js/index.js';
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Getting Started
|
|
37
|
+
|
|
38
|
+
Follow these simple steps to integrate Connect Auth into your frontend application:
|
|
39
|
+
|
|
40
|
+
### 1. Import the Auth module
|
|
41
|
+
|
|
42
|
+
```tsx
|
|
43
|
+
import { Auth } from '@connect-xyz/auth-js';
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### 1.1 Or import via CDN
|
|
47
|
+
|
|
48
|
+
```javascript
|
|
49
|
+
import { Auth } from 'https://sdk.connect.xyz/auth-js/index.js';
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### 2. Initialize the Auth module into Your App
|
|
53
|
+
|
|
54
|
+
```javascript
|
|
55
|
+
// Create an Auth instance with configuration
|
|
56
|
+
const auth = new Auth({
|
|
57
|
+
jwt: 'your-jwt-token',
|
|
58
|
+
env: 'production', // or 'sandbox'
|
|
59
|
+
onError: ({ error, reason }) => {
|
|
60
|
+
console.error('Auth error:', error, 'Reason:', reason);
|
|
61
|
+
},
|
|
62
|
+
onClose: () => {
|
|
63
|
+
console.log('Auth widget closed');
|
|
64
|
+
},
|
|
65
|
+
onDeposit: ({ data }) => {
|
|
66
|
+
console.log('Deposit initiated:', data);
|
|
67
|
+
},
|
|
68
|
+
onEvent: ({ type, data }) => {
|
|
69
|
+
console.log('Event received:', type, data);
|
|
70
|
+
},
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
// Render the widget to a container element
|
|
74
|
+
const container = document.getElementById('auth-container');
|
|
75
|
+
await auth.render(container);
|
|
76
|
+
|
|
77
|
+
// Update configuration dynamically
|
|
78
|
+
auth.updateConfig({
|
|
79
|
+
jwt: 'new-jwt-token',
|
|
80
|
+
onError: newErrorHandler,
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
// Check if rendered
|
|
84
|
+
if (auth.isRendered()) {
|
|
85
|
+
console.log('Widget is active');
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// Clean up when done
|
|
89
|
+
auth.destroy();
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### 2.1 Using Typescript (optional)
|
|
93
|
+
|
|
94
|
+
```typescript
|
|
95
|
+
import { Auth, AuthConfig, ErrorData, DepositData } from '@connect/auth-js';
|
|
96
|
+
|
|
97
|
+
const config: AuthConfig = {
|
|
98
|
+
jwt: 'your-jwt-token',
|
|
99
|
+
env: 'sandbox',
|
|
100
|
+
onError: ({ error, reason }: ErrorData) => {
|
|
101
|
+
// Handle error with type safety
|
|
102
|
+
},
|
|
103
|
+
onDeposit: ({ data }: DepositData) => {
|
|
104
|
+
// Handle deposit with type safety
|
|
105
|
+
},
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
const auth = new Auth(config);
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
## API Reference
|
|
112
|
+
|
|
113
|
+
### Auth widget Props
|
|
114
|
+
|
|
115
|
+
| Prop | Type | Required | Default | Description |
|
|
116
|
+
| ----------- | --------------------------------- | -------- | -------------- | ---------------------------------------------- |
|
|
117
|
+
| `jwt` | `string` | Yes | - | JWT token for authentication with Connect Auth |
|
|
118
|
+
| `env` | `"production" \| "sandbox"` | No | `"production"` | Target environment |
|
|
119
|
+
| `onError` | `({ errorCode, reason }) => void` | No | - | Callback for error events |
|
|
120
|
+
| `onClose` | `() => void` | No | - | Callback when the widget is closed |
|
|
121
|
+
| `onDeposit` | `({ data }) => void` | No | - | Callback for deposit received |
|
|
122
|
+
| `onEvent` | `({ type, data }) => void` | No | - | Callback for general events |
|
|
123
|
+
|
|
124
|
+
### Constructor
|
|
125
|
+
|
|
126
|
+
```javascript
|
|
127
|
+
new Auth(config: AuthConfig)
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
Creates a new Auth instance with the provided configuration.
|
|
131
|
+
|
|
132
|
+
#### Parameters
|
|
133
|
+
|
|
134
|
+
- `config` (AuthConfig): Configuration object
|
|
135
|
+
- `jwt` (string, required): JWT token for authentication
|
|
136
|
+
- `env` (string, optional): Environment - 'production' (default) or 'sandbox'
|
|
137
|
+
- `onError` (function, optional): Error callback
|
|
138
|
+
- `onClose` (function, optional): Close callback
|
|
139
|
+
- `onDeposit` (function, optional): Deposit callback
|
|
140
|
+
- `onEvent` (function, optional): General event callback
|
|
141
|
+
|
|
142
|
+
### Methods
|
|
143
|
+
|
|
144
|
+
#### `render(container: HTMLElement): Promise<void>`
|
|
145
|
+
|
|
146
|
+
Renders the Auth widget to the specified container element.
|
|
147
|
+
|
|
148
|
+
```javascript
|
|
149
|
+
await auth.render(document.getElementById('auth-container'));
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
#### `updateConfig(config: Partial<AuthConfig>): void`
|
|
153
|
+
|
|
154
|
+
Updates the configuration of an already rendered widget.
|
|
155
|
+
|
|
156
|
+
```javascript
|
|
157
|
+
auth.updateConfig({
|
|
158
|
+
jwt: 'new-token',
|
|
159
|
+
onError: newErrorHandler,
|
|
160
|
+
});
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
#### `destroy(): void`
|
|
164
|
+
|
|
165
|
+
Removes the widget from the DOM and cleans up resources.
|
|
166
|
+
|
|
167
|
+
```javascript
|
|
168
|
+
auth.destroy();
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
#### `isRendered(): boolean`
|
|
172
|
+
|
|
173
|
+
Returns whether the widget is currently rendered.
|
|
174
|
+
|
|
175
|
+
```javascript
|
|
176
|
+
if (auth.isRendered()) {
|
|
177
|
+
// Widget is active
|
|
178
|
+
}
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
#### `getConfig(): AuthConfig`
|
|
182
|
+
|
|
183
|
+
Returns a copy of the current configuration.
|
|
184
|
+
|
|
185
|
+
```javascript
|
|
186
|
+
const config = auth.getConfig();
|
|
187
|
+
console.log('Current JWT:', config.jwt);
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
## Callback Functions
|
|
191
|
+
|
|
192
|
+
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).
|
|
193
|
+
|
|
194
|
+
### onError
|
|
195
|
+
|
|
196
|
+
Called when an error occurs in the Auth widget.
|
|
197
|
+
|
|
198
|
+
```javascript
|
|
199
|
+
onError: ({ errorCode, reason }) => {
|
|
200
|
+
// errorCode: Error code
|
|
201
|
+
// reason: Human-readable error description
|
|
202
|
+
};
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
### onClose
|
|
206
|
+
|
|
207
|
+
Called when the Auth widget is closed by the user.
|
|
208
|
+
|
|
209
|
+
```javascript
|
|
210
|
+
onClose: () => {
|
|
211
|
+
// Handle close event
|
|
212
|
+
};
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
### onDeposit
|
|
216
|
+
|
|
217
|
+
Called when a deposit is received.
|
|
218
|
+
|
|
219
|
+
```javascript
|
|
220
|
+
onDeposit: ({ data }) => {
|
|
221
|
+
// data: Object containing deposit details
|
|
222
|
+
};
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
### onEvent
|
|
226
|
+
|
|
227
|
+
Called for general events from the Auth widget.
|
|
228
|
+
|
|
229
|
+
```javascript
|
|
230
|
+
onEvent: ({ type, data }) => {
|
|
231
|
+
// type: Event type string
|
|
232
|
+
// data: Event-specific data object
|
|
233
|
+
};
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
## Browser Support
|
|
237
|
+
|
|
238
|
+
- Chrome/Edge 90+
|
|
239
|
+
- Firefox 88+
|
|
240
|
+
- Safari 14+
|
|
241
|
+
- All modern browsers with Web Components support
|
|
242
|
+
|
|
243
|
+
## More Information & Support
|
|
244
|
+
|
|
245
|
+
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,242 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Connect Auth JavaScript SDK
|
|
3
|
+
*
|
|
4
|
+
* A programmatic JavaScript SDK for integrating the Connect Auth widget
|
|
5
|
+
* into web applications without requiring HTML custom elements.
|
|
6
|
+
*
|
|
7
|
+
* @packageDocumentation
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Generic app event structure
|
|
12
|
+
* @template TType - Event type string
|
|
13
|
+
* @template TData - Event data payload
|
|
14
|
+
*/
|
|
15
|
+
declare type AppEvent<TType extends string = string, TData = Record<string, unknown>> = {
|
|
16
|
+
/** The type of event that occurred */
|
|
17
|
+
type: TType;
|
|
18
|
+
/** Data associated with the event */
|
|
19
|
+
data: TData;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Auth SDK class for programmatic control of the Connect Auth widget
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```javascript
|
|
27
|
+
* const auth = new Auth({
|
|
28
|
+
* jwt: 'your-jwt-token',
|
|
29
|
+
* env: 'production',
|
|
30
|
+
* onError: ({ error, reason }) => console.error(error, reason),
|
|
31
|
+
* onClose: () => console.log('Auth closed'),
|
|
32
|
+
* onDeposit: ({ data }) => console.log('Deposit', data),
|
|
33
|
+
* onEvent: ({ type, data }) => console.log('Event', type, data)
|
|
34
|
+
* });
|
|
35
|
+
*
|
|
36
|
+
* // Render to a container
|
|
37
|
+
* await auth.render(document.getElementById('auth-container'));
|
|
38
|
+
*
|
|
39
|
+
* // Update configuration
|
|
40
|
+
* auth.updateConfig({ jwt: 'new-token' });
|
|
41
|
+
*
|
|
42
|
+
* // Clean up
|
|
43
|
+
* auth.destroy();
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
declare class Auth {
|
|
47
|
+
private config;
|
|
48
|
+
private state;
|
|
49
|
+
/**
|
|
50
|
+
* Create a new Auth instance
|
|
51
|
+
* @param config - Configuration options for the Auth SDK
|
|
52
|
+
*/
|
|
53
|
+
constructor(config: AuthConfig);
|
|
54
|
+
/**
|
|
55
|
+
* Get the current environment
|
|
56
|
+
*/
|
|
57
|
+
private get environment();
|
|
58
|
+
/**
|
|
59
|
+
* Load the web component script if not already loaded
|
|
60
|
+
*/
|
|
61
|
+
private ensureScriptLoaded;
|
|
62
|
+
/**
|
|
63
|
+
* Create the web component element
|
|
64
|
+
*/
|
|
65
|
+
private createWebComponent;
|
|
66
|
+
/**
|
|
67
|
+
* Render the Auth widget to a container element
|
|
68
|
+
* @param container - The container element to render the widget into
|
|
69
|
+
* @returns Promise that resolves when the widget is rendered
|
|
70
|
+
*/
|
|
71
|
+
render(container: HTMLElement): Promise<void>;
|
|
72
|
+
/**
|
|
73
|
+
* Update the configuration of the Auth widget
|
|
74
|
+
* @param config - Partial configuration to update
|
|
75
|
+
*/
|
|
76
|
+
updateConfig(config: Partial<AuthConfig>): void;
|
|
77
|
+
/**
|
|
78
|
+
* Destroy the Auth widget and clean up resources
|
|
79
|
+
*/
|
|
80
|
+
destroy(): void;
|
|
81
|
+
/**
|
|
82
|
+
* Check if the Auth widget is currently rendered
|
|
83
|
+
* @returns True if the widget is rendered, false otherwise
|
|
84
|
+
*/
|
|
85
|
+
isRendered(): boolean;
|
|
86
|
+
/**
|
|
87
|
+
* Get the current configuration
|
|
88
|
+
* @returns The current configuration object
|
|
89
|
+
*/
|
|
90
|
+
getConfig(): Readonly<AuthConfig>;
|
|
91
|
+
}
|
|
92
|
+
export { Auth }
|
|
93
|
+
export default Auth;
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Callback function types for Connect Auth components
|
|
97
|
+
* Extends common callbacks with auth-specific callbacks
|
|
98
|
+
*/
|
|
99
|
+
declare type AuthCallbacks = CommonCallbacks<AuthEvent> & {
|
|
100
|
+
/** Called when a deposit action occurs */
|
|
101
|
+
onDeposit?: (deposit: DepositCompletedPayload) => void;
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Configuration options for the Auth SDK
|
|
106
|
+
*/
|
|
107
|
+
export declare interface AuthConfig extends AuthCallbacks {
|
|
108
|
+
/**
|
|
109
|
+
* JWT token used for authentication with the Connect Auth service
|
|
110
|
+
*/
|
|
111
|
+
jwt: string;
|
|
112
|
+
/**
|
|
113
|
+
* Target environment for the Connect Auth service
|
|
114
|
+
* @default 'production'
|
|
115
|
+
*/
|
|
116
|
+
env?: Environment;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Auth event structure for auth-specific events
|
|
121
|
+
*/
|
|
122
|
+
declare type AuthEvent = AppEvent<AuthEventType, DepositSubmittedEventData>;
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Auth event type literals
|
|
126
|
+
*/
|
|
127
|
+
declare type AuthEventType = 'deposit.submitted';
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Internal state of the Auth SDK
|
|
131
|
+
*/
|
|
132
|
+
export declare interface AuthState {
|
|
133
|
+
/** Whether the SDK is initialized */
|
|
134
|
+
initialized: boolean;
|
|
135
|
+
/** Whether the web component script is loaded */
|
|
136
|
+
scriptLoaded: boolean;
|
|
137
|
+
/** The container element where the auth widget is rendered */
|
|
138
|
+
container: HTMLElement | null;
|
|
139
|
+
/** The web component element */
|
|
140
|
+
element: HTMLElement | null;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Common callback function types shared across all Connect apps
|
|
145
|
+
* @template TEvent - The event type for onEvent callback
|
|
146
|
+
*/
|
|
147
|
+
declare type CommonCallbacks<TEvent = AppEvent> = {
|
|
148
|
+
/** Called when an error occurs */
|
|
149
|
+
onError?: (error: ErrorPayload) => void;
|
|
150
|
+
/** Called when the component is closed */
|
|
151
|
+
onClose?: () => void;
|
|
152
|
+
/** Called when a general event occurs */
|
|
153
|
+
onEvent?: (event: TEvent) => void;
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* ConnectAuth element interface
|
|
158
|
+
*/
|
|
159
|
+
export declare interface ConnectAuthElement extends HTMLElement {
|
|
160
|
+
jwt: string;
|
|
161
|
+
env?: Environment;
|
|
162
|
+
onError?: (errorData: ErrorPayload) => void;
|
|
163
|
+
onClose?: () => void;
|
|
164
|
+
onDeposit?: (depositData: DepositCompletedPayload) => void;
|
|
165
|
+
onEvent?: (eventData: AuthEvent) => void;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Deposit completed payload structure for deposit callbacks
|
|
170
|
+
*/
|
|
171
|
+
declare type DepositCompletedPayload = {
|
|
172
|
+
/** Data associated with the deposit */
|
|
173
|
+
data: {
|
|
174
|
+
/** Unique identifier for the deposit */
|
|
175
|
+
depositId: string;
|
|
176
|
+
/** Current status of the deposit */
|
|
177
|
+
status: DepositStatus;
|
|
178
|
+
/** Asset identifier (e.g., 'btc', 'eth') */
|
|
179
|
+
assetId: string;
|
|
180
|
+
/** Network identifier (e.g., 'bitcoin', 'ethereum') */
|
|
181
|
+
networkId: string;
|
|
182
|
+
/** Amount being deposited */
|
|
183
|
+
amount: string;
|
|
184
|
+
};
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Deposit status object structure
|
|
189
|
+
*/
|
|
190
|
+
declare type DepositStatus = {
|
|
191
|
+
/** Status value */
|
|
192
|
+
value: string;
|
|
193
|
+
/** Human-readable details about the status */
|
|
194
|
+
details: string;
|
|
195
|
+
/** Timestamp when the status occurred (ISO 8601 format) */
|
|
196
|
+
occurredAt: string;
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Deposit submitted event data
|
|
201
|
+
*/
|
|
202
|
+
declare type DepositSubmittedEventData = {
|
|
203
|
+
/** Unique identifier for the deposit */
|
|
204
|
+
depositId: string;
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Environment configuration for the Auth SDK
|
|
209
|
+
*/
|
|
210
|
+
export declare type Environment = 'sandbox' | 'production';
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Generic error codes for all Connect applications
|
|
214
|
+
*/
|
|
215
|
+
declare enum ErrorCode {
|
|
216
|
+
/** Network connectivity error */
|
|
217
|
+
NETWORK_ERROR = 'network_error',
|
|
218
|
+
/** Authentication or session expired error */
|
|
219
|
+
AUTH_ERROR = 'auth_error',
|
|
220
|
+
/** Resource not found error */
|
|
221
|
+
NOT_FOUND_ERROR = 'not_found_error',
|
|
222
|
+
/** Validation error from user input */
|
|
223
|
+
VALIDATION_ERROR = 'validation_error',
|
|
224
|
+
/** Server error (5xx) */
|
|
225
|
+
SERVER_ERROR = 'server_error',
|
|
226
|
+
/** Client error (4xx) */
|
|
227
|
+
CLIENT_ERROR = 'client_error',
|
|
228
|
+
/** Unknown or unexpected error */
|
|
229
|
+
UNKNOWN_ERROR = 'unknown_error',
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Generic error payload structure for error callbacks
|
|
234
|
+
*/
|
|
235
|
+
declare type ErrorPayload = {
|
|
236
|
+
/** Error code indicating the type of error */
|
|
237
|
+
errorCode: ErrorCode;
|
|
238
|
+
/** Human-readable reason for the error */
|
|
239
|
+
reason: string;
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
export { }
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
var h = Object.defineProperty;
|
|
2
|
+
var E = (n, t, e) => t in n ? h(n, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : n[t] = e;
|
|
3
|
+
var c = (n, t, e) => E(n, typeof t != "symbol" ? t + "" : t, e);
|
|
4
|
+
const u = {
|
|
5
|
+
sandbox: "https://sdk.sandbox.connect.xyz/auth-web/index.js",
|
|
6
|
+
production: "https://sdk.connect.xyz/auth-web/index.js"
|
|
7
|
+
}, m = "connect-auth-script", p = "connect-auth", d = "production", r = {
|
|
8
|
+
ALREADY_RENDERED: "Auth widget is already rendered. Call destroy() before rendering again.",
|
|
9
|
+
NOT_RENDERED: "Auth widget is not rendered. Call render() first.",
|
|
10
|
+
INVALID_CONTAINER: "Invalid container element provided.",
|
|
11
|
+
SCRIPT_LOAD_FAILED: "Failed to load the Connect Auth script.",
|
|
12
|
+
WEB_COMPONENT_NOT_DEFINED: "Web component is not defined. Script may not be loaded.",
|
|
13
|
+
INVALID_JWT: "JWT token is required and must be a string."
|
|
14
|
+
}, a = /* @__PURE__ */ new Map();
|
|
15
|
+
function f(n) {
|
|
16
|
+
return u[n];
|
|
17
|
+
}
|
|
18
|
+
function l(n) {
|
|
19
|
+
return `${m}-${n}`;
|
|
20
|
+
}
|
|
21
|
+
function w(n) {
|
|
22
|
+
const t = l(n);
|
|
23
|
+
return !!document.getElementById(t);
|
|
24
|
+
}
|
|
25
|
+
async function D(n) {
|
|
26
|
+
const t = l(n);
|
|
27
|
+
if (w(n))
|
|
28
|
+
return Promise.resolve();
|
|
29
|
+
if (a.has(t))
|
|
30
|
+
return a.get(t);
|
|
31
|
+
const e = new Promise((i, s) => {
|
|
32
|
+
const o = document.createElement("script");
|
|
33
|
+
o.id = t, o.src = f(n), o.type = "module", o.async = !0, o.onload = () => {
|
|
34
|
+
setTimeout(() => {
|
|
35
|
+
customElements.get("connect-auth") ? i() : s(new Error(r.WEB_COMPONENT_NOT_DEFINED));
|
|
36
|
+
}, 0);
|
|
37
|
+
}, o.onerror = () => {
|
|
38
|
+
a.delete(t), s(new Error(`${r.SCRIPT_LOAD_FAILED} (${n})`));
|
|
39
|
+
}, document.head.appendChild(o);
|
|
40
|
+
});
|
|
41
|
+
a.set(t, e);
|
|
42
|
+
try {
|
|
43
|
+
await e;
|
|
44
|
+
} catch (i) {
|
|
45
|
+
throw a.delete(t), i;
|
|
46
|
+
}
|
|
47
|
+
return e;
|
|
48
|
+
}
|
|
49
|
+
async function N(n = 5e3) {
|
|
50
|
+
const t = "connect-auth";
|
|
51
|
+
return customElements.get(t) ? Promise.resolve() : new Promise((e, i) => {
|
|
52
|
+
const s = setTimeout(() => {
|
|
53
|
+
i(new Error(`Timeout waiting for ${t} to be defined`));
|
|
54
|
+
}, n);
|
|
55
|
+
customElements.whenDefined(t).then(() => {
|
|
56
|
+
clearTimeout(s), e();
|
|
57
|
+
}).catch((o) => {
|
|
58
|
+
clearTimeout(s), i(o);
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
class I {
|
|
63
|
+
/**
|
|
64
|
+
* Create a new Auth instance
|
|
65
|
+
* @param config - Configuration options for the Auth SDK
|
|
66
|
+
*/
|
|
67
|
+
constructor(t) {
|
|
68
|
+
c(this, "config");
|
|
69
|
+
c(this, "state");
|
|
70
|
+
if (!t.jwt || typeof t.jwt != "string")
|
|
71
|
+
throw new Error(r.INVALID_JWT);
|
|
72
|
+
this.config = {
|
|
73
|
+
...t,
|
|
74
|
+
env: t.env || d
|
|
75
|
+
}, this.state = {
|
|
76
|
+
initialized: !1,
|
|
77
|
+
scriptLoaded: !1,
|
|
78
|
+
container: null,
|
|
79
|
+
element: null
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Get the current environment
|
|
84
|
+
*/
|
|
85
|
+
get environment() {
|
|
86
|
+
return this.config.env || d;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Load the web component script if not already loaded
|
|
90
|
+
*/
|
|
91
|
+
async ensureScriptLoaded() {
|
|
92
|
+
if (!this.state.scriptLoaded)
|
|
93
|
+
try {
|
|
94
|
+
await D(this.environment), await N(), this.state.scriptLoaded = !0;
|
|
95
|
+
} catch (t) {
|
|
96
|
+
throw console.error("Failed to load Connect Auth script:", t), t;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Create the web component element
|
|
101
|
+
*/
|
|
102
|
+
createWebComponent() {
|
|
103
|
+
const t = document.createElement(p);
|
|
104
|
+
return t.jwt = this.config.jwt, this.config.env && (t.env = this.config.env), this.config.onError && (t.onError = this.config.onError), this.config.onClose && (t.onClose = this.config.onClose), this.config.onDeposit && (t.onDeposit = this.config.onDeposit), this.config.onEvent && (t.onEvent = this.config.onEvent), t;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Render the Auth widget to a container element
|
|
108
|
+
* @param container - The container element to render the widget into
|
|
109
|
+
* @returns Promise that resolves when the widget is rendered
|
|
110
|
+
*/
|
|
111
|
+
async render(t) {
|
|
112
|
+
if (!t || !(t instanceof HTMLElement))
|
|
113
|
+
throw new Error(r.INVALID_CONTAINER);
|
|
114
|
+
if (this.state.initialized)
|
|
115
|
+
throw new Error(r.ALREADY_RENDERED);
|
|
116
|
+
try {
|
|
117
|
+
await this.ensureScriptLoaded();
|
|
118
|
+
const e = this.createWebComponent();
|
|
119
|
+
t.innerHTML = "", t.appendChild(e), this.state.container = t, this.state.element = e, this.state.initialized = !0;
|
|
120
|
+
} catch (e) {
|
|
121
|
+
throw console.error("Failed to render Auth widget:", e), e;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Update the configuration of the Auth widget
|
|
126
|
+
* @param config - Partial configuration to update
|
|
127
|
+
*/
|
|
128
|
+
updateConfig(t) {
|
|
129
|
+
if (!this.state.initialized || !this.state.element)
|
|
130
|
+
throw new Error(r.NOT_RENDERED);
|
|
131
|
+
const e = this.state.element;
|
|
132
|
+
t.jwt && (this.config.jwt = t.jwt, e.jwt = t.jwt), t.onError !== void 0 && (this.config.onError = t.onError, e.onError = t.onError), t.onClose !== void 0 && (this.config.onClose = t.onClose, e.onClose = t.onClose), t.onDeposit !== void 0 && (this.config.onDeposit = t.onDeposit, e.onDeposit = t.onDeposit), t.onEvent !== void 0 && (this.config.onEvent = t.onEvent, e.onEvent = t.onEvent);
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Destroy the Auth widget and clean up resources
|
|
136
|
+
*/
|
|
137
|
+
destroy() {
|
|
138
|
+
this.state.initialized && (this.state.element && this.state.element.parentNode && this.state.element.parentNode.removeChild(this.state.element), this.state.container && (this.state.container.innerHTML = ""), this.state.container = null, this.state.element = null, this.state.initialized = !1);
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Check if the Auth widget is currently rendered
|
|
142
|
+
* @returns True if the widget is rendered, false otherwise
|
|
143
|
+
*/
|
|
144
|
+
isRendered() {
|
|
145
|
+
return this.state.initialized;
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Get the current configuration
|
|
149
|
+
* @returns The current configuration object
|
|
150
|
+
*/
|
|
151
|
+
getConfig() {
|
|
152
|
+
return { ...this.config };
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
export {
|
|
156
|
+
I as Auth,
|
|
157
|
+
I as default
|
|
158
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
(function(n,o){typeof exports=="object"&&typeof module<"u"?o(exports):typeof define=="function"&&define.amd?define(["exports"],o):(n=typeof globalThis<"u"?globalThis:n||self,o(n.Auth={}))})(this,function(n){"use strict";var T=Object.defineProperty;var C=(n,o,s)=>o in n?T(n,o,{enumerable:!0,configurable:!0,writable:!0,value:s}):n[o]=s;var h=(n,o,s)=>C(n,typeof o!="symbol"?o+"":o,s);const o={sandbox:"https://sdk.sandbox.connect.xyz/auth-web/index.js",production:"https://sdk.connect.xyz/auth-web/index.js"},s="connect-auth-script",m="connect-auth",u="production",a={ALREADY_RENDERED:"Auth widget is already rendered. Call destroy() before rendering again.",NOT_RENDERED:"Auth widget is not rendered. Call render() first.",INVALID_CONTAINER:"Invalid container element provided.",SCRIPT_LOAD_FAILED:"Failed to load the Connect Auth script.",WEB_COMPONENT_NOT_DEFINED:"Web component is not defined. Script may not be loaded.",INVALID_JWT:"JWT token is required and must be a string."},c=new Map;function p(i){return o[i]}function E(i){return`${s}-${i}`}function w(i){const t=E(i);return!!document.getElementById(t)}async function D(i){const t=E(i);if(w(i))return Promise.resolve();if(c.has(t))return c.get(t);const e=new Promise((d,l)=>{const r=document.createElement("script");r.id=t,r.src=p(i),r.type="module",r.async=!0,r.onload=()=>{setTimeout(()=>{customElements.get("connect-auth")?d():l(new Error(a.WEB_COMPONENT_NOT_DEFINED))},0)},r.onerror=()=>{c.delete(t),l(new Error(`${a.SCRIPT_LOAD_FAILED} (${i})`))},document.head.appendChild(r)});c.set(t,e);try{await e}catch(d){throw c.delete(t),d}return e}async function N(i=5e3){const t="connect-auth";return customElements.get(t)?Promise.resolve():new Promise((e,d)=>{const l=setTimeout(()=>{d(new Error(`Timeout waiting for ${t} to be defined`))},i);customElements.whenDefined(t).then(()=>{clearTimeout(l),e()}).catch(r=>{clearTimeout(l),d(r)})})}class f{constructor(t){h(this,"config");h(this,"state");if(!t.jwt||typeof t.jwt!="string")throw new Error(a.INVALID_JWT);this.config={...t,env:t.env||u},this.state={initialized:!1,scriptLoaded:!1,container:null,element:null}}get environment(){return this.config.env||u}async ensureScriptLoaded(){if(!this.state.scriptLoaded)try{await D(this.environment),await N(),this.state.scriptLoaded=!0}catch(t){throw console.error("Failed to load Connect Auth script:",t),t}}createWebComponent(){const t=document.createElement(m);return t.jwt=this.config.jwt,this.config.env&&(t.env=this.config.env),this.config.onError&&(t.onError=this.config.onError),this.config.onClose&&(t.onClose=this.config.onClose),this.config.onDeposit&&(t.onDeposit=this.config.onDeposit),this.config.onEvent&&(t.onEvent=this.config.onEvent),t}async render(t){if(!t||!(t instanceof HTMLElement))throw new Error(a.INVALID_CONTAINER);if(this.state.initialized)throw new Error(a.ALREADY_RENDERED);try{await this.ensureScriptLoaded();const e=this.createWebComponent();t.innerHTML="",t.appendChild(e),this.state.container=t,this.state.element=e,this.state.initialized=!0}catch(e){throw console.error("Failed to render Auth widget:",e),e}}updateConfig(t){if(!this.state.initialized||!this.state.element)throw new Error(a.NOT_RENDERED);const e=this.state.element;t.jwt&&(this.config.jwt=t.jwt,e.jwt=t.jwt),t.onError!==void 0&&(this.config.onError=t.onError,e.onError=t.onError),t.onClose!==void 0&&(this.config.onClose=t.onClose,e.onClose=t.onClose),t.onDeposit!==void 0&&(this.config.onDeposit=t.onDeposit,e.onDeposit=t.onDeposit),t.onEvent!==void 0&&(this.config.onEvent=t.onEvent,e.onEvent=t.onEvent)}destroy(){this.state.initialized&&(this.state.element&&this.state.element.parentNode&&this.state.element.parentNode.removeChild(this.state.element),this.state.container&&(this.state.container.innerHTML=""),this.state.container=null,this.state.element=null,this.state.initialized=!1)}isRendered(){return this.state.initialized}getConfig(){return{...this.config}}}n.Auth=f,n.default=f,Object.defineProperties(n,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
|
package/package.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@connect-xyz/auth-js",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"private": false,
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
"./package.json": "./package.json",
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"import": "./dist/index.js",
|
|
14
|
+
"require": "./dist/index.umd.js",
|
|
15
|
+
"default": "./dist/index.js"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"dist",
|
|
20
|
+
"!**/*.tsbuildinfo"
|
|
21
|
+
],
|
|
22
|
+
"nx": {
|
|
23
|
+
"name": "auth-js",
|
|
24
|
+
"targets": {
|
|
25
|
+
"build-static": {}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|