@insforge/react 0.5.10 → 0.6.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/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 +3 -2
- package/dist/components.cjs.map +1 -1
- package/dist/components.js +3 -2
- 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 +2 -1
- package/dist/hooks.cjs.map +1 -1
- package/dist/hooks.js +2 -1
- package/dist/hooks.js.map +1 -1
- package/dist/index.cjs +376 -292
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +8 -4
- package/dist/index.d.ts +8 -4
- package/dist/index.js +377 -293
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -12,13 +12,15 @@ if (typeof document !== 'undefined' && typeof window !== 'undefined') {
|
|
|
12
12
|
}
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
import { createContext, useContext, useState,
|
|
15
|
+
import { createContext, useContext, useState, useMemo, useEffect, useRef, useCallback, isValidElement, cloneElement } from 'react';
|
|
16
16
|
import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
|
|
17
17
|
import { createClient } from '@insforge/sdk';
|
|
18
18
|
import { AlertTriangle, Check, EyeOff, Eye, Loader2, CircleCheck, LogOut } from 'lucide-react';
|
|
19
19
|
import { z } from 'zod';
|
|
20
20
|
|
|
21
|
-
|
|
21
|
+
var __defProp = Object.defineProperty;
|
|
22
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
23
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
22
24
|
var NavigationContext = createContext(null);
|
|
23
25
|
function NavigationProvider({ adapter, children }) {
|
|
24
26
|
return /* @__PURE__ */ jsx(NavigationContext.Provider, { value: adapter, children });
|
|
@@ -62,34 +64,80 @@ function useSearchParams() {
|
|
|
62
64
|
const adapter = useNavigationAdapter();
|
|
63
65
|
return adapter.useSearchParams();
|
|
64
66
|
}
|
|
65
|
-
var
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
67
|
+
var _InsforgeManager = class _InsforgeManager {
|
|
68
|
+
// Private constructor (prevents external instantiation)
|
|
69
|
+
constructor(config) {
|
|
70
|
+
// State storage
|
|
71
|
+
__publicField(this, "user", null);
|
|
72
|
+
__publicField(this, "isLoaded", false);
|
|
73
|
+
__publicField(this, "sdk");
|
|
74
|
+
__publicField(this, "listeners", /* @__PURE__ */ new Set());
|
|
75
|
+
// Config
|
|
76
|
+
__publicField(this, "config");
|
|
77
|
+
__publicField(this, "refreshIntervalRef", null);
|
|
78
|
+
__publicField(this, "hasProcessedCallbackRef", false);
|
|
79
|
+
this.config = config;
|
|
80
|
+
this.sdk = createClient({ baseUrl: config.baseUrl });
|
|
81
|
+
}
|
|
82
|
+
// Public access method (Singleton core)
|
|
83
|
+
static getInstance(config) {
|
|
84
|
+
if (typeof window !== "undefined" && _InsforgeManager.instance) {
|
|
85
|
+
_InsforgeManager.instance.updateConfig(config);
|
|
86
|
+
return _InsforgeManager.instance;
|
|
87
|
+
}
|
|
88
|
+
_InsforgeManager.instance = new _InsforgeManager(config);
|
|
89
|
+
return _InsforgeManager.instance;
|
|
90
|
+
}
|
|
91
|
+
// Clear instance (used for testing or cleanup)
|
|
92
|
+
static clearInstance() {
|
|
93
|
+
if (_InsforgeManager.instance) {
|
|
94
|
+
_InsforgeManager.instance.cleanup();
|
|
95
|
+
}
|
|
96
|
+
_InsforgeManager.instance = null;
|
|
97
|
+
}
|
|
98
|
+
// Update config (for hot reloads)
|
|
99
|
+
updateConfig(config) {
|
|
100
|
+
this.config = { ...this.config, ...config };
|
|
101
|
+
}
|
|
102
|
+
// Public initialization method
|
|
103
|
+
async initialize() {
|
|
104
|
+
if (!this.isLoaded) {
|
|
105
|
+
await this.loadAuthState();
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
// Get current state
|
|
109
|
+
getState() {
|
|
110
|
+
return {
|
|
111
|
+
user: this.user,
|
|
112
|
+
isLoaded: this.isLoaded,
|
|
113
|
+
isSignedIn: !!this.user
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
// Subscription mechanism
|
|
117
|
+
subscribe(listener) {
|
|
118
|
+
this.listeners.add(listener);
|
|
119
|
+
return () => this.listeners.delete(listener);
|
|
120
|
+
}
|
|
121
|
+
notifyListeners() {
|
|
122
|
+
const state = this.getState();
|
|
123
|
+
this.listeners.forEach((listener) => listener(state));
|
|
124
|
+
}
|
|
125
|
+
// Load auth state
|
|
126
|
+
async loadAuthState() {
|
|
80
127
|
try {
|
|
81
|
-
const sessionResult =
|
|
128
|
+
const sessionResult = this.sdk.auth.getCurrentSession();
|
|
82
129
|
const session = sessionResult.data?.session;
|
|
83
130
|
const token = session?.accessToken || null;
|
|
84
131
|
if (!token) {
|
|
85
|
-
|
|
86
|
-
if (onAuthChange) {
|
|
87
|
-
onAuthChange(null);
|
|
132
|
+
this.user = null;
|
|
133
|
+
if (this.config.onAuthChange) {
|
|
134
|
+
this.config.onAuthChange(null);
|
|
88
135
|
}
|
|
89
|
-
|
|
136
|
+
this.isLoaded = true;
|
|
137
|
+
this.notifyListeners();
|
|
90
138
|
return { success: false, error: "no_session" };
|
|
91
139
|
}
|
|
92
|
-
const userResult = await
|
|
140
|
+
const userResult = await this.sdk.auth.getCurrentUser();
|
|
93
141
|
if (userResult.data) {
|
|
94
142
|
const profile = userResult.data.profile;
|
|
95
143
|
const userData = {
|
|
@@ -98,326 +146,362 @@ function InsforgeProviderCore({
|
|
|
98
146
|
name: profile?.nickname || "",
|
|
99
147
|
avatarUrl: profile?.avatarUrl || ""
|
|
100
148
|
};
|
|
101
|
-
|
|
102
|
-
if (onAuthChange) {
|
|
103
|
-
onAuthChange(userData);
|
|
149
|
+
this.user = userData;
|
|
150
|
+
if (this.config.onAuthChange) {
|
|
151
|
+
this.config.onAuthChange(userData);
|
|
104
152
|
}
|
|
105
|
-
|
|
153
|
+
this.isLoaded = true;
|
|
154
|
+
this.notifyListeners();
|
|
106
155
|
return { success: true };
|
|
107
156
|
} else {
|
|
108
|
-
await
|
|
109
|
-
if (onSignOut) {
|
|
157
|
+
await this.sdk.auth.signOut();
|
|
158
|
+
if (this.config.onSignOut) {
|
|
110
159
|
try {
|
|
111
|
-
await onSignOut();
|
|
160
|
+
await this.config.onSignOut();
|
|
112
161
|
} catch (error) {
|
|
113
162
|
if (error instanceof Error) {
|
|
114
|
-
console.error("[
|
|
163
|
+
console.error("[InsforgeManager] Error clearing cookie:", error.message);
|
|
115
164
|
}
|
|
116
165
|
}
|
|
117
166
|
}
|
|
118
|
-
|
|
119
|
-
if (onAuthChange) {
|
|
120
|
-
onAuthChange(null);
|
|
167
|
+
this.user = null;
|
|
168
|
+
if (this.config.onAuthChange) {
|
|
169
|
+
this.config.onAuthChange(null);
|
|
121
170
|
}
|
|
122
|
-
|
|
171
|
+
this.isLoaded = true;
|
|
172
|
+
this.notifyListeners();
|
|
123
173
|
return { success: false, error: "invalid_token" };
|
|
124
174
|
}
|
|
125
175
|
} catch (error) {
|
|
126
|
-
console.error("[
|
|
127
|
-
await
|
|
128
|
-
if (onSignOut) {
|
|
176
|
+
console.error("[InsforgeManager] Token validation failed:", error);
|
|
177
|
+
await this.sdk.auth.signOut();
|
|
178
|
+
if (this.config.onSignOut) {
|
|
129
179
|
try {
|
|
130
|
-
await onSignOut();
|
|
180
|
+
await this.config.onSignOut();
|
|
131
181
|
} catch (error2) {
|
|
132
182
|
if (error2 instanceof Error) {
|
|
133
|
-
console.error("[
|
|
183
|
+
console.error("[InsforgeManager] Error clearing cookie:", error2.message);
|
|
134
184
|
}
|
|
135
185
|
}
|
|
136
186
|
}
|
|
137
|
-
|
|
138
|
-
if (onAuthChange) {
|
|
139
|
-
onAuthChange(null);
|
|
187
|
+
this.user = null;
|
|
188
|
+
if (this.config.onAuthChange) {
|
|
189
|
+
this.config.onAuthChange(null);
|
|
140
190
|
}
|
|
141
|
-
|
|
191
|
+
this.isLoaded = true;
|
|
192
|
+
this.notifyListeners();
|
|
142
193
|
return {
|
|
143
194
|
success: false,
|
|
144
195
|
error: error instanceof Error ? error.message : "Authentication failed"
|
|
145
196
|
};
|
|
146
197
|
}
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
const
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
const accessToken = searchParams.get("access_token");
|
|
163
|
-
if (accessToken && !!user) {
|
|
164
|
-
hasProcessedCallbackRef.current = true;
|
|
165
|
-
const url = new URL(window.location.href);
|
|
166
|
-
url.search = "";
|
|
167
|
-
window.history.replaceState({}, "", url.toString());
|
|
168
|
-
setTimeout(() => {
|
|
169
|
-
window.location.href = afterSignInUrl;
|
|
170
|
-
}, 100);
|
|
171
|
-
}
|
|
172
|
-
}, [isLoaded, user, afterSignInUrl]);
|
|
173
|
-
const getPublicAuthConfig = useCallback(async () => {
|
|
174
|
-
try {
|
|
175
|
-
const result = await insforge.auth.getPublicAuthConfig();
|
|
176
|
-
if (result.data) {
|
|
177
|
-
return result.data;
|
|
178
|
-
} else {
|
|
179
|
-
console.error("[InsforgeProvider] Failed to get public auth config:", result.error);
|
|
180
|
-
return null;
|
|
198
|
+
}
|
|
199
|
+
// Helper to handle auth success
|
|
200
|
+
async handleAuthSuccess(authToken, fallbackUser) {
|
|
201
|
+
const userResult = await this.sdk.auth.getCurrentUser();
|
|
202
|
+
if (userResult.data) {
|
|
203
|
+
const profile = userResult.data.profile;
|
|
204
|
+
const userData = {
|
|
205
|
+
id: userResult.data.user.id,
|
|
206
|
+
email: userResult.data.user.email,
|
|
207
|
+
name: profile?.nickname || "",
|
|
208
|
+
avatarUrl: profile?.avatarUrl || ""
|
|
209
|
+
};
|
|
210
|
+
this.user = userData;
|
|
211
|
+
if (this.config.onAuthChange) {
|
|
212
|
+
this.config.onAuthChange(userData);
|
|
181
213
|
}
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
async (authToken, fallbackUser) => {
|
|
189
|
-
const userResult = await insforge.auth.getCurrentUser();
|
|
190
|
-
if (userResult.data) {
|
|
191
|
-
const profile = userResult.data.profile;
|
|
192
|
-
const userData = {
|
|
193
|
-
id: userResult.data.user.id,
|
|
194
|
-
email: userResult.data.user.email,
|
|
195
|
-
name: profile?.nickname || "",
|
|
196
|
-
avatarUrl: profile?.avatarUrl || ""
|
|
197
|
-
};
|
|
198
|
-
setUser(userData);
|
|
199
|
-
if (onAuthChange) {
|
|
200
|
-
onAuthChange(userData);
|
|
201
|
-
}
|
|
202
|
-
if (onSignIn) {
|
|
203
|
-
try {
|
|
204
|
-
await onSignIn(authToken);
|
|
205
|
-
} catch (error) {
|
|
206
|
-
if (error instanceof Error) {
|
|
207
|
-
console.error("[InsforgeProvider] Error syncing token to cookie:", error.message);
|
|
208
|
-
}
|
|
214
|
+
if (this.config.onSignIn) {
|
|
215
|
+
try {
|
|
216
|
+
await this.config.onSignIn(authToken);
|
|
217
|
+
} catch (error) {
|
|
218
|
+
if (error instanceof Error) {
|
|
219
|
+
console.error("[InsforgeManager] Error syncing token to cookie:", error.message);
|
|
209
220
|
}
|
|
210
221
|
}
|
|
211
|
-
} else if (fallbackUser) {
|
|
212
|
-
const userData = {
|
|
213
|
-
id: fallbackUser.id || "",
|
|
214
|
-
email: fallbackUser.email || "",
|
|
215
|
-
name: fallbackUser.name || "",
|
|
216
|
-
avatarUrl: ""
|
|
217
|
-
};
|
|
218
|
-
setUser(userData);
|
|
219
|
-
if (onAuthChange) {
|
|
220
|
-
onAuthChange(userData);
|
|
221
|
-
}
|
|
222
222
|
}
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
if (
|
|
233
|
-
|
|
234
|
-
|
|
223
|
+
this.notifyListeners();
|
|
224
|
+
} else if (fallbackUser) {
|
|
225
|
+
const userData = {
|
|
226
|
+
id: fallbackUser.id || "",
|
|
227
|
+
email: fallbackUser.email || "",
|
|
228
|
+
name: fallbackUser.name || "",
|
|
229
|
+
avatarUrl: ""
|
|
230
|
+
};
|
|
231
|
+
this.user = userData;
|
|
232
|
+
if (this.config.onAuthChange) {
|
|
233
|
+
this.config.onAuthChange(userData);
|
|
234
|
+
}
|
|
235
|
+
this.notifyListeners();
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
// Business methods
|
|
239
|
+
async signIn(email, password) {
|
|
240
|
+
const sdkResult = await this.sdk.auth.signInWithPassword({
|
|
241
|
+
email,
|
|
242
|
+
password
|
|
243
|
+
});
|
|
244
|
+
if (sdkResult.data) {
|
|
245
|
+
await this.handleAuthSuccess(
|
|
246
|
+
sdkResult.data.accessToken || "",
|
|
247
|
+
sdkResult.data.user ? {
|
|
248
|
+
id: sdkResult.data.user.id,
|
|
249
|
+
email: sdkResult.data.user.email,
|
|
250
|
+
name: sdkResult.data.user.name
|
|
251
|
+
} : void 0
|
|
252
|
+
);
|
|
253
|
+
return sdkResult.data;
|
|
254
|
+
} else {
|
|
255
|
+
return {
|
|
256
|
+
error: sdkResult.error?.message || "Invalid email or password",
|
|
257
|
+
statusCode: sdkResult.error?.statusCode,
|
|
258
|
+
errorCode: sdkResult.error?.error
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
async signUp(email, password) {
|
|
263
|
+
const sdkResult = await this.sdk.auth.signUp({ email, password });
|
|
264
|
+
if (sdkResult.data) {
|
|
265
|
+
if (sdkResult.data.accessToken) {
|
|
266
|
+
await this.handleAuthSuccess(
|
|
267
|
+
sdkResult.data.accessToken,
|
|
235
268
|
sdkResult.data.user ? {
|
|
236
269
|
id: sdkResult.data.user.id,
|
|
237
270
|
email: sdkResult.data.user.email,
|
|
238
271
|
name: sdkResult.data.user.name
|
|
239
272
|
} : void 0
|
|
240
273
|
);
|
|
241
|
-
return sdkResult.data;
|
|
242
|
-
} else {
|
|
243
|
-
return {
|
|
244
|
-
error: sdkResult.error?.message || "Invalid email or password",
|
|
245
|
-
statusCode: sdkResult.error?.statusCode,
|
|
246
|
-
errorCode: sdkResult.error?.error
|
|
247
|
-
};
|
|
248
|
-
}
|
|
249
|
-
},
|
|
250
|
-
[insforge, handleAuthSuccess]
|
|
251
|
-
);
|
|
252
|
-
const signUp = useCallback(
|
|
253
|
-
async (email, password) => {
|
|
254
|
-
const sdkResult = await insforge.auth.signUp({ email, password });
|
|
255
|
-
if (sdkResult.data) {
|
|
256
|
-
if (sdkResult.data.accessToken) {
|
|
257
|
-
await handleAuthSuccess(
|
|
258
|
-
sdkResult.data.accessToken,
|
|
259
|
-
sdkResult.data.user ? {
|
|
260
|
-
id: sdkResult.data.user.id,
|
|
261
|
-
email: sdkResult.data.user.email,
|
|
262
|
-
name: sdkResult.data.user.name
|
|
263
|
-
} : void 0
|
|
264
|
-
);
|
|
265
|
-
}
|
|
266
|
-
return sdkResult.data;
|
|
267
|
-
} else {
|
|
268
|
-
return {
|
|
269
|
-
error: sdkResult.error?.message || "Sign up failed",
|
|
270
|
-
statusCode: sdkResult.error?.statusCode,
|
|
271
|
-
errorCode: sdkResult.error?.error
|
|
272
|
-
};
|
|
273
274
|
}
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
275
|
+
return sdkResult.data;
|
|
276
|
+
} else {
|
|
277
|
+
return {
|
|
278
|
+
error: sdkResult.error?.message || "Sign up failed",
|
|
279
|
+
statusCode: sdkResult.error?.statusCode,
|
|
280
|
+
errorCode: sdkResult.error?.error
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
async signOut() {
|
|
285
|
+
await this.sdk.auth.signOut();
|
|
286
|
+
if (this.config.onSignOut) {
|
|
280
287
|
try {
|
|
281
|
-
await onSignOut();
|
|
288
|
+
await this.config.onSignOut();
|
|
282
289
|
} catch (error) {
|
|
283
290
|
if (error instanceof Error) {
|
|
284
|
-
console.error("[
|
|
291
|
+
console.error("[InsforgeManager] Error clearing cookie:", error.message);
|
|
285
292
|
}
|
|
286
293
|
}
|
|
287
294
|
}
|
|
288
|
-
if (refreshIntervalRef
|
|
289
|
-
clearInterval(refreshIntervalRef
|
|
295
|
+
if (this.refreshIntervalRef) {
|
|
296
|
+
clearInterval(this.refreshIntervalRef);
|
|
297
|
+
this.refreshIntervalRef = null;
|
|
290
298
|
}
|
|
291
|
-
|
|
292
|
-
if (onAuthChange) {
|
|
293
|
-
onAuthChange(null);
|
|
299
|
+
this.user = null;
|
|
300
|
+
if (this.config.onAuthChange) {
|
|
301
|
+
this.config.onAuthChange(null);
|
|
294
302
|
}
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
}
|
|
303
|
+
this.notifyListeners();
|
|
304
|
+
}
|
|
305
|
+
async updateUser(data) {
|
|
306
|
+
if (!this.user) {
|
|
307
|
+
return { error: "No user signed in" };
|
|
308
|
+
}
|
|
309
|
+
const profileUpdate = {
|
|
310
|
+
nickname: data.name,
|
|
311
|
+
avatarUrl: data.avatarUrl
|
|
312
|
+
};
|
|
313
|
+
const result = await this.sdk.auth.setProfile(profileUpdate);
|
|
314
|
+
if (result.data) {
|
|
315
|
+
const userResult = await this.sdk.auth.getCurrentUser();
|
|
316
|
+
if (userResult.data) {
|
|
317
|
+
const profile = userResult.data.profile;
|
|
318
|
+
const updatedUser = {
|
|
319
|
+
id: userResult.data.user.id,
|
|
320
|
+
email: userResult.data.user.email,
|
|
321
|
+
name: profile?.nickname || "",
|
|
322
|
+
avatarUrl: profile?.avatarUrl || ""
|
|
323
|
+
};
|
|
324
|
+
this.user = updatedUser;
|
|
325
|
+
if (this.config.onAuthChange) {
|
|
326
|
+
this.config.onAuthChange(updatedUser);
|
|
320
327
|
}
|
|
321
|
-
|
|
322
|
-
} else {
|
|
323
|
-
return { error: result.error?.message || "Failed to update user" };
|
|
328
|
+
this.notifyListeners();
|
|
324
329
|
}
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
330
|
+
return null;
|
|
331
|
+
} else {
|
|
332
|
+
return { error: result.error?.message || "Failed to update user" };
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
async reloadAuth() {
|
|
336
|
+
return await this.loadAuthState();
|
|
337
|
+
}
|
|
338
|
+
async sendVerificationEmail(email) {
|
|
339
|
+
const sdkResult = await this.sdk.auth.sendVerificationEmail({ email });
|
|
340
|
+
return sdkResult.data;
|
|
341
|
+
}
|
|
342
|
+
async sendResetPasswordEmail(email) {
|
|
343
|
+
const sdkResult = await this.sdk.auth.sendResetPasswordEmail({ email });
|
|
344
|
+
return sdkResult.data;
|
|
345
|
+
}
|
|
346
|
+
async resetPassword(token, newPassword) {
|
|
347
|
+
const sdkResult = await this.sdk.auth.resetPassword({
|
|
348
|
+
newPassword,
|
|
349
|
+
otp: token
|
|
350
|
+
});
|
|
351
|
+
return sdkResult.data;
|
|
352
|
+
}
|
|
353
|
+
async verifyEmail(otp, email) {
|
|
354
|
+
const sdkResult = await this.sdk.auth.verifyEmail({ otp, email: email || void 0 });
|
|
355
|
+
if (sdkResult.data) {
|
|
338
356
|
return sdkResult.data;
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
357
|
+
} else {
|
|
358
|
+
return {
|
|
359
|
+
accessToken: "",
|
|
360
|
+
error: {
|
|
361
|
+
message: sdkResult.error?.message || "Email verification failed"
|
|
362
|
+
}
|
|
363
|
+
};
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
async exchangeResetPasswordToken(email, code) {
|
|
367
|
+
const sdkResult = await this.sdk.auth.exchangeResetPasswordToken({ email, code });
|
|
368
|
+
if (sdkResult.data) {
|
|
348
369
|
return sdkResult.data;
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
if (sdkResult.data) {
|
|
372
|
-
return sdkResult.data;
|
|
370
|
+
} else {
|
|
371
|
+
return {
|
|
372
|
+
error: {
|
|
373
|
+
message: sdkResult.error?.message || "Failed to exchange reset password token"
|
|
374
|
+
}
|
|
375
|
+
};
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
async loginWithOAuth(provider, redirectTo) {
|
|
379
|
+
const sdkResult = await this.sdk.auth.signInWithOAuth({
|
|
380
|
+
provider,
|
|
381
|
+
redirectTo: redirectTo || window.location.origin + (this.config.afterSignInUrl || "/")
|
|
382
|
+
});
|
|
383
|
+
if (sdkResult.error) {
|
|
384
|
+
throw new Error(sdkResult.error.message);
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
async getPublicAuthConfig() {
|
|
388
|
+
try {
|
|
389
|
+
const result = await this.sdk.auth.getPublicAuthConfig();
|
|
390
|
+
if (result.data) {
|
|
391
|
+
return result.data;
|
|
373
392
|
} else {
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
message: sdkResult.error?.message || "Failed to exchange reset password token"
|
|
377
|
-
}
|
|
378
|
-
};
|
|
379
|
-
}
|
|
380
|
-
},
|
|
381
|
-
[insforge]
|
|
382
|
-
);
|
|
383
|
-
const loginWithOAuth = useCallback(
|
|
384
|
-
async (provider, redirectTo) => {
|
|
385
|
-
const sdkResult = await insforge.auth.signInWithOAuth({
|
|
386
|
-
provider,
|
|
387
|
-
redirectTo: redirectTo || window.location.origin + afterSignInUrl
|
|
388
|
-
});
|
|
389
|
-
if (sdkResult.error) {
|
|
390
|
-
throw new Error(sdkResult.error.message);
|
|
393
|
+
console.error("[InsforgeManager] Failed to get public auth config:", result.error);
|
|
394
|
+
return null;
|
|
391
395
|
}
|
|
392
|
-
}
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
396
|
+
} catch (error) {
|
|
397
|
+
console.error("[InsforgeManager] Failed to get public auth config:", error);
|
|
398
|
+
return null;
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
setUser(user) {
|
|
402
|
+
this.user = user;
|
|
403
|
+
if (this.config.onAuthChange) {
|
|
404
|
+
this.config.onAuthChange(user);
|
|
405
|
+
}
|
|
406
|
+
this.notifyListeners();
|
|
407
|
+
}
|
|
408
|
+
getConfig() {
|
|
409
|
+
return this.config;
|
|
410
|
+
}
|
|
411
|
+
getSDK() {
|
|
412
|
+
return this.sdk;
|
|
413
|
+
}
|
|
414
|
+
// Handle OAuth callback
|
|
415
|
+
handleOAuthCallback(isLoaded, user) {
|
|
416
|
+
if (!isLoaded || this.hasProcessedCallbackRef) {
|
|
417
|
+
return false;
|
|
418
|
+
}
|
|
419
|
+
const searchParams = new URLSearchParams(window.location.search);
|
|
420
|
+
const accessToken = searchParams.get("access_token");
|
|
421
|
+
if (accessToken && !!user) {
|
|
422
|
+
this.hasProcessedCallbackRef = true;
|
|
423
|
+
const url = new URL(window.location.href);
|
|
424
|
+
url.search = "";
|
|
425
|
+
window.history.replaceState({}, "", url.toString());
|
|
426
|
+
setTimeout(() => {
|
|
427
|
+
window.location.href = this.config.afterSignInUrl || "/";
|
|
428
|
+
}, 100);
|
|
429
|
+
return true;
|
|
430
|
+
}
|
|
431
|
+
return false;
|
|
432
|
+
}
|
|
433
|
+
// Cleanup
|
|
434
|
+
cleanup() {
|
|
435
|
+
if (this.refreshIntervalRef) {
|
|
436
|
+
clearInterval(this.refreshIntervalRef);
|
|
437
|
+
this.refreshIntervalRef = null;
|
|
419
438
|
}
|
|
439
|
+
this.listeners.clear();
|
|
440
|
+
}
|
|
441
|
+
};
|
|
442
|
+
// Static private instance
|
|
443
|
+
__publicField(_InsforgeManager, "instance", null);
|
|
444
|
+
var InsforgeManager = _InsforgeManager;
|
|
445
|
+
var InsforgeContext = createContext(void 0);
|
|
446
|
+
InsforgeContext.displayName = "InsforgeContext";
|
|
447
|
+
function InsforgeProviderCore({
|
|
448
|
+
children,
|
|
449
|
+
baseUrl,
|
|
450
|
+
afterSignInUrl = "/",
|
|
451
|
+
onAuthChange,
|
|
452
|
+
onSignIn,
|
|
453
|
+
onSignOut
|
|
454
|
+
}) {
|
|
455
|
+
const manager = useMemo(
|
|
456
|
+
() => InsforgeManager.getInstance({
|
|
457
|
+
baseUrl,
|
|
458
|
+
afterSignInUrl,
|
|
459
|
+
onAuthChange,
|
|
460
|
+
onSignIn,
|
|
461
|
+
onSignOut
|
|
462
|
+
}),
|
|
463
|
+
[baseUrl, afterSignInUrl, onAuthChange, onSignIn, onSignOut]
|
|
464
|
+
);
|
|
465
|
+
const [state, setState] = useState(() => manager.getState());
|
|
466
|
+
useEffect(() => {
|
|
467
|
+
const unsubscribe = manager.subscribe((newState) => {
|
|
468
|
+
setState(newState);
|
|
469
|
+
});
|
|
470
|
+
manager.initialize();
|
|
471
|
+
return unsubscribe;
|
|
472
|
+
}, [manager]);
|
|
473
|
+
useEffect(() => {
|
|
474
|
+
if (typeof window !== "undefined") {
|
|
475
|
+
manager.handleOAuthCallback(state.isLoaded, state.user);
|
|
476
|
+
}
|
|
477
|
+
}, [manager, state.isLoaded, state.user]);
|
|
478
|
+
const contextValue = useMemo(
|
|
479
|
+
() => ({
|
|
480
|
+
// State from Manager
|
|
481
|
+
user: state.user,
|
|
482
|
+
isLoaded: state.isLoaded,
|
|
483
|
+
isSignedIn: state.isSignedIn,
|
|
484
|
+
// Methods delegated to Manager
|
|
485
|
+
setUser: (user) => manager.setUser(user),
|
|
486
|
+
signIn: (email, password) => manager.signIn(email, password),
|
|
487
|
+
signUp: (email, password) => manager.signUp(email, password),
|
|
488
|
+
signOut: () => manager.signOut(),
|
|
489
|
+
updateUser: (data) => manager.updateUser(data),
|
|
490
|
+
reloadAuth: () => manager.reloadAuth(),
|
|
491
|
+
sendVerificationEmail: (email) => manager.sendVerificationEmail(email),
|
|
492
|
+
sendResetPasswordEmail: (email) => manager.sendResetPasswordEmail(email),
|
|
493
|
+
resetPassword: (token, newPassword) => manager.resetPassword(token, newPassword),
|
|
494
|
+
verifyEmail: (otp, email) => manager.verifyEmail(otp, email),
|
|
495
|
+
exchangeResetPasswordToken: (email, code) => manager.exchangeResetPasswordToken(email, code),
|
|
496
|
+
loginWithOAuth: (provider, redirectTo) => manager.loginWithOAuth(provider, redirectTo),
|
|
497
|
+
getPublicAuthConfig: () => manager.getPublicAuthConfig(),
|
|
498
|
+
// Config
|
|
499
|
+
baseUrl: manager.getConfig().baseUrl,
|
|
500
|
+
afterSignInUrl: manager.getConfig().afterSignInUrl || "/"
|
|
501
|
+
}),
|
|
502
|
+
[state, manager]
|
|
420
503
|
);
|
|
504
|
+
return /* @__PURE__ */ jsx(InsforgeContext.Provider, { value: contextValue, children });
|
|
421
505
|
}
|
|
422
506
|
function InsforgeProvider(props) {
|
|
423
507
|
return /* @__PURE__ */ jsx(NavigationProvider, { adapter: BrowserNavigationAdapter, children: /* @__PURE__ */ jsx(InsforgeProviderCore, { ...props }) });
|
|
@@ -1090,7 +1174,7 @@ function AuthEmailVerificationStep({
|
|
|
1090
1174
|
const isLinkMethod = method === "link";
|
|
1091
1175
|
const displayDescription = isLinkMethod ? "We've sent an email to {email}. Please check your email to confirm your account before signing in. The confirmation link expires in 10 minutes." : "We've sent a verification code to your inbox at {email}. Enter it below to proceed.";
|
|
1092
1176
|
useEffect(() => {
|
|
1093
|
-
if (
|
|
1177
|
+
if (resendDisabled && resendCountdown > 0) {
|
|
1094
1178
|
const timer = setInterval(() => {
|
|
1095
1179
|
setResendCountdown((prev) => {
|
|
1096
1180
|
if (prev <= 1) {
|
|
@@ -1102,7 +1186,7 @@ function AuthEmailVerificationStep({
|
|
|
1102
1186
|
}, 1e3);
|
|
1103
1187
|
return () => clearInterval(timer);
|
|
1104
1188
|
}
|
|
1105
|
-
}, [
|
|
1189
|
+
}, [resendDisabled, resendCountdown]);
|
|
1106
1190
|
const handleResend = async () => {
|
|
1107
1191
|
setResendDisabled(true);
|
|
1108
1192
|
setResendCountdown(60);
|