@agg-build/auth 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 +251 -0
- package/dist/chunk-4RGHFPGG.mjs +535 -0
- package/dist/chunk-AXBFBHS2.mjs +45 -0
- package/dist/chunk-BVV6GEOL.mjs +36 -0
- package/dist/chunk-FGEWDEEE.mjs +51 -0
- package/dist/chunk-MEJEY2D2.mjs +76 -0
- package/dist/connect-button.d.mts +28 -0
- package/dist/connect-button.d.ts +28 -0
- package/dist/connect-button.js +372 -0
- package/dist/connect-button.mjs +8 -0
- package/dist/email.d.mts +18 -0
- package/dist/email.d.ts +18 -0
- package/dist/email.js +97 -0
- package/dist/email.mjs +8 -0
- package/dist/index.d.mts +26 -0
- package/dist/index.d.ts +26 -0
- package/dist/index.js +749 -0
- package/dist/index.mjs +59 -0
- package/dist/oauth.d.mts +17 -0
- package/dist/oauth.d.ts +17 -0
- package/dist/oauth.js +124 -0
- package/dist/oauth.mjs +12 -0
- package/dist/siwe.d.mts +16 -0
- package/dist/siwe.d.ts +16 -0
- package/dist/siwe.js +129 -0
- package/dist/siwe.mjs +88 -0
- package/dist/siws.d.mts +14 -0
- package/dist/siws.d.ts +14 -0
- package/dist/siws.js +219 -0
- package/dist/siws.mjs +155 -0
- package/dist/types-D-OXBxXn.d.mts +60 -0
- package/dist/types-D-OXBxXn.d.ts +60 -0
- package/package.json +134 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,749 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __defProps = Object.defineProperties;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
10
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
11
|
+
var __spreadValues = (a, b) => {
|
|
12
|
+
for (var prop in b || (b = {}))
|
|
13
|
+
if (__hasOwnProp.call(b, prop))
|
|
14
|
+
__defNormalProp(a, prop, b[prop]);
|
|
15
|
+
if (__getOwnPropSymbols)
|
|
16
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
17
|
+
if (__propIsEnum.call(b, prop))
|
|
18
|
+
__defNormalProp(a, prop, b[prop]);
|
|
19
|
+
}
|
|
20
|
+
return a;
|
|
21
|
+
};
|
|
22
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
23
|
+
var __export = (target, all) => {
|
|
24
|
+
for (var name in all)
|
|
25
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
26
|
+
};
|
|
27
|
+
var __copyProps = (to, from, except, desc) => {
|
|
28
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
29
|
+
for (let key of __getOwnPropNames(from))
|
|
30
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
31
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
32
|
+
}
|
|
33
|
+
return to;
|
|
34
|
+
};
|
|
35
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
36
|
+
var __async = (__this, __arguments, generator) => {
|
|
37
|
+
return new Promise((resolve, reject) => {
|
|
38
|
+
var fulfilled = (value) => {
|
|
39
|
+
try {
|
|
40
|
+
step(generator.next(value));
|
|
41
|
+
} catch (e) {
|
|
42
|
+
reject(e);
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
var rejected = (value) => {
|
|
46
|
+
try {
|
|
47
|
+
step(generator.throw(value));
|
|
48
|
+
} catch (e) {
|
|
49
|
+
reject(e);
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
53
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
54
|
+
});
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
// src/index.ts
|
|
58
|
+
var src_exports = {};
|
|
59
|
+
__export(src_exports, {
|
|
60
|
+
AggAuthProvider: () => AggAuthProvider,
|
|
61
|
+
ConnectButton: () => ConnectButton,
|
|
62
|
+
createAppleAuthMethod: () => createAppleAuthMethod,
|
|
63
|
+
createEmailAuthMethod: () => createEmailAuthMethod,
|
|
64
|
+
createGoogleAuthMethod: () => createGoogleAuthMethod,
|
|
65
|
+
createTwitterAuthMethod: () => createTwitterAuthMethod,
|
|
66
|
+
useAggAuthCallback: () => useAggAuthCallback,
|
|
67
|
+
useAggAuthFlow: () => useAggAuthFlow
|
|
68
|
+
});
|
|
69
|
+
module.exports = __toCommonJS(src_exports);
|
|
70
|
+
|
|
71
|
+
// src/core/context.tsx
|
|
72
|
+
var import_react = require("react");
|
|
73
|
+
var import_hooks = require("@agg-build/hooks");
|
|
74
|
+
|
|
75
|
+
// src/shared/utils.ts
|
|
76
|
+
var toError = (value) => {
|
|
77
|
+
return value instanceof Error ? value : new Error(String(value));
|
|
78
|
+
};
|
|
79
|
+
var normalizeAuthError = (error) => {
|
|
80
|
+
const lower = error.message.toLowerCase();
|
|
81
|
+
if (lower.includes("user rejected") || lower.includes("user denied") || lower.includes("rejected the request") || lower.includes("denied transaction signature")) {
|
|
82
|
+
return new Error("Wallet request rejected.");
|
|
83
|
+
}
|
|
84
|
+
return error;
|
|
85
|
+
};
|
|
86
|
+
var shortenAddress = (value) => {
|
|
87
|
+
if (value.length < 10) return value;
|
|
88
|
+
if (value.startsWith("0x")) return `${value.slice(0, 6)}...${value.slice(-4)}`;
|
|
89
|
+
return `${value.slice(0, 4)}...${value.slice(-4)}`;
|
|
90
|
+
};
|
|
91
|
+
var shortenProfileLabel = (value) => {
|
|
92
|
+
if (!value) return value;
|
|
93
|
+
if (value.includes("@")) return value;
|
|
94
|
+
if (value.startsWith("0x")) return shortenAddress(value);
|
|
95
|
+
if (value.length <= 16) return value;
|
|
96
|
+
return `${value.slice(0, 8)}...${value.slice(-5)}`;
|
|
97
|
+
};
|
|
98
|
+
var resolveRedirectUrl = (redirectUrl) => {
|
|
99
|
+
if (redirectUrl) return redirectUrl;
|
|
100
|
+
if (typeof window === "undefined") return "http://localhost";
|
|
101
|
+
return `${window.location.origin}${window.location.pathname}${window.location.search}`;
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
// src/core/methods.ts
|
|
105
|
+
var getAvailableAuthMethods = (methods) => {
|
|
106
|
+
return methods.filter((method) => method.isAvailable);
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
// src/core/context.tsx
|
|
110
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
111
|
+
var AggAuthFlowContext = (0, import_react.createContext)(null);
|
|
112
|
+
var AggAuthProvider = ({
|
|
113
|
+
autoHandleAuthCallback = true,
|
|
114
|
+
children,
|
|
115
|
+
methods
|
|
116
|
+
}) => {
|
|
117
|
+
const auth = (0, import_hooks.useAggAuthContext)();
|
|
118
|
+
const handledCallbackRef = (0, import_react.useRef)(false);
|
|
119
|
+
const [activeMethodId, setActiveMethodId] = (0, import_react.useState)(null);
|
|
120
|
+
const [selectedMethodId, setSelectedMethodId] = (0, import_react.useState)(null);
|
|
121
|
+
const [error, setError] = (0, import_react.useState)(null);
|
|
122
|
+
const [notice, setNotice] = (0, import_react.useState)(null);
|
|
123
|
+
const [challengeSiteKey, setChallengeSiteKey] = (0, import_react.useState)(null);
|
|
124
|
+
const pendingMethodIdRef = (0, import_react.useRef)(null);
|
|
125
|
+
const pendingInputRef = (0, import_react.useRef)(void 0);
|
|
126
|
+
const challengeTimeoutRef = (0, import_react.useRef)(null);
|
|
127
|
+
(0, import_react.useEffect)(() => {
|
|
128
|
+
if (!autoHandleAuthCallback || handledCallbackRef.current) return;
|
|
129
|
+
handledCallbackRef.current = true;
|
|
130
|
+
void auth.handleAuthCallback().catch((callbackError) => {
|
|
131
|
+
setError(normalizeAuthError(toError(callbackError)));
|
|
132
|
+
});
|
|
133
|
+
}, [auth, autoHandleAuthCallback]);
|
|
134
|
+
(0, import_react.useEffect)(() => {
|
|
135
|
+
if (!auth.isAuthenticated) return;
|
|
136
|
+
setNotice(null);
|
|
137
|
+
setChallengeSiteKey(null);
|
|
138
|
+
pendingMethodIdRef.current = null;
|
|
139
|
+
pendingInputRef.current = void 0;
|
|
140
|
+
}, [auth.isAuthenticated]);
|
|
141
|
+
(0, import_react.useEffect)(() => {
|
|
142
|
+
if (challengeTimeoutRef.current) {
|
|
143
|
+
clearTimeout(challengeTimeoutRef.current);
|
|
144
|
+
challengeTimeoutRef.current = null;
|
|
145
|
+
}
|
|
146
|
+
if (!challengeSiteKey) return;
|
|
147
|
+
challengeTimeoutRef.current = setTimeout(() => {
|
|
148
|
+
setChallengeSiteKey(null);
|
|
149
|
+
pendingMethodIdRef.current = null;
|
|
150
|
+
pendingInputRef.current = void 0;
|
|
151
|
+
setError(new Error("Security check timed out. Please try again."));
|
|
152
|
+
setActiveMethodId(null);
|
|
153
|
+
}, 15e3);
|
|
154
|
+
return () => {
|
|
155
|
+
if (challengeTimeoutRef.current) {
|
|
156
|
+
clearTimeout(challengeTimeoutRef.current);
|
|
157
|
+
challengeTimeoutRef.current = null;
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
}, [challengeSiteKey]);
|
|
161
|
+
const availableMethods = (0, import_react.useMemo)(() => {
|
|
162
|
+
return getAvailableAuthMethods(methods);
|
|
163
|
+
}, [methods]);
|
|
164
|
+
const clearFeedback = (0, import_react.useCallback)(() => {
|
|
165
|
+
setError(null);
|
|
166
|
+
setNotice(null);
|
|
167
|
+
setChallengeSiteKey(null);
|
|
168
|
+
auth.clearError();
|
|
169
|
+
}, [auth]);
|
|
170
|
+
const startMethod = (0, import_react.useCallback)(
|
|
171
|
+
(methodId, input) => __async(null, null, function* () {
|
|
172
|
+
const method = methods.find((candidate) => candidate.id === methodId);
|
|
173
|
+
if (!method || !method.isAvailable) {
|
|
174
|
+
const unavailableError = new Error(`Auth method "${methodId}" is not available`);
|
|
175
|
+
setError(unavailableError);
|
|
176
|
+
throw unavailableError;
|
|
177
|
+
}
|
|
178
|
+
setError(null);
|
|
179
|
+
setNotice(null);
|
|
180
|
+
setActiveMethodId(methodId);
|
|
181
|
+
setSelectedMethodId(methodId);
|
|
182
|
+
try {
|
|
183
|
+
const result = yield method.start(
|
|
184
|
+
{
|
|
185
|
+
signIn: auth.signIn,
|
|
186
|
+
startAuth: auth.startAuth,
|
|
187
|
+
setSession: auth.setSession
|
|
188
|
+
},
|
|
189
|
+
input
|
|
190
|
+
);
|
|
191
|
+
if (result == null ? void 0 : result.notice) {
|
|
192
|
+
setNotice(result.notice);
|
|
193
|
+
}
|
|
194
|
+
return {};
|
|
195
|
+
} catch (startError) {
|
|
196
|
+
if (startError instanceof import_hooks.TurnstileChallengeError) {
|
|
197
|
+
pendingMethodIdRef.current = methodId;
|
|
198
|
+
pendingInputRef.current = input;
|
|
199
|
+
setChallengeSiteKey(startError.siteKey);
|
|
200
|
+
setActiveMethodId(null);
|
|
201
|
+
auth.clearError();
|
|
202
|
+
return { challengePending: true };
|
|
203
|
+
}
|
|
204
|
+
const resolvedError = normalizeAuthError(toError(startError));
|
|
205
|
+
setError(resolvedError);
|
|
206
|
+
throw resolvedError;
|
|
207
|
+
} finally {
|
|
208
|
+
setActiveMethodId(null);
|
|
209
|
+
}
|
|
210
|
+
}),
|
|
211
|
+
[auth, methods]
|
|
212
|
+
);
|
|
213
|
+
const handleTurnstileVerify = (0, import_react.useCallback)(
|
|
214
|
+
(token) => __async(null, null, function* () {
|
|
215
|
+
const methodId = pendingMethodIdRef.current;
|
|
216
|
+
const input = pendingInputRef.current;
|
|
217
|
+
if (!methodId) return;
|
|
218
|
+
setChallengeSiteKey(null);
|
|
219
|
+
pendingMethodIdRef.current = null;
|
|
220
|
+
pendingInputRef.current = void 0;
|
|
221
|
+
const method = methods.find((candidate) => candidate.id === methodId);
|
|
222
|
+
if (!method || !method.isAvailable) return;
|
|
223
|
+
setError(null);
|
|
224
|
+
setActiveMethodId(methodId);
|
|
225
|
+
try {
|
|
226
|
+
const result = yield method.start(
|
|
227
|
+
{
|
|
228
|
+
signIn: (options) => auth.signIn(__spreadProps(__spreadValues({}, options), { turnstileToken: token })),
|
|
229
|
+
startAuth: (options) => auth.startAuth(__spreadProps(__spreadValues({}, options), { turnstileToken: token })),
|
|
230
|
+
setSession: auth.setSession
|
|
231
|
+
},
|
|
232
|
+
input
|
|
233
|
+
);
|
|
234
|
+
if (result == null ? void 0 : result.notice) {
|
|
235
|
+
setNotice(result.notice);
|
|
236
|
+
}
|
|
237
|
+
} catch (retryError) {
|
|
238
|
+
setError(normalizeAuthError(toError(retryError)));
|
|
239
|
+
} finally {
|
|
240
|
+
setActiveMethodId(null);
|
|
241
|
+
}
|
|
242
|
+
}),
|
|
243
|
+
[auth, methods]
|
|
244
|
+
);
|
|
245
|
+
const handleTurnstileError = (0, import_react.useCallback)(() => {
|
|
246
|
+
setChallengeSiteKey(null);
|
|
247
|
+
pendingMethodIdRef.current = null;
|
|
248
|
+
pendingInputRef.current = void 0;
|
|
249
|
+
setError(new Error("Bot protection check failed. Please try again."));
|
|
250
|
+
setActiveMethodId(null);
|
|
251
|
+
}, []);
|
|
252
|
+
const signOut = (0, import_react.useCallback)(() => __async(null, null, function* () {
|
|
253
|
+
setError(null);
|
|
254
|
+
setNotice(null);
|
|
255
|
+
setChallengeSiteKey(null);
|
|
256
|
+
yield auth.signOut();
|
|
257
|
+
yield Promise.allSettled(
|
|
258
|
+
methods.map((method) => __async(null, null, function* () {
|
|
259
|
+
if (!method.disconnect) return;
|
|
260
|
+
yield method.disconnect();
|
|
261
|
+
}))
|
|
262
|
+
);
|
|
263
|
+
setSelectedMethodId(null);
|
|
264
|
+
}), [auth, methods]);
|
|
265
|
+
const value = (0, import_react.useMemo)(() => {
|
|
266
|
+
return {
|
|
267
|
+
activeMethodId,
|
|
268
|
+
availableMethods,
|
|
269
|
+
challengeSiteKey,
|
|
270
|
+
configuredMethods: methods,
|
|
271
|
+
error: error != null ? error : auth.error ? normalizeAuthError(auth.error) : null,
|
|
272
|
+
isBusy: auth.isLoading || activeMethodId !== null,
|
|
273
|
+
notice,
|
|
274
|
+
onChallengeError: handleTurnstileError,
|
|
275
|
+
onChallengeVerify: handleTurnstileVerify,
|
|
276
|
+
selectedMethodId,
|
|
277
|
+
startMethod,
|
|
278
|
+
clearFeedback,
|
|
279
|
+
signOut
|
|
280
|
+
};
|
|
281
|
+
}, [
|
|
282
|
+
activeMethodId,
|
|
283
|
+
auth.error,
|
|
284
|
+
auth.isLoading,
|
|
285
|
+
availableMethods,
|
|
286
|
+
challengeSiteKey,
|
|
287
|
+
clearFeedback,
|
|
288
|
+
error,
|
|
289
|
+
handleTurnstileError,
|
|
290
|
+
handleTurnstileVerify,
|
|
291
|
+
methods,
|
|
292
|
+
notice,
|
|
293
|
+
selectedMethodId,
|
|
294
|
+
signOut,
|
|
295
|
+
startMethod
|
|
296
|
+
]);
|
|
297
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(AggAuthFlowContext.Provider, { value, children });
|
|
298
|
+
};
|
|
299
|
+
var useAggAuthFlow = () => {
|
|
300
|
+
const context = (0, import_react.useContext)(AggAuthFlowContext);
|
|
301
|
+
if (!context) {
|
|
302
|
+
throw new Error("useAggAuthFlow must be used within an <AggAuthProvider>");
|
|
303
|
+
}
|
|
304
|
+
return context;
|
|
305
|
+
};
|
|
306
|
+
|
|
307
|
+
// src/callback/index.tsx
|
|
308
|
+
var import_react2 = require("react");
|
|
309
|
+
var import_hooks2 = require("@agg-build/hooks");
|
|
310
|
+
var useAggAuthCallback = (options = {}) => {
|
|
311
|
+
const { callbackUrl, enabled = true } = options;
|
|
312
|
+
const auth = (0, import_hooks2.useAggAuthContext)();
|
|
313
|
+
const handledRef = (0, import_react2.useRef)(false);
|
|
314
|
+
const [error, setError] = (0, import_react2.useState)(null);
|
|
315
|
+
const [isHandled, setIsHandled] = (0, import_react2.useState)(false);
|
|
316
|
+
const [isHandling, setIsHandling] = (0, import_react2.useState)(false);
|
|
317
|
+
const [user, setUser] = (0, import_react2.useState)(null);
|
|
318
|
+
(0, import_react2.useEffect)(() => {
|
|
319
|
+
if (!enabled || handledRef.current) return;
|
|
320
|
+
handledRef.current = true;
|
|
321
|
+
setIsHandling(true);
|
|
322
|
+
void auth.handleAuthCallback(callbackUrl).then((resolvedUser) => {
|
|
323
|
+
setUser(resolvedUser);
|
|
324
|
+
setIsHandled(resolvedUser !== null);
|
|
325
|
+
}).catch((callbackError) => {
|
|
326
|
+
setError(toError(callbackError));
|
|
327
|
+
}).finally(() => {
|
|
328
|
+
setIsHandling(false);
|
|
329
|
+
});
|
|
330
|
+
}, [auth, callbackUrl, enabled]);
|
|
331
|
+
return {
|
|
332
|
+
error,
|
|
333
|
+
isHandled,
|
|
334
|
+
isHandling,
|
|
335
|
+
user
|
|
336
|
+
};
|
|
337
|
+
};
|
|
338
|
+
|
|
339
|
+
// src/connect-button/index.tsx
|
|
340
|
+
var import_react4 = require("react");
|
|
341
|
+
var import_ui = require("@agg-build/ui");
|
|
342
|
+
var import_hooks4 = require("@agg-build/hooks");
|
|
343
|
+
|
|
344
|
+
// src/turnstile/index.tsx
|
|
345
|
+
var import_react3 = require("react");
|
|
346
|
+
var import_react_turnstile = require("@marsidev/react-turnstile");
|
|
347
|
+
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
348
|
+
var TurnstileErrorBoundary = class extends import_react3.Component {
|
|
349
|
+
constructor() {
|
|
350
|
+
super(...arguments);
|
|
351
|
+
this.state = { hasError: false };
|
|
352
|
+
}
|
|
353
|
+
static getDerivedStateFromError() {
|
|
354
|
+
return { hasError: true };
|
|
355
|
+
}
|
|
356
|
+
componentDidCatch(_error, _info) {
|
|
357
|
+
var _a, _b;
|
|
358
|
+
(_b = (_a = this.props).onError) == null ? void 0 : _b.call(_a);
|
|
359
|
+
}
|
|
360
|
+
render() {
|
|
361
|
+
if (this.state.hasError) return null;
|
|
362
|
+
return this.props.children;
|
|
363
|
+
}
|
|
364
|
+
};
|
|
365
|
+
var TurnstileWidget = ({
|
|
366
|
+
siteKey,
|
|
367
|
+
onVerify,
|
|
368
|
+
onError,
|
|
369
|
+
onExpire
|
|
370
|
+
}) => {
|
|
371
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(TurnstileErrorBoundary, { onError, children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
|
|
372
|
+
import_react_turnstile.Turnstile,
|
|
373
|
+
{
|
|
374
|
+
siteKey,
|
|
375
|
+
onSuccess: onVerify,
|
|
376
|
+
onError,
|
|
377
|
+
onExpire,
|
|
378
|
+
options: { appearance: "always", theme: "auto" }
|
|
379
|
+
}
|
|
380
|
+
) });
|
|
381
|
+
};
|
|
382
|
+
|
|
383
|
+
// src/connect-button/connect-button.utils.ts
|
|
384
|
+
var import_hooks3 = require("@agg-build/hooks");
|
|
385
|
+
var getAuthProfileLabel = (user) => {
|
|
386
|
+
var _a, _b;
|
|
387
|
+
if (user == null ? void 0 : user.username) return user.username;
|
|
388
|
+
const walletAddress = (0, import_hooks3.getWalletAddressFromUserProfile)(user);
|
|
389
|
+
if (walletAddress) return shortenAddress(walletAddress);
|
|
390
|
+
const providerAccountId = (_b = (_a = user == null ? void 0 : user.accounts) == null ? void 0 : _a[0]) == null ? void 0 : _b.providerAccountId;
|
|
391
|
+
if (providerAccountId) return shortenProfileLabel(providerAccountId);
|
|
392
|
+
if (user == null ? void 0 : user.id) return shortenProfileLabel(user.id);
|
|
393
|
+
return "";
|
|
394
|
+
};
|
|
395
|
+
var socialOrder = {
|
|
396
|
+
google: 0,
|
|
397
|
+
apple: 1,
|
|
398
|
+
twitter: 2
|
|
399
|
+
};
|
|
400
|
+
var walletOrder = {
|
|
401
|
+
siws: 0,
|
|
402
|
+
siwe: 1
|
|
403
|
+
};
|
|
404
|
+
var toViewMethod = (method) => {
|
|
405
|
+
return {
|
|
406
|
+
chain: method.chain,
|
|
407
|
+
description: method.description,
|
|
408
|
+
iconName: method.iconName,
|
|
409
|
+
id: method.id,
|
|
410
|
+
input: method.input,
|
|
411
|
+
isAvailable: method.isAvailable,
|
|
412
|
+
kind: method.kind,
|
|
413
|
+
label: method.label
|
|
414
|
+
};
|
|
415
|
+
};
|
|
416
|
+
var groupConnectButtonMethods = (methods) => {
|
|
417
|
+
var _a;
|
|
418
|
+
const emailMethod = (_a = methods.find((method) => method.kind === "email")) != null ? _a : null;
|
|
419
|
+
const socialMethods = methods.filter((method) => method.kind === "social").slice().sort((left, right) => {
|
|
420
|
+
var _a2, _b;
|
|
421
|
+
return ((_a2 = socialOrder[left.id]) != null ? _a2 : 99) - ((_b = socialOrder[right.id]) != null ? _b : 99);
|
|
422
|
+
}).map(toViewMethod);
|
|
423
|
+
const walletMethods = methods.filter((method) => method.kind === "wallet").slice().sort((left, right) => {
|
|
424
|
+
var _a2, _b;
|
|
425
|
+
return ((_a2 = walletOrder[left.id]) != null ? _a2 : 99) - ((_b = walletOrder[right.id]) != null ? _b : 99);
|
|
426
|
+
}).map(toViewMethod);
|
|
427
|
+
return {
|
|
428
|
+
emailMethod: emailMethod ? toViewMethod(emailMethod) : null,
|
|
429
|
+
socialMethods,
|
|
430
|
+
walletMethods
|
|
431
|
+
};
|
|
432
|
+
};
|
|
433
|
+
|
|
434
|
+
// src/connect-button/index.tsx
|
|
435
|
+
var import_jsx_runtime3 = require("react/jsx-runtime");
|
|
436
|
+
var ConnectButton = ({
|
|
437
|
+
buttonProps,
|
|
438
|
+
classNames,
|
|
439
|
+
disabled,
|
|
440
|
+
onAuthMethodSelect,
|
|
441
|
+
onDepositClick,
|
|
442
|
+
onDisconnect,
|
|
443
|
+
onProfileCardClick,
|
|
444
|
+
onProfileClick,
|
|
445
|
+
onClaimWinningsClick,
|
|
446
|
+
claimReadyCount,
|
|
447
|
+
onWithdrawClick
|
|
448
|
+
}) => {
|
|
449
|
+
var _a, _b;
|
|
450
|
+
const [emailValue, setEmailValue] = (0, import_react4.useState)("");
|
|
451
|
+
const [emailError, setEmailError] = (0, import_react4.useState)(null);
|
|
452
|
+
const [isChooserOpen, setIsChooserOpen] = (0, import_react4.useState)(false);
|
|
453
|
+
const [isProfileMenuOpen, setIsProfileMenuOpen] = (0, import_react4.useState)(false);
|
|
454
|
+
const {
|
|
455
|
+
activeMethodId,
|
|
456
|
+
challengeSiteKey,
|
|
457
|
+
clearFeedback,
|
|
458
|
+
configuredMethods,
|
|
459
|
+
error,
|
|
460
|
+
isBusy,
|
|
461
|
+
notice,
|
|
462
|
+
onChallengeError,
|
|
463
|
+
onChallengeVerify,
|
|
464
|
+
signOut,
|
|
465
|
+
startMethod
|
|
466
|
+
} = useAggAuthFlow();
|
|
467
|
+
const { isAuthenticated, user } = (0, import_hooks4.useAggAuthContext)();
|
|
468
|
+
const {
|
|
469
|
+
totalBalance,
|
|
470
|
+
positionsBalanceTotal,
|
|
471
|
+
isLoading: isBalanceLoading,
|
|
472
|
+
error: balanceError
|
|
473
|
+
} = (0, import_hooks4.useAggBalanceState)();
|
|
474
|
+
const { isDepositBlocked } = (0, import_hooks4.useGeoBlock)();
|
|
475
|
+
const resolvedWalletAddress = (_a = (0, import_hooks4.getWalletAddressFromUserProfile)(user)) != null ? _a : "";
|
|
476
|
+
const resolvedAddressLabel = resolvedWalletAddress ? shortenAddress(resolvedWalletAddress) : void 0;
|
|
477
|
+
const resolvedProfileLabel = getAuthProfileLabel(user) || void 0;
|
|
478
|
+
const { emailMethod, socialMethods, walletMethods } = (0, import_react4.useMemo)(() => {
|
|
479
|
+
return groupConnectButtonMethods(configuredMethods);
|
|
480
|
+
}, [configuredMethods]);
|
|
481
|
+
(0, import_react4.useEffect)(() => {
|
|
482
|
+
if (!isAuthenticated) return;
|
|
483
|
+
setIsChooserOpen(false);
|
|
484
|
+
setEmailError(null);
|
|
485
|
+
}, [isAuthenticated]);
|
|
486
|
+
(0, import_react4.useEffect)(() => {
|
|
487
|
+
if (typeof window === "undefined") return;
|
|
488
|
+
const handleOpenChooserRequest = () => {
|
|
489
|
+
if (disabled || isAuthenticated) return;
|
|
490
|
+
setEmailError(null);
|
|
491
|
+
clearFeedback();
|
|
492
|
+
setIsChooserOpen(true);
|
|
493
|
+
};
|
|
494
|
+
window.addEventListener(import_hooks4.AUTH_CHOOSER_OPEN_EVENT, handleOpenChooserRequest);
|
|
495
|
+
return () => {
|
|
496
|
+
window.removeEventListener(import_hooks4.AUTH_CHOOSER_OPEN_EVENT, handleOpenChooserRequest);
|
|
497
|
+
};
|
|
498
|
+
}, [clearFeedback, disabled, isAuthenticated]);
|
|
499
|
+
const resolvedError = error != null ? error : balanceError;
|
|
500
|
+
const challengeContent = challengeSiteKey ? /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("div", { className: "flex flex-col items-center gap-3", children: [
|
|
501
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("p", { className: "text-agg-sm text-agg-muted-foreground text-center", children: "Please complete the verification below to continue." }),
|
|
502
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
|
|
503
|
+
TurnstileWidget,
|
|
504
|
+
{
|
|
505
|
+
siteKey: challengeSiteKey,
|
|
506
|
+
onVerify: onChallengeVerify,
|
|
507
|
+
onError: onChallengeError,
|
|
508
|
+
onExpire: onChallengeError
|
|
509
|
+
}
|
|
510
|
+
)
|
|
511
|
+
] }) : null;
|
|
512
|
+
const handleMethodSelection = (method) => __async(null, null, function* () {
|
|
513
|
+
if (!method.isAvailable || disabled || isBusy) return;
|
|
514
|
+
setEmailError(null);
|
|
515
|
+
clearFeedback();
|
|
516
|
+
setIsChooserOpen(false);
|
|
517
|
+
let result;
|
|
518
|
+
try {
|
|
519
|
+
result = yield startMethod(method.id);
|
|
520
|
+
} catch (e) {
|
|
521
|
+
setIsChooserOpen(true);
|
|
522
|
+
return;
|
|
523
|
+
}
|
|
524
|
+
if (result == null ? void 0 : result.challengePending) return;
|
|
525
|
+
yield Promise.resolve(onAuthMethodSelect == null ? void 0 : onAuthMethodSelect(method.id));
|
|
526
|
+
setIsChooserOpen(false);
|
|
527
|
+
});
|
|
528
|
+
const handleEmailSubmit = () => __async(null, null, function* () {
|
|
529
|
+
var _a2, _b2, _c;
|
|
530
|
+
if (!emailMethod || !emailMethod.isAvailable || disabled || isBusy) return;
|
|
531
|
+
const parsedEmail = (0, import_hooks4.parseEmail)(emailValue);
|
|
532
|
+
if (!parsedEmail.success) {
|
|
533
|
+
setEmailError((_c = (_b2 = (_a2 = parsedEmail.error) == null ? void 0 : _a2.issues[0]) == null ? void 0 : _b2.message) != null ? _c : "Enter a valid email address.");
|
|
534
|
+
return;
|
|
535
|
+
}
|
|
536
|
+
setEmailError(null);
|
|
537
|
+
const result = yield startMethod(emailMethod.id, {
|
|
538
|
+
email: parsedEmail.data.email
|
|
539
|
+
});
|
|
540
|
+
if (result == null ? void 0 : result.challengePending) return;
|
|
541
|
+
yield Promise.resolve(onAuthMethodSelect == null ? void 0 : onAuthMethodSelect(emailMethod.id));
|
|
542
|
+
setEmailValue("");
|
|
543
|
+
setIsChooserOpen(false);
|
|
544
|
+
});
|
|
545
|
+
const handleDepositClick = () => {
|
|
546
|
+
if (disabled) return;
|
|
547
|
+
setIsProfileMenuOpen(false);
|
|
548
|
+
void Promise.resolve(onDepositClick == null ? void 0 : onDepositClick());
|
|
549
|
+
};
|
|
550
|
+
const handleProfileCardClick = () => {
|
|
551
|
+
if (disabled) return;
|
|
552
|
+
setIsProfileMenuOpen(false);
|
|
553
|
+
void Promise.resolve(onProfileCardClick == null ? void 0 : onProfileCardClick());
|
|
554
|
+
};
|
|
555
|
+
const handleProfileClick = () => {
|
|
556
|
+
if (disabled) return;
|
|
557
|
+
setIsProfileMenuOpen(false);
|
|
558
|
+
void Promise.resolve(onProfileClick == null ? void 0 : onProfileClick());
|
|
559
|
+
};
|
|
560
|
+
const handleClaimWinningsClick = () => {
|
|
561
|
+
if (disabled) return;
|
|
562
|
+
setIsProfileMenuOpen(false);
|
|
563
|
+
void Promise.resolve(onClaimWinningsClick == null ? void 0 : onClaimWinningsClick());
|
|
564
|
+
};
|
|
565
|
+
const handleWithdrawClick = () => {
|
|
566
|
+
if (disabled) return;
|
|
567
|
+
setIsProfileMenuOpen(false);
|
|
568
|
+
void Promise.resolve(onWithdrawClick == null ? void 0 : onWithdrawClick());
|
|
569
|
+
};
|
|
570
|
+
const handleDisconnectClick = () => __async(null, null, function* () {
|
|
571
|
+
if (disabled) return;
|
|
572
|
+
setIsProfileMenuOpen(false);
|
|
573
|
+
yield signOut();
|
|
574
|
+
yield Promise.resolve(onDisconnect == null ? void 0 : onDisconnect());
|
|
575
|
+
});
|
|
576
|
+
const handleChooserOpenChange = (open) => {
|
|
577
|
+
if (disabled) return;
|
|
578
|
+
setIsChooserOpen(open);
|
|
579
|
+
if (open) return;
|
|
580
|
+
setEmailError(null);
|
|
581
|
+
clearFeedback();
|
|
582
|
+
};
|
|
583
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
|
|
584
|
+
import_ui.AuthConnectButtonView,
|
|
585
|
+
{
|
|
586
|
+
activeMethodId,
|
|
587
|
+
addressLabel: resolvedAddressLabel,
|
|
588
|
+
buttonProps,
|
|
589
|
+
challengeContent,
|
|
590
|
+
classNames,
|
|
591
|
+
disabled,
|
|
592
|
+
emailMethod,
|
|
593
|
+
emailError,
|
|
594
|
+
emailValue,
|
|
595
|
+
errorMessage: (_b = resolvedError == null ? void 0 : resolvedError.message) != null ? _b : null,
|
|
596
|
+
isAuthenticated,
|
|
597
|
+
isBalanceLoading,
|
|
598
|
+
isBusy,
|
|
599
|
+
isChooserOpen,
|
|
600
|
+
isProfileMenuOpen,
|
|
601
|
+
notice,
|
|
602
|
+
onChooserOpenChange: handleChooserOpenChange,
|
|
603
|
+
onDepositClick: handleDepositClick,
|
|
604
|
+
onDisconnectClick: () => {
|
|
605
|
+
void handleDisconnectClick();
|
|
606
|
+
},
|
|
607
|
+
onEmailChange: setEmailValue,
|
|
608
|
+
onEmailSubmit: () => {
|
|
609
|
+
void handleEmailSubmit();
|
|
610
|
+
},
|
|
611
|
+
onMethodSelect: (methodId) => {
|
|
612
|
+
const selectedMethod = [...socialMethods, ...walletMethods].find(
|
|
613
|
+
(method) => method.id === methodId
|
|
614
|
+
);
|
|
615
|
+
if (!selectedMethod) return;
|
|
616
|
+
void handleMethodSelection(selectedMethod);
|
|
617
|
+
},
|
|
618
|
+
onProfileCardClick: handleProfileCardClick,
|
|
619
|
+
onProfileClick: handleProfileClick,
|
|
620
|
+
onProfileMenuOpenChange: setIsProfileMenuOpen,
|
|
621
|
+
socialMethods,
|
|
622
|
+
onWithdrawClick: handleWithdrawClick,
|
|
623
|
+
profileLabel: resolvedProfileLabel,
|
|
624
|
+
totalBalance,
|
|
625
|
+
positionsBalance: positionsBalanceTotal,
|
|
626
|
+
claimReadyCount,
|
|
627
|
+
onClaimWinningsClick: handleClaimWinningsClick,
|
|
628
|
+
walletMethods,
|
|
629
|
+
isDepositBlocked
|
|
630
|
+
}
|
|
631
|
+
);
|
|
632
|
+
};
|
|
633
|
+
ConnectButton.displayName = "ConnectButton";
|
|
634
|
+
|
|
635
|
+
// src/email/index.ts
|
|
636
|
+
var import_hooks5 = require("@agg-build/hooks");
|
|
637
|
+
var createEmailAuthMethod = (options = {}) => {
|
|
638
|
+
var _a, _b, _c, _d, _e;
|
|
639
|
+
return {
|
|
640
|
+
id: "email",
|
|
641
|
+
kind: "email",
|
|
642
|
+
label: (_a = options.label) != null ? _a : "Email",
|
|
643
|
+
description: (_b = options.description) != null ? _b : "Get a magic link sent to your inbox.",
|
|
644
|
+
iconName: "email",
|
|
645
|
+
input: {
|
|
646
|
+
type: "email",
|
|
647
|
+
placeholder: (_c = options.placeholder) != null ? _c : "Email address",
|
|
648
|
+
submitLabel: (_d = options.submitLabel) != null ? _d : "Continue",
|
|
649
|
+
helperText: options.helperText
|
|
650
|
+
},
|
|
651
|
+
isAvailable: (_e = options.isAvailable) != null ? _e : true,
|
|
652
|
+
start: (_0, _1) => __async(null, [_0, _1], function* ({ startAuth }, input) {
|
|
653
|
+
var _a2, _b2;
|
|
654
|
+
const { email } = (0, import_hooks5.parseEmailStrict)((_a2 = input == null ? void 0 : input.email) != null ? _a2 : "");
|
|
655
|
+
const response = yield startAuth({
|
|
656
|
+
provider: "email",
|
|
657
|
+
email,
|
|
658
|
+
redirectUrl: resolveRedirectUrl(options.redirectUrl)
|
|
659
|
+
});
|
|
660
|
+
if (response.type === "challenge_required") {
|
|
661
|
+
if (!response.siteKey) {
|
|
662
|
+
throw new Error("Server returned challenge_required but no siteKey was provided.");
|
|
663
|
+
}
|
|
664
|
+
throw new import_hooks5.TurnstileChallengeError(response.siteKey);
|
|
665
|
+
}
|
|
666
|
+
if (response.type !== "magic_link") {
|
|
667
|
+
throw new Error(`AGG auth start returned an unexpected "${response.type}" response`);
|
|
668
|
+
}
|
|
669
|
+
return {
|
|
670
|
+
notice: (_b2 = options.successNotice) != null ? _b2 : "Check your email for a magic link to finish signing in."
|
|
671
|
+
};
|
|
672
|
+
})
|
|
673
|
+
};
|
|
674
|
+
};
|
|
675
|
+
|
|
676
|
+
// src/oauth/index.ts
|
|
677
|
+
var defaultNavigate = (url) => {
|
|
678
|
+
if (typeof window === "undefined") {
|
|
679
|
+
throw new Error("Redirect auth methods require a browser environment");
|
|
680
|
+
}
|
|
681
|
+
window.location.assign(url);
|
|
682
|
+
};
|
|
683
|
+
var createRedirectAuthMethod = (provider, defaults, options = {}) => {
|
|
684
|
+
var _a, _b, _c;
|
|
685
|
+
return {
|
|
686
|
+
id: provider,
|
|
687
|
+
kind: "social",
|
|
688
|
+
label: (_a = options.label) != null ? _a : defaults.label,
|
|
689
|
+
description: (_b = options.description) != null ? _b : defaults.description,
|
|
690
|
+
iconName: defaults.iconName,
|
|
691
|
+
isAvailable: (_c = options.isAvailable) != null ? _c : true,
|
|
692
|
+
start: (_0) => __async(null, [_0], function* ({ startAuth }) {
|
|
693
|
+
var _a2;
|
|
694
|
+
const response = yield startAuth({
|
|
695
|
+
provider,
|
|
696
|
+
redirectUrl: resolveRedirectUrl(options.redirectUrl)
|
|
697
|
+
});
|
|
698
|
+
if (response.type !== "redirect") {
|
|
699
|
+
throw new Error(`AGG auth start returned an unexpected "${response.type}" response`);
|
|
700
|
+
}
|
|
701
|
+
const navigate = (_a2 = options.navigate) != null ? _a2 : defaultNavigate;
|
|
702
|
+
navigate(response.url);
|
|
703
|
+
})
|
|
704
|
+
};
|
|
705
|
+
};
|
|
706
|
+
var createGoogleAuthMethod = (options = {}) => {
|
|
707
|
+
return createRedirectAuthMethod(
|
|
708
|
+
"google",
|
|
709
|
+
{
|
|
710
|
+
label: "Google",
|
|
711
|
+
description: "Continue with your Google account.",
|
|
712
|
+
iconName: "google"
|
|
713
|
+
},
|
|
714
|
+
options
|
|
715
|
+
);
|
|
716
|
+
};
|
|
717
|
+
var createTwitterAuthMethod = (options = {}) => {
|
|
718
|
+
return createRedirectAuthMethod(
|
|
719
|
+
"twitter",
|
|
720
|
+
{
|
|
721
|
+
label: "X",
|
|
722
|
+
description: "Continue with your X account.",
|
|
723
|
+
iconName: "twitter"
|
|
724
|
+
},
|
|
725
|
+
options
|
|
726
|
+
);
|
|
727
|
+
};
|
|
728
|
+
var createAppleAuthMethod = (options = {}) => {
|
|
729
|
+
return createRedirectAuthMethod(
|
|
730
|
+
"apple",
|
|
731
|
+
{
|
|
732
|
+
label: "Apple",
|
|
733
|
+
description: "Continue with Apple.",
|
|
734
|
+
iconName: "apple"
|
|
735
|
+
},
|
|
736
|
+
options
|
|
737
|
+
);
|
|
738
|
+
};
|
|
739
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
740
|
+
0 && (module.exports = {
|
|
741
|
+
AggAuthProvider,
|
|
742
|
+
ConnectButton,
|
|
743
|
+
createAppleAuthMethod,
|
|
744
|
+
createEmailAuthMethod,
|
|
745
|
+
createGoogleAuthMethod,
|
|
746
|
+
createTwitterAuthMethod,
|
|
747
|
+
useAggAuthCallback,
|
|
748
|
+
useAggAuthFlow
|
|
749
|
+
});
|