@insforge/react 0.6.3 → 0.6.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/dist/atoms.cjs +3 -2
- package/dist/atoms.cjs.map +1 -1
- package/dist/atoms.js +3 -2
- package/dist/atoms.js.map +1 -1
- package/dist/components.cjs +17 -22
- package/dist/components.cjs.map +1 -1
- package/dist/components.d.cts +16 -0
- package/dist/components.d.ts +16 -0
- package/dist/components.js +17 -22
- package/dist/components.js.map +1 -1
- package/dist/forms.cjs +3 -2
- package/dist/forms.cjs.map +1 -1
- package/dist/forms.js +3 -2
- package/dist/forms.js.map +1 -1
- package/dist/hooks.cjs +3 -2
- package/dist/hooks.cjs.map +1 -1
- package/dist/hooks.d.cts +2 -2
- package/dist/hooks.d.ts +2 -2
- package/dist/hooks.js +3 -2
- package/dist/hooks.js.map +1 -1
- package/dist/index.cjs +53 -40
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +53 -40
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -67,8 +67,9 @@ var InsforgeManager = class _InsforgeManager {
|
|
|
67
67
|
// Static private instance
|
|
68
68
|
static instance = null;
|
|
69
69
|
// State storage
|
|
70
|
-
user =
|
|
70
|
+
user = void 0;
|
|
71
71
|
isLoaded = false;
|
|
72
|
+
isInitializing = false;
|
|
72
73
|
sdk;
|
|
73
74
|
listeners = /* @__PURE__ */ new Set();
|
|
74
75
|
// Config
|
|
@@ -79,6 +80,8 @@ var InsforgeManager = class _InsforgeManager {
|
|
|
79
80
|
constructor(config) {
|
|
80
81
|
this.config = config;
|
|
81
82
|
this.sdk = createClient({ baseUrl: config.baseUrl });
|
|
83
|
+
this.user = void 0;
|
|
84
|
+
this.isLoaded = false;
|
|
82
85
|
}
|
|
83
86
|
// Public access method (Singleton core)
|
|
84
87
|
static getInstance(config) {
|
|
@@ -102,16 +105,28 @@ var InsforgeManager = class _InsforgeManager {
|
|
|
102
105
|
}
|
|
103
106
|
// Public initialization method
|
|
104
107
|
async initialize() {
|
|
105
|
-
if (
|
|
108
|
+
if (this.isLoaded) {
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
if (this.isInitializing) {
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
this.isInitializing = true;
|
|
115
|
+
try {
|
|
106
116
|
await this.loadAuthState();
|
|
117
|
+
} finally {
|
|
118
|
+
this.isInitializing = false;
|
|
107
119
|
}
|
|
108
120
|
}
|
|
109
121
|
// Get current state
|
|
110
122
|
getState() {
|
|
123
|
+
const userId = this.user === void 0 ? void 0 : this.user === null ? null : this.user.id;
|
|
124
|
+
const isSignedIn = this.user === void 0 ? void 0 : this.user !== null;
|
|
111
125
|
return {
|
|
112
126
|
user: this.user,
|
|
127
|
+
userId,
|
|
113
128
|
isLoaded: this.isLoaded,
|
|
114
|
-
isSignedIn
|
|
129
|
+
isSignedIn
|
|
115
130
|
};
|
|
116
131
|
}
|
|
117
132
|
// Subscription mechanism
|
|
@@ -412,22 +427,22 @@ var InsforgeManager = class _InsforgeManager {
|
|
|
412
427
|
getSDK() {
|
|
413
428
|
return this.sdk;
|
|
414
429
|
}
|
|
415
|
-
// Handle
|
|
416
|
-
|
|
430
|
+
// Handle auth redirect after successful authentication
|
|
431
|
+
// Works for all auth sources: OAuth providers, cloud hosting sign-in, email verification
|
|
432
|
+
handleAuthRedirect(isLoaded, user) {
|
|
417
433
|
if (!isLoaded || this.hasProcessedCallbackRef) {
|
|
418
434
|
return false;
|
|
419
435
|
}
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
this.hasProcessedCallbackRef
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
}
|
|
430
|
-
return true;
|
|
436
|
+
if (user && this.config.afterSignInUrl) {
|
|
437
|
+
const currentPath = window.location.pathname + window.location.search;
|
|
438
|
+
const targetPath = this.config.afterSignInUrl;
|
|
439
|
+
if (currentPath !== targetPath && !this.hasProcessedCallbackRef) {
|
|
440
|
+
this.hasProcessedCallbackRef = true;
|
|
441
|
+
setTimeout(() => {
|
|
442
|
+
window.location.href = targetPath;
|
|
443
|
+
}, 100);
|
|
444
|
+
return true;
|
|
445
|
+
}
|
|
431
446
|
}
|
|
432
447
|
return false;
|
|
433
448
|
}
|
|
@@ -464,17 +479,20 @@ function InsforgeProviderCore({
|
|
|
464
479
|
setState(newState);
|
|
465
480
|
});
|
|
466
481
|
void manager.initialize();
|
|
467
|
-
return
|
|
482
|
+
return () => {
|
|
483
|
+
unsubscribe();
|
|
484
|
+
};
|
|
468
485
|
}, [manager]);
|
|
469
486
|
useEffect(() => {
|
|
470
487
|
if (typeof window !== "undefined") {
|
|
471
|
-
manager.
|
|
488
|
+
manager.handleAuthRedirect(state.isLoaded, state.user);
|
|
472
489
|
}
|
|
473
490
|
}, [manager, state.isLoaded, state.user]);
|
|
474
491
|
const contextValue = useMemo(
|
|
475
492
|
() => ({
|
|
476
493
|
// State from Manager
|
|
477
494
|
user: state.user,
|
|
495
|
+
userId: state.userId,
|
|
478
496
|
isLoaded: state.isLoaded,
|
|
479
497
|
isSignedIn: state.isSignedIn,
|
|
480
498
|
// Methods delegated to Manager
|
|
@@ -506,9 +524,10 @@ function useInsforge() {
|
|
|
506
524
|
const context = useContext(InsforgeContext);
|
|
507
525
|
if (!context) {
|
|
508
526
|
return {
|
|
509
|
-
user:
|
|
527
|
+
user: void 0,
|
|
528
|
+
userId: void 0,
|
|
510
529
|
isLoaded: false,
|
|
511
|
-
isSignedIn:
|
|
530
|
+
isSignedIn: void 0,
|
|
512
531
|
setUser: () => {
|
|
513
532
|
},
|
|
514
533
|
signIn: () => Promise.resolve({ error: "SSR mode" }),
|
|
@@ -2488,16 +2507,16 @@ function Protect({
|
|
|
2488
2507
|
condition,
|
|
2489
2508
|
onRedirect
|
|
2490
2509
|
}) {
|
|
2491
|
-
const {
|
|
2510
|
+
const { userId, user } = useInsforge();
|
|
2492
2511
|
const resolvedRedirectTo = useMemo(() => resolveAuthPath(redirectTo), [redirectTo]);
|
|
2493
2512
|
useEffect(() => {
|
|
2494
|
-
if (
|
|
2513
|
+
if (userId === null) {
|
|
2495
2514
|
if (onRedirect) {
|
|
2496
2515
|
onRedirect(resolvedRedirectTo);
|
|
2497
2516
|
} else {
|
|
2498
2517
|
window.location.href = resolvedRedirectTo;
|
|
2499
2518
|
}
|
|
2500
|
-
} else if (
|
|
2519
|
+
} else if (userId && condition && user) {
|
|
2501
2520
|
if (!condition(user)) {
|
|
2502
2521
|
if (onRedirect) {
|
|
2503
2522
|
onRedirect(resolvedRedirectTo);
|
|
@@ -2506,11 +2525,11 @@ function Protect({
|
|
|
2506
2525
|
}
|
|
2507
2526
|
}
|
|
2508
2527
|
}
|
|
2509
|
-
}, [
|
|
2510
|
-
if (
|
|
2528
|
+
}, [userId, resolvedRedirectTo, condition, user, onRedirect]);
|
|
2529
|
+
if (userId === void 0) {
|
|
2511
2530
|
return fallback || /* @__PURE__ */ jsx("div", { className: "insforge-loading", children: "Loading..." });
|
|
2512
2531
|
}
|
|
2513
|
-
if (
|
|
2532
|
+
if (userId === null) {
|
|
2514
2533
|
return fallback || null;
|
|
2515
2534
|
}
|
|
2516
2535
|
if (condition && user && !condition(user)) {
|
|
@@ -2519,24 +2538,18 @@ function Protect({
|
|
|
2519
2538
|
return /* @__PURE__ */ jsx(Fragment, { children });
|
|
2520
2539
|
}
|
|
2521
2540
|
function SignedIn({ children }) {
|
|
2522
|
-
const {
|
|
2523
|
-
if (
|
|
2524
|
-
return
|
|
2541
|
+
const { userId } = useInsforge();
|
|
2542
|
+
if (userId) {
|
|
2543
|
+
return /* @__PURE__ */ jsx(Fragment, { children });
|
|
2525
2544
|
}
|
|
2526
|
-
|
|
2527
|
-
return null;
|
|
2528
|
-
}
|
|
2529
|
-
return /* @__PURE__ */ jsx(Fragment, { children });
|
|
2545
|
+
return null;
|
|
2530
2546
|
}
|
|
2531
2547
|
function SignedOut({ children }) {
|
|
2532
|
-
const {
|
|
2533
|
-
if (
|
|
2534
|
-
return
|
|
2535
|
-
}
|
|
2536
|
-
if (isSignedIn) {
|
|
2537
|
-
return null;
|
|
2548
|
+
const { userId } = useInsforge();
|
|
2549
|
+
if (userId === null) {
|
|
2550
|
+
return /* @__PURE__ */ jsx(Fragment, { children });
|
|
2538
2551
|
}
|
|
2539
|
-
return
|
|
2552
|
+
return null;
|
|
2540
2553
|
}
|
|
2541
2554
|
function SignInButton({ children, className }) {
|
|
2542
2555
|
const { afterSignInUrl, baseUrl } = useInsforge();
|