@bigio/better-auth-electron 1.0.4 → 1.0.5
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 +193 -50
- package/dist/main.js +19 -35
- package/dist/main.js.map +1 -1
- package/dist/metafile-esm.json +1 -1
- package/dist/options.d.ts +29 -10
- package/dist/options.js +23 -11
- package/dist/options.js.map +1 -1
- package/dist/renderer.d.ts +27 -2
- package/dist/renderer.js +292 -150
- package/dist/renderer.js.map +1 -1
- package/dist/server.d.ts +5 -0
- package/dist/server.js +46 -26
- package/dist/server.js.map +1 -1
- package/dist/web.d.ts +1 -0
- package/dist/web.js +505 -109
- package/dist/web.js.map +1 -1
- package/package.json +1 -1
package/dist/web.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { useRef, useCallback, useSyncExternalStore } from 'react';
|
|
2
|
-
import z from 'zod';
|
|
2
|
+
import z, { boolean } from 'zod';
|
|
3
3
|
|
|
4
4
|
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
5
5
|
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
@@ -111,14 +111,19 @@ function useStore(store, { keys, deps = [store, keys] } = {}) {
|
|
|
111
111
|
let get = () => snapshotRef.current;
|
|
112
112
|
return useSyncExternalStore(subscribe, get, get);
|
|
113
113
|
}
|
|
114
|
-
var
|
|
114
|
+
var defaultWebOptions = {
|
|
115
115
|
ELECTRON_SCHEME: "bigio",
|
|
116
116
|
SCHEME_NAME_IN_URL: "scheme",
|
|
117
117
|
PROVIDERS: ["github", "google"],
|
|
118
118
|
PROVIDER_NAME_IN_URL: "provider",
|
|
119
|
-
CHALLENGE_NAME_IN_URL: "
|
|
119
|
+
CHALLENGE_NAME_IN_URL: "electron-challenge",
|
|
120
120
|
WEB_OAUTH_SIGNIN_CALLBACK_PATHNAME: "electron-handoff",
|
|
121
|
-
BACKEND_FAST_TICKET_URL: "electron/fastTicket"
|
|
121
|
+
BACKEND_FAST_TICKET_URL: "electron/fastTicket",
|
|
122
|
+
SCOPES_NAME_IN_URL: "electron-scopes",
|
|
123
|
+
LOGINHINT_NAME_IN_URL: "electron-loginhint",
|
|
124
|
+
ADDITIONAL_DATA_NAME_IN_URL: "electron-addata",
|
|
125
|
+
REQUEST_SIGN_UP_NAME_IN_URL: "electron-reqsignup",
|
|
126
|
+
AUTH_STATUS_NAME_IN_URL: "electron-status"
|
|
122
127
|
};
|
|
123
128
|
|
|
124
129
|
// node_modules/.pnpm/@oslojs+encoding@1.1.0/node_modules/@oslojs/encoding/dist/base32.js
|
|
@@ -134,6 +139,52 @@ var DecodingPadding;
|
|
|
134
139
|
})(DecodingPadding || (DecodingPadding = {}));
|
|
135
140
|
|
|
136
141
|
// node_modules/.pnpm/@oslojs+encoding@1.1.0/node_modules/@oslojs/encoding/dist/base64.js
|
|
142
|
+
function decodeBase64urlIgnorePadding(encoded) {
|
|
143
|
+
return decodeBase64_internal(encoded, base64urlDecodeMap, DecodingPadding2.Ignore);
|
|
144
|
+
}
|
|
145
|
+
function decodeBase64_internal(encoded, decodeMap, padding) {
|
|
146
|
+
const result = new Uint8Array(Math.ceil(encoded.length / 4) * 3);
|
|
147
|
+
let totalBytes = 0;
|
|
148
|
+
for (let i = 0; i < encoded.length; i += 4) {
|
|
149
|
+
let chunk = 0;
|
|
150
|
+
let bitsRead = 0;
|
|
151
|
+
for (let j = 0; j < 4; j++) {
|
|
152
|
+
if (padding === DecodingPadding2.Required && encoded[i + j] === "=") {
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
if (padding === DecodingPadding2.Ignore && (i + j >= encoded.length || encoded[i + j] === "=")) {
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
if (j > 0 && encoded[i + j - 1] === "=") {
|
|
159
|
+
throw new Error("Invalid padding");
|
|
160
|
+
}
|
|
161
|
+
if (!(encoded[i + j] in decodeMap)) {
|
|
162
|
+
throw new Error("Invalid character");
|
|
163
|
+
}
|
|
164
|
+
chunk |= decodeMap[encoded[i + j]] << (3 - j) * 6;
|
|
165
|
+
bitsRead += 6;
|
|
166
|
+
}
|
|
167
|
+
if (bitsRead < 24) {
|
|
168
|
+
let unused;
|
|
169
|
+
if (bitsRead === 12) {
|
|
170
|
+
unused = chunk & 65535;
|
|
171
|
+
} else if (bitsRead === 18) {
|
|
172
|
+
unused = chunk & 255;
|
|
173
|
+
} else {
|
|
174
|
+
throw new Error("Invalid padding");
|
|
175
|
+
}
|
|
176
|
+
if (unused !== 0) {
|
|
177
|
+
throw new Error("Invalid padding");
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
const byteLength = Math.floor(bitsRead / 8);
|
|
181
|
+
for (let i2 = 0; i2 < byteLength; i2++) {
|
|
182
|
+
result[totalBytes] = chunk >> 16 - i2 * 8 & 255;
|
|
183
|
+
totalBytes++;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
return result.slice(0, totalBytes);
|
|
187
|
+
}
|
|
137
188
|
var EncodingPadding2;
|
|
138
189
|
(function(EncodingPadding3) {
|
|
139
190
|
EncodingPadding3[EncodingPadding3["Include"] = 0] = "Include";
|
|
@@ -144,6 +195,72 @@ var DecodingPadding2;
|
|
|
144
195
|
DecodingPadding3[DecodingPadding3["Required"] = 0] = "Required";
|
|
145
196
|
DecodingPadding3[DecodingPadding3["Ignore"] = 1] = "Ignore";
|
|
146
197
|
})(DecodingPadding2 || (DecodingPadding2 = {}));
|
|
198
|
+
var base64urlDecodeMap = {
|
|
199
|
+
"0": 52,
|
|
200
|
+
"1": 53,
|
|
201
|
+
"2": 54,
|
|
202
|
+
"3": 55,
|
|
203
|
+
"4": 56,
|
|
204
|
+
"5": 57,
|
|
205
|
+
"6": 58,
|
|
206
|
+
"7": 59,
|
|
207
|
+
"8": 60,
|
|
208
|
+
"9": 61,
|
|
209
|
+
A: 0,
|
|
210
|
+
B: 1,
|
|
211
|
+
C: 2,
|
|
212
|
+
D: 3,
|
|
213
|
+
E: 4,
|
|
214
|
+
F: 5,
|
|
215
|
+
G: 6,
|
|
216
|
+
H: 7,
|
|
217
|
+
I: 8,
|
|
218
|
+
J: 9,
|
|
219
|
+
K: 10,
|
|
220
|
+
L: 11,
|
|
221
|
+
M: 12,
|
|
222
|
+
N: 13,
|
|
223
|
+
O: 14,
|
|
224
|
+
P: 15,
|
|
225
|
+
Q: 16,
|
|
226
|
+
R: 17,
|
|
227
|
+
S: 18,
|
|
228
|
+
T: 19,
|
|
229
|
+
U: 20,
|
|
230
|
+
V: 21,
|
|
231
|
+
W: 22,
|
|
232
|
+
X: 23,
|
|
233
|
+
Y: 24,
|
|
234
|
+
Z: 25,
|
|
235
|
+
a: 26,
|
|
236
|
+
b: 27,
|
|
237
|
+
c: 28,
|
|
238
|
+
d: 29,
|
|
239
|
+
e: 30,
|
|
240
|
+
f: 31,
|
|
241
|
+
g: 32,
|
|
242
|
+
h: 33,
|
|
243
|
+
i: 34,
|
|
244
|
+
j: 35,
|
|
245
|
+
k: 36,
|
|
246
|
+
l: 37,
|
|
247
|
+
m: 38,
|
|
248
|
+
n: 39,
|
|
249
|
+
o: 40,
|
|
250
|
+
p: 41,
|
|
251
|
+
q: 42,
|
|
252
|
+
r: 43,
|
|
253
|
+
s: 44,
|
|
254
|
+
t: 45,
|
|
255
|
+
u: 46,
|
|
256
|
+
v: 47,
|
|
257
|
+
w: 48,
|
|
258
|
+
x: 49,
|
|
259
|
+
y: 50,
|
|
260
|
+
z: 51,
|
|
261
|
+
"-": 62,
|
|
262
|
+
_: 63
|
|
263
|
+
};
|
|
147
264
|
|
|
148
265
|
// src/utils/electron-plugin-env.ts
|
|
149
266
|
var getGlobal = () => {
|
|
@@ -172,6 +289,7 @@ var getRawNodeEnv = () => {
|
|
|
172
289
|
return "development";
|
|
173
290
|
};
|
|
174
291
|
var RAW_ENV = getRawNodeEnv();
|
|
292
|
+
var IS_DEV = RAW_ENV === "development";
|
|
175
293
|
var IS_ELECTRON_PACKAGED = (() => {
|
|
176
294
|
if (typeof process !== "undefined" && process.versions && process.versions.electron) {
|
|
177
295
|
try {
|
|
@@ -209,6 +327,34 @@ var BigIOError = class extends Error {
|
|
|
209
327
|
};
|
|
210
328
|
}
|
|
211
329
|
};
|
|
330
|
+
var consoleError = (originalError, message, bigioErrorStack) => {
|
|
331
|
+
if (!IS_DEV) {
|
|
332
|
+
return;
|
|
333
|
+
}
|
|
334
|
+
if (message && bigioErrorStack) {
|
|
335
|
+
console.groupCollapsed(`[BigIOError]`);
|
|
336
|
+
console.error("[BigIOError] message: ", message);
|
|
337
|
+
if (Array.isArray(bigioErrorStack) && bigioErrorStack.length > 0) {
|
|
338
|
+
console.log("[BigIOError] Stack Trace Table: ");
|
|
339
|
+
console.table(bigioErrorStack);
|
|
340
|
+
}
|
|
341
|
+
console.error("[BigIOError] Original Error: ", originalError);
|
|
342
|
+
console.groupEnd();
|
|
343
|
+
} else if (originalError instanceof BigIOError) {
|
|
344
|
+
console.groupCollapsed(`[BigIOError]`);
|
|
345
|
+
console.error("[BigIOError] message: ", originalError.message);
|
|
346
|
+
if (Array.isArray(originalError.bigioErrorStack) && originalError.bigioErrorStack.length > 0) {
|
|
347
|
+
console.log("[BigIOError] Stack Trace Table: ");
|
|
348
|
+
console.table(originalError.bigioErrorStack);
|
|
349
|
+
}
|
|
350
|
+
console.error("[BigIOError] Original Error: ", originalError.cause);
|
|
351
|
+
console.groupEnd();
|
|
352
|
+
} else {
|
|
353
|
+
console.groupCollapsed(`[Error]`);
|
|
354
|
+
console.error("[Error]: ", originalError);
|
|
355
|
+
console.groupEnd();
|
|
356
|
+
}
|
|
357
|
+
};
|
|
212
358
|
function getPreviousBigIOErrorStack(error) {
|
|
213
359
|
if (error instanceof BigIOError) {
|
|
214
360
|
return error.bigioErrorStack;
|
|
@@ -225,15 +371,41 @@ function okOr(value, errorMessage) {
|
|
|
225
371
|
const now = Date.now();
|
|
226
372
|
const ctx = (() => {
|
|
227
373
|
const DEFAULT_CTX = "okOr function failed";
|
|
228
|
-
{
|
|
374
|
+
if (!(errorMessage instanceof Error) && typeof errorMessage === "object" && "ctx" in errorMessage) {
|
|
375
|
+
return errorMessage.ctx;
|
|
376
|
+
}
|
|
377
|
+
if (typeof errorMessage === "string") {
|
|
378
|
+
return DEFAULT_CTX;
|
|
379
|
+
}
|
|
380
|
+
if (errorMessage === void 0) {
|
|
381
|
+
return DEFAULT_CTX;
|
|
382
|
+
}
|
|
383
|
+
if (errorMessage instanceof Error) {
|
|
384
|
+
return DEFAULT_CTX;
|
|
385
|
+
}
|
|
386
|
+
if (errorMessage === true) {
|
|
229
387
|
return DEFAULT_CTX;
|
|
230
388
|
}
|
|
389
|
+
return DEFAULT_CTX;
|
|
231
390
|
})();
|
|
232
391
|
const msg = (() => {
|
|
233
392
|
const DEFAULT_MSG = "Unexpected return null or undefined value";
|
|
234
|
-
{
|
|
393
|
+
if (!(errorMessage instanceof Error) && typeof errorMessage === "object" && "msg" in errorMessage && typeof errorMessage.msg === "string") {
|
|
394
|
+
return errorMessage.msg;
|
|
395
|
+
}
|
|
396
|
+
if (typeof errorMessage === "string") {
|
|
397
|
+
return errorMessage;
|
|
398
|
+
}
|
|
399
|
+
if (errorMessage === void 0) {
|
|
400
|
+
return DEFAULT_MSG;
|
|
401
|
+
}
|
|
402
|
+
if (errorMessage instanceof Error) {
|
|
403
|
+
return DEFAULT_MSG;
|
|
404
|
+
}
|
|
405
|
+
if (errorMessage === true) {
|
|
235
406
|
return DEFAULT_MSG;
|
|
236
407
|
}
|
|
408
|
+
return DEFAULT_MSG;
|
|
237
409
|
})();
|
|
238
410
|
const newItem = {
|
|
239
411
|
msg,
|
|
@@ -275,6 +447,114 @@ function okOr(value, errorMessage) {
|
|
|
275
447
|
bigioErrorStack: [newItem]
|
|
276
448
|
});
|
|
277
449
|
}
|
|
450
|
+
var handleUnsafeError = (originalError, errorMessage) => {
|
|
451
|
+
const now = Date.now();
|
|
452
|
+
const prevStack = getPreviousBigIOErrorStack(originalError);
|
|
453
|
+
const originalErrorMsg = originalError instanceof Error ? originalError.message : String(originalError);
|
|
454
|
+
const newMsg = (() => {
|
|
455
|
+
if (errorMessage === true) {
|
|
456
|
+
return originalErrorMsg;
|
|
457
|
+
}
|
|
458
|
+
if (typeof errorMessage === "string") {
|
|
459
|
+
return errorMessage;
|
|
460
|
+
}
|
|
461
|
+
if (typeof errorMessage === "object" && "ctx" in errorMessage && !(errorMessage instanceof Error)) {
|
|
462
|
+
return errorMessage.msg || originalErrorMsg;
|
|
463
|
+
}
|
|
464
|
+
if (errorMessage instanceof Error) {
|
|
465
|
+
return errorMessage.message;
|
|
466
|
+
}
|
|
467
|
+
return originalErrorMsg;
|
|
468
|
+
})();
|
|
469
|
+
const userStack = (() => {
|
|
470
|
+
if (errorMessage === true) {
|
|
471
|
+
return [
|
|
472
|
+
{
|
|
473
|
+
msg: "SafeTry failed",
|
|
474
|
+
ctx: originalErrorMsg,
|
|
475
|
+
timestamp: now
|
|
476
|
+
}
|
|
477
|
+
];
|
|
478
|
+
}
|
|
479
|
+
if (typeof errorMessage === "string") {
|
|
480
|
+
return [
|
|
481
|
+
{
|
|
482
|
+
msg: errorMessage,
|
|
483
|
+
ctx: originalErrorMsg,
|
|
484
|
+
timestamp: now
|
|
485
|
+
}
|
|
486
|
+
];
|
|
487
|
+
}
|
|
488
|
+
if (typeof errorMessage === "object" && "ctx" in errorMessage && !(errorMessage instanceof Error)) {
|
|
489
|
+
return [
|
|
490
|
+
{
|
|
491
|
+
msg: errorMessage.msg || originalErrorMsg,
|
|
492
|
+
ctx: errorMessage.ctx,
|
|
493
|
+
timestamp: now
|
|
494
|
+
}
|
|
495
|
+
];
|
|
496
|
+
}
|
|
497
|
+
if (errorMessage instanceof Error) {
|
|
498
|
+
const userNewStack = getPreviousBigIOErrorStack(errorMessage);
|
|
499
|
+
if (userNewStack.length > 0) {
|
|
500
|
+
const lastItem = userNewStack.at(-1);
|
|
501
|
+
if (lastItem && typeof lastItem === "object" && !("timestamp" in lastItem && lastItem.timestamp)) {
|
|
502
|
+
return [...userNewStack.slice(0, -1), { ...lastItem, timestamp: now }];
|
|
503
|
+
}
|
|
504
|
+
return userNewStack;
|
|
505
|
+
}
|
|
506
|
+
return [{ msg: errorMessage.message, ctx: originalErrorMsg, timestamp: now }];
|
|
507
|
+
}
|
|
508
|
+
return [{ ctx: originalErrorMsg }];
|
|
509
|
+
})();
|
|
510
|
+
const MAX_STACK_SIZE = IS_DEV ? 500 : 50;
|
|
511
|
+
const newStack = (() => {
|
|
512
|
+
const stack = [...prevStack, ...userStack];
|
|
513
|
+
if (stack.length > MAX_STACK_SIZE) {
|
|
514
|
+
return [
|
|
515
|
+
{
|
|
516
|
+
msg: `... Truncated (Stack > ${MAX_STACK_SIZE}) ...`,
|
|
517
|
+
ctx: null,
|
|
518
|
+
timestamp: now
|
|
519
|
+
},
|
|
520
|
+
...stack.slice(-MAX_STACK_SIZE)
|
|
521
|
+
];
|
|
522
|
+
}
|
|
523
|
+
return stack;
|
|
524
|
+
})();
|
|
525
|
+
consoleError(originalError, newMsg, newStack);
|
|
526
|
+
if (errorMessage instanceof Error) {
|
|
527
|
+
if (!errorMessage.cause && originalError) {
|
|
528
|
+
try {
|
|
529
|
+
Object.defineProperty(errorMessage, "cause", {
|
|
530
|
+
value: originalError,
|
|
531
|
+
configurable: true,
|
|
532
|
+
writable: true,
|
|
533
|
+
enumerable: false
|
|
534
|
+
});
|
|
535
|
+
} catch {
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
try {
|
|
539
|
+
Object.defineProperty(errorMessage, "bigioErrorStack", {
|
|
540
|
+
value: newStack,
|
|
541
|
+
configurable: true,
|
|
542
|
+
writable: true,
|
|
543
|
+
enumerable: false
|
|
544
|
+
});
|
|
545
|
+
} catch {
|
|
546
|
+
throw new BigIOError("Wrapper for frozen user error", {
|
|
547
|
+
cause: errorMessage,
|
|
548
|
+
bigioErrorStack: newStack
|
|
549
|
+
});
|
|
550
|
+
}
|
|
551
|
+
throw errorMessage;
|
|
552
|
+
}
|
|
553
|
+
throw new BigIOError(newMsg, {
|
|
554
|
+
cause: originalError,
|
|
555
|
+
bigioErrorStack: newStack
|
|
556
|
+
});
|
|
557
|
+
};
|
|
278
558
|
var handleSafeError = (originalError) => {
|
|
279
559
|
const prevStack = getPreviousBigIOErrorStack(originalError);
|
|
280
560
|
const originalErrorMsg = originalError instanceof Error ? originalError.message : String(originalError);
|
|
@@ -292,6 +572,9 @@ var handleSafeError = (originalError) => {
|
|
|
292
572
|
};
|
|
293
573
|
};
|
|
294
574
|
var handlePromise = (internalPromise, errorMessage) => {
|
|
575
|
+
if (errorMessage !== void 0) {
|
|
576
|
+
return internalPromise.then((data) => okOr(data, errorMessage)).catch((originalError) => handleUnsafeError(originalError, errorMessage));
|
|
577
|
+
}
|
|
295
578
|
return internalPromise.then((data) => ({ data: okOr(data), error: null })).catch((originalError) => handleSafeError(originalError));
|
|
296
579
|
};
|
|
297
580
|
function safeTry(func, errorMessage) {
|
|
@@ -304,13 +587,18 @@ function safeTry(func, errorMessage) {
|
|
|
304
587
|
if (result instanceof Promise) {
|
|
305
588
|
return handlePromise(result, errorMessage);
|
|
306
589
|
}
|
|
307
|
-
if (errorMessage !== void 0)
|
|
590
|
+
if (errorMessage !== void 0) {
|
|
591
|
+
return okOr(result, errorMessage);
|
|
592
|
+
}
|
|
308
593
|
return {
|
|
309
594
|
data: okOr(result),
|
|
310
595
|
error: null
|
|
311
596
|
};
|
|
312
597
|
}
|
|
313
598
|
} catch (error) {
|
|
599
|
+
if (errorMessage !== void 0) {
|
|
600
|
+
handleUnsafeError(error, errorMessage);
|
|
601
|
+
}
|
|
314
602
|
return handleSafeError(error);
|
|
315
603
|
}
|
|
316
604
|
const now = Date.now();
|
|
@@ -325,22 +613,67 @@ function safeTry(func, errorMessage) {
|
|
|
325
613
|
}
|
|
326
614
|
]
|
|
327
615
|
});
|
|
616
|
+
if (errorMessage !== void 0) {
|
|
617
|
+
throw invalidError;
|
|
618
|
+
}
|
|
328
619
|
return {
|
|
329
620
|
data: null,
|
|
330
621
|
error: invalidError
|
|
331
622
|
};
|
|
332
623
|
}
|
|
333
624
|
new TextEncoder();
|
|
625
|
+
var GLOBAL_DECODER = new TextDecoder();
|
|
626
|
+
function decode64(str) {
|
|
627
|
+
const checkStr = okOr(str, {
|
|
628
|
+
msg: "Invalid string input for fromBase64Url",
|
|
629
|
+
ctx: {
|
|
630
|
+
stringLength: str?.length
|
|
631
|
+
}
|
|
632
|
+
});
|
|
633
|
+
const normalizedStr = checkStr.replace(/\+/g, "-").replace(/\//g, "_");
|
|
634
|
+
const bytesBuffer = safeTry(() => decodeBase64urlIgnorePadding(normalizedStr), {
|
|
635
|
+
msg: "Base64 Decoding Failed",
|
|
636
|
+
ctx: {
|
|
637
|
+
msg: "Oslo decode failed",
|
|
638
|
+
ctx: { strPart: str.slice(0, 10) }
|
|
639
|
+
}
|
|
640
|
+
});
|
|
641
|
+
return bytesBuffer;
|
|
642
|
+
}
|
|
334
643
|
var REGEX_BASE64_URL = /^[a-zA-Z0-9\-_]+=*$/;
|
|
335
|
-
function
|
|
644
|
+
function RequiredSearchParamsBuilder(ELECTRON_SCHEME, PROVIDERS) {
|
|
336
645
|
return z.object({
|
|
337
646
|
scheme: z.string().min(1, "Scheme cannot be empty").regex(REGEX_BASE64_URL).refine((scheme) => scheme === ELECTRON_SCHEME, {
|
|
338
647
|
message: "Invalid scheme provided"
|
|
339
648
|
}),
|
|
340
649
|
provider: z.enum(PROVIDERS),
|
|
341
|
-
challenge: z.string().length(43, "Challenge must be exactly 43 characters").regex(REGEX_BASE64_URL)
|
|
650
|
+
challenge: z.string().length(43, "Challenge must be exactly 43 characters").regex(REGEX_BASE64_URL),
|
|
651
|
+
status: z.enum(["succeed", "error", "newUser"]).optional()
|
|
342
652
|
});
|
|
343
653
|
}
|
|
654
|
+
var OptionalSearchParamsZodBuilder = z.object({
|
|
655
|
+
scopes: z.array(z.string()).optional(),
|
|
656
|
+
loginHint: z.string().optional(),
|
|
657
|
+
additionalData: z.record(z.string(), z.any()).optional(),
|
|
658
|
+
requestSignUp: boolean().optional()
|
|
659
|
+
});
|
|
660
|
+
var safeDecodeURL = (data) => {
|
|
661
|
+
return safeTry(() => {
|
|
662
|
+
if (data === void 0 || data === null) {
|
|
663
|
+
throw new BigIOError("Invalid input for safeDecodeURL: input is null/undefined", {
|
|
664
|
+
bigioErrorStack: [{ msg: "input is null/undefined" }]
|
|
665
|
+
});
|
|
666
|
+
}
|
|
667
|
+
if (typeof data !== "string") {
|
|
668
|
+
throw new BigIOError("Invalid input for safeDecodeURL: expected string", {
|
|
669
|
+
bigioErrorStack: [{ ctx: { type: typeof data, value: data } }]
|
|
670
|
+
});
|
|
671
|
+
}
|
|
672
|
+
const bytes = decode64(data);
|
|
673
|
+
const jsonStr = GLOBAL_DECODER.decode(bytes);
|
|
674
|
+
return JSON.parse(jsonStr);
|
|
675
|
+
}, true);
|
|
676
|
+
};
|
|
344
677
|
|
|
345
678
|
// src/web/electron-web-clientHelper.ts
|
|
346
679
|
var lazyClient = atom();
|
|
@@ -357,7 +690,7 @@ var checkAndSetGlobalLock = () => {
|
|
|
357
690
|
return false;
|
|
358
691
|
};
|
|
359
692
|
var electronWebHandoffPlugin = () => {
|
|
360
|
-
const config = { ...
|
|
693
|
+
const config = { ...defaultWebOptions };
|
|
361
694
|
const {
|
|
362
695
|
CHALLENGE_NAME_IN_URL,
|
|
363
696
|
PROVIDER_NAME_IN_URL,
|
|
@@ -365,16 +698,23 @@ var electronWebHandoffPlugin = () => {
|
|
|
365
698
|
PROVIDERS,
|
|
366
699
|
ELECTRON_SCHEME,
|
|
367
700
|
WEB_OAUTH_SIGNIN_CALLBACK_PATHNAME,
|
|
368
|
-
BACKEND_FAST_TICKET_URL
|
|
701
|
+
BACKEND_FAST_TICKET_URL,
|
|
702
|
+
SCOPES_NAME_IN_URL,
|
|
703
|
+
LOGINHINT_NAME_IN_URL,
|
|
704
|
+
ADDITIONAL_DATA_NAME_IN_URL,
|
|
705
|
+
REQUEST_SIGN_UP_NAME_IN_URL,
|
|
706
|
+
AUTH_STATUS_NAME_IN_URL
|
|
369
707
|
} = config;
|
|
370
708
|
const electronWebAtoms = ($fetch) => {
|
|
371
709
|
const handoffError = atom(null);
|
|
372
710
|
const handoffStatus = atom("idle");
|
|
373
711
|
const fastLogin = atom(null);
|
|
712
|
+
const initializationErrorAtom = atom(null);
|
|
374
713
|
return {
|
|
375
714
|
handoffError,
|
|
376
715
|
handoffStatus,
|
|
377
|
-
fastLogin
|
|
716
|
+
fastLogin,
|
|
717
|
+
initializationErrorAtom
|
|
378
718
|
};
|
|
379
719
|
};
|
|
380
720
|
return {
|
|
@@ -382,152 +722,208 @@ var electronWebHandoffPlugin = () => {
|
|
|
382
722
|
getAtoms: ($fetch) => electronWebAtoms(),
|
|
383
723
|
getActions: ($fetch, $store) => {
|
|
384
724
|
const sessionAtom = $store.atoms.session;
|
|
385
|
-
const {
|
|
386
|
-
|
|
725
|
+
const {
|
|
726
|
+
handoffStatus,
|
|
727
|
+
handoffError,
|
|
728
|
+
fastLogin: fastLoginAtom,
|
|
729
|
+
initializationErrorAtom
|
|
730
|
+
} = $store.atoms;
|
|
731
|
+
const handoffLogic = async () => {
|
|
387
732
|
if (handoffStatus.get() !== "idle" && handoffStatus.get() !== "pending") {
|
|
388
|
-
return;
|
|
733
|
+
return false;
|
|
389
734
|
}
|
|
390
735
|
const { data: sessionData, isPending, isRefetching, error } = sessionAtom.get();
|
|
391
736
|
if (isPending || isRefetching || error) {
|
|
392
|
-
return;
|
|
737
|
+
return false;
|
|
393
738
|
}
|
|
394
739
|
const searchParams = new URLSearchParams(window.location.search);
|
|
395
740
|
const scheme = searchParams.get(SCHEME_NAME_IN_URL);
|
|
396
741
|
if (!scheme) {
|
|
397
|
-
return;
|
|
742
|
+
return false;
|
|
398
743
|
}
|
|
399
744
|
if (scheme !== ELECTRON_SCHEME) {
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
745
|
+
throw new BigIOError(`Wrong Scheme: ${scheme}`, {
|
|
746
|
+
bigioErrorStack: [
|
|
747
|
+
{
|
|
748
|
+
msg: `Wrong Scheme: ${scheme}`
|
|
749
|
+
}
|
|
750
|
+
]
|
|
751
|
+
});
|
|
403
752
|
}
|
|
404
753
|
const provider = searchParams.get(PROVIDER_NAME_IN_URL);
|
|
754
|
+
if (!provider) {
|
|
755
|
+
return false;
|
|
756
|
+
}
|
|
405
757
|
const challenge = searchParams.get(CHALLENGE_NAME_IN_URL);
|
|
406
|
-
|
|
407
|
-
|
|
758
|
+
if (!challenge) {
|
|
759
|
+
return false;
|
|
760
|
+
}
|
|
761
|
+
const SearchParamsZod = RequiredSearchParamsBuilder(ELECTRON_SCHEME, [...PROVIDERS]);
|
|
762
|
+
const requiredValidParams = SearchParamsZod.parse({
|
|
408
763
|
scheme,
|
|
409
764
|
provider,
|
|
410
765
|
challenge
|
|
411
|
-
};
|
|
412
|
-
const
|
|
766
|
+
});
|
|
767
|
+
const scopes = searchParams.get(SCOPES_NAME_IN_URL);
|
|
768
|
+
const loginHint = searchParams.get(LOGINHINT_NAME_IN_URL);
|
|
769
|
+
const additionalData = searchParams.get(ADDITIONAL_DATA_NAME_IN_URL);
|
|
770
|
+
const requestSignUp = searchParams.get(REQUEST_SIGN_UP_NAME_IN_URL);
|
|
771
|
+
const optionalValidParams = OptionalSearchParamsZodBuilder.parse({
|
|
772
|
+
scopes: scopes ? safeDecodeURL(scopes) : void 0,
|
|
773
|
+
loginHint: loginHint ? safeDecodeURL(loginHint) : void 0,
|
|
774
|
+
additionalData: additionalData ? safeDecodeURL(additionalData) : void 0,
|
|
775
|
+
requestSignUp: requestSignUp ? safeDecodeURL(requestSignUp) : void 0
|
|
776
|
+
});
|
|
413
777
|
const client = lazyClient.get();
|
|
414
778
|
if (!client) {
|
|
415
|
-
|
|
779
|
+
throw new BigIOError("handoff fn faild to get AuthClient", {
|
|
780
|
+
bigioErrorStack: [{ msg: "check the init authClient code" }]
|
|
781
|
+
});
|
|
416
782
|
}
|
|
417
|
-
const
|
|
783
|
+
const fastLogin = async () => {
|
|
418
784
|
handoffStatus.set("connecting");
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
785
|
+
return await safeTry(async () => {
|
|
786
|
+
const { data: fastTicketData, error: fastTicketError } = await client.$fetch(`/${BACKEND_FAST_TICKET_URL}`, {
|
|
787
|
+
method: "POST",
|
|
788
|
+
body: {
|
|
789
|
+
userid: (sessionData?.user).id,
|
|
790
|
+
scheme,
|
|
791
|
+
provider,
|
|
792
|
+
challenge
|
|
793
|
+
},
|
|
794
|
+
credentials: "include"
|
|
795
|
+
});
|
|
796
|
+
if (fastTicketError || !fastTicketData.redirect) {
|
|
797
|
+
throw new BigIOError("Failed to get fast ticket", {
|
|
798
|
+
bigioErrorStack: [
|
|
799
|
+
{
|
|
800
|
+
msg: "Failed to get fast ticket",
|
|
801
|
+
ctx: {
|
|
802
|
+
requiredValidParams,
|
|
803
|
+
error: fastTicketError
|
|
804
|
+
}
|
|
805
|
+
}
|
|
806
|
+
]
|
|
430
807
|
});
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
808
|
+
}
|
|
809
|
+
const targetUrl = fastTicketData.redirect;
|
|
810
|
+
if (!targetUrl.startsWith(`${scheme}://`)) {
|
|
811
|
+
throw new BigIOError(
|
|
812
|
+
`Failed to get fast ticket with Wrong Scheme: ${scheme}`,
|
|
813
|
+
{
|
|
434
814
|
bigioErrorStack: [
|
|
435
|
-
{
|
|
815
|
+
{
|
|
816
|
+
msg: "Failed to get fast ticket with Wrong Scheme",
|
|
817
|
+
ctx: scheme
|
|
818
|
+
}
|
|
436
819
|
]
|
|
437
|
-
}
|
|
438
|
-
|
|
439
|
-
const targetUrl = fastTicketData.redirect;
|
|
440
|
-
if (!targetUrl.startsWith(`${scheme}://`)) {
|
|
441
|
-
handoffStatus.set("failed");
|
|
442
|
-
throw new BigIOError(
|
|
443
|
-
`Failed to get fast ticket with Wrong Scheme: ${scheme}`,
|
|
444
|
-
{
|
|
445
|
-
bigioErrorStack: [
|
|
446
|
-
{
|
|
447
|
-
msg: "Failed to get fast ticket with Wrong Scheme",
|
|
448
|
-
ctx: scheme
|
|
449
|
-
}
|
|
450
|
-
]
|
|
451
|
-
}
|
|
452
|
-
);
|
|
453
|
-
}
|
|
454
|
-
return fastTicketData;
|
|
820
|
+
}
|
|
821
|
+
);
|
|
455
822
|
}
|
|
823
|
+
handoffStatus.set("succeed");
|
|
824
|
+
window.location.href = targetUrl;
|
|
825
|
+
return sessionData.user;
|
|
826
|
+
}, true);
|
|
827
|
+
};
|
|
828
|
+
const parseURL = (status) => {
|
|
829
|
+
const callbackURL = new URL(
|
|
830
|
+
`/${WEB_OAUTH_SIGNIN_CALLBACK_PATHNAME}`,
|
|
831
|
+
window.location.origin
|
|
456
832
|
);
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
return
|
|
833
|
+
callbackURL.searchParams.set(AUTH_STATUS_NAME_IN_URL, status);
|
|
834
|
+
callbackURL.searchParams.set(SCHEME_NAME_IN_URL, requiredValidParams.scheme);
|
|
835
|
+
callbackURL.searchParams.set(PROVIDER_NAME_IN_URL, requiredValidParams.provider);
|
|
836
|
+
callbackURL.searchParams.set(
|
|
837
|
+
CHALLENGE_NAME_IN_URL,
|
|
838
|
+
requiredValidParams.challenge
|
|
839
|
+
);
|
|
840
|
+
return callbackURL.pathname + callbackURL.search;
|
|
465
841
|
};
|
|
466
|
-
const
|
|
842
|
+
const regularLogin = async (loginProvider) => {
|
|
467
843
|
handoffStatus.set("connecting");
|
|
468
|
-
|
|
469
|
-
const
|
|
470
|
-
|
|
471
|
-
|
|
844
|
+
return await safeTry(async () => {
|
|
845
|
+
const { data: socialData, error: socialError } = await client.signIn.social(
|
|
846
|
+
{
|
|
847
|
+
provider: loginProvider,
|
|
848
|
+
callbackURL: parseURL("succeed"),
|
|
849
|
+
scopes: optionalValidParams.scopes,
|
|
850
|
+
additionalData: optionalValidParams.additionalData,
|
|
851
|
+
loginHint: optionalValidParams.loginHint,
|
|
852
|
+
requestSignUp: optionalValidParams.requestSignUp,
|
|
853
|
+
disableRedirect: false,
|
|
854
|
+
errorCallbackURL: parseURL("error"),
|
|
855
|
+
newUserCallbackURL: parseURL("newUser")
|
|
856
|
+
}
|
|
472
857
|
);
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
// scopes
|
|
489
|
-
});
|
|
490
|
-
return social;
|
|
491
|
-
});
|
|
492
|
-
if (!loginData || loginError) {
|
|
493
|
-
handoffStatus.set("failed");
|
|
494
|
-
handoffError.set("Failed to perform oauth login");
|
|
495
|
-
return false;
|
|
496
|
-
}
|
|
497
|
-
handoffStatus.set("succeed");
|
|
498
|
-
return true;
|
|
858
|
+
if (!socialData || socialError) {
|
|
859
|
+
throw new BigIOError(`Faild to sign in with ${loginProvider}`, {
|
|
860
|
+
bigioErrorStack: [
|
|
861
|
+
{
|
|
862
|
+
ctx: {
|
|
863
|
+
provider: loginProvider,
|
|
864
|
+
error: socialError
|
|
865
|
+
}
|
|
866
|
+
}
|
|
867
|
+
]
|
|
868
|
+
});
|
|
869
|
+
}
|
|
870
|
+
handoffStatus.set("succeed");
|
|
871
|
+
return socialData;
|
|
872
|
+
}, true);
|
|
499
873
|
};
|
|
500
|
-
if (sessionData
|
|
501
|
-
|
|
874
|
+
if (sessionData?.session) {
|
|
875
|
+
if (optionalValidParams.requestSignUp) {
|
|
876
|
+
return await regularLogin(provider);
|
|
877
|
+
}
|
|
878
|
+
const isFastLogin = fastLoginAtom.get();
|
|
502
879
|
if (isFastLogin === true) {
|
|
503
|
-
|
|
880
|
+
return await fastLogin();
|
|
504
881
|
}
|
|
505
882
|
if (isFastLogin === false) {
|
|
506
|
-
|
|
883
|
+
return await regularLogin(provider);
|
|
507
884
|
}
|
|
508
885
|
if (isFastLogin === null) {
|
|
509
886
|
handoffStatus.set("pending");
|
|
510
|
-
return;
|
|
887
|
+
return true;
|
|
511
888
|
}
|
|
512
889
|
} else {
|
|
513
|
-
|
|
890
|
+
return await regularLogin(provider);
|
|
514
891
|
}
|
|
892
|
+
return false;
|
|
515
893
|
};
|
|
516
894
|
if (!checkAndSetGlobalLock()) {
|
|
517
|
-
sessionAtom.listen((aatom) => {
|
|
518
|
-
|
|
895
|
+
sessionAtom.listen(async (aatom) => {
|
|
896
|
+
console.log(sessionAtom.get());
|
|
897
|
+
const { data, error } = await safeTry(() => handoffLogic());
|
|
898
|
+
if (error) {
|
|
899
|
+
handoffStatus.set("failed");
|
|
900
|
+
handoffError.set(error.message);
|
|
901
|
+
initializationErrorAtom.set(error);
|
|
902
|
+
console.error(error);
|
|
903
|
+
}
|
|
519
904
|
});
|
|
520
|
-
|
|
905
|
+
fastLoginAtom.listen(async () => {
|
|
521
906
|
handoffStatus.set("idle");
|
|
522
|
-
handoffLogic();
|
|
907
|
+
const { data, error } = await safeTry(() => handoffLogic());
|
|
908
|
+
if (error) {
|
|
909
|
+
handoffStatus.set("failed");
|
|
910
|
+
handoffError.set(error.message);
|
|
911
|
+
initializationErrorAtom.set(error);
|
|
912
|
+
console.error(error);
|
|
913
|
+
}
|
|
523
914
|
});
|
|
524
915
|
}
|
|
525
916
|
const setFastLogin = (decision) => {
|
|
526
|
-
|
|
917
|
+
fastLoginAtom.set(decision);
|
|
527
918
|
};
|
|
528
919
|
return {
|
|
529
920
|
bigio: {
|
|
530
921
|
useElectronOAuthSession: () => {
|
|
922
|
+
const initializationError = initializationErrorAtom.get();
|
|
923
|
+
if (initializationError instanceof Error) {
|
|
924
|
+
console.error(initializationError);
|
|
925
|
+
initializationErrorAtom.set(null);
|
|
926
|
+
}
|
|
531
927
|
const { data, error, isPending, isRefetching, refetch } = useStore(sessionAtom);
|
|
532
928
|
const oauthError = useStore(handoffError);
|
|
533
929
|
const oauthStatus = useStore(handoffStatus);
|