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