@insforge/react 0.5.11 → 0.6.1
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 +14 -16
- package/dist/atoms.cjs.map +1 -1
- package/dist/atoms.d.cts +1 -0
- package/dist/atoms.d.ts +1 -0
- package/dist/atoms.js +13 -15
- package/dist/atoms.js.map +1 -1
- package/dist/components.cjs +23 -25
- package/dist/components.cjs.map +1 -1
- package/dist/components.d.cts +2 -1
- package/dist/components.d.ts +2 -1
- package/dist/components.js +22 -24
- package/dist/components.js.map +1 -1
- package/dist/forms.cjs +14 -16
- package/dist/forms.cjs.map +1 -1
- package/dist/forms.d.cts +1 -0
- package/dist/forms.d.ts +1 -0
- package/dist/forms.js +13 -15
- package/dist/forms.js.map +1 -1
- package/dist/hooks.cjs +16 -18
- package/dist/hooks.cjs.map +1 -1
- package/dist/hooks.d.cts +4 -5
- package/dist/hooks.d.ts +4 -5
- package/dist/hooks.js +15 -17
- package/dist/hooks.js.map +1 -1
- package/dist/index.cjs +391 -313
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +12 -60
- package/dist/index.d.ts +12 -60
- package/dist/index.js +391 -313
- package/dist/index.js.map +1 -1
- package/dist/types.d.cts +3 -19
- package/dist/types.d.ts +3 -19
- package/package.json +98 -110
package/dist/index.cjs
CHANGED
|
@@ -17,6 +17,7 @@ if (typeof document !== 'undefined' && typeof window !== 'undefined') {
|
|
|
17
17
|
var react = require('react');
|
|
18
18
|
var jsxRuntime = require('react/jsx-runtime');
|
|
19
19
|
var sdk = require('@insforge/sdk');
|
|
20
|
+
var react$1 = require('@insforge/shared/react');
|
|
20
21
|
var lucideReact = require('lucide-react');
|
|
21
22
|
var zod = require('zod');
|
|
22
23
|
|
|
@@ -64,34 +65,82 @@ function useSearchParams() {
|
|
|
64
65
|
const adapter = useNavigationAdapter();
|
|
65
66
|
return adapter.useSearchParams();
|
|
66
67
|
}
|
|
67
|
-
var
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
68
|
+
var InsforgeManager = class _InsforgeManager {
|
|
69
|
+
// Static private instance
|
|
70
|
+
static instance = null;
|
|
71
|
+
// State storage
|
|
72
|
+
user = null;
|
|
73
|
+
isLoaded = false;
|
|
74
|
+
sdk;
|
|
75
|
+
listeners = /* @__PURE__ */ new Set();
|
|
76
|
+
// Config
|
|
77
|
+
config;
|
|
78
|
+
refreshIntervalRef = null;
|
|
79
|
+
hasProcessedCallbackRef = false;
|
|
80
|
+
// Private constructor (prevents external instantiation)
|
|
81
|
+
constructor(config) {
|
|
82
|
+
this.config = config;
|
|
83
|
+
this.sdk = sdk.createClient({ baseUrl: config.baseUrl });
|
|
84
|
+
}
|
|
85
|
+
// Public access method (Singleton core)
|
|
86
|
+
static getInstance(config) {
|
|
87
|
+
if (typeof window !== "undefined" && _InsforgeManager.instance) {
|
|
88
|
+
_InsforgeManager.instance.updateConfig(config);
|
|
89
|
+
return _InsforgeManager.instance;
|
|
90
|
+
}
|
|
91
|
+
_InsforgeManager.instance = new _InsforgeManager(config);
|
|
92
|
+
return _InsforgeManager.instance;
|
|
93
|
+
}
|
|
94
|
+
// Clear instance (used for testing or cleanup)
|
|
95
|
+
static clearInstance() {
|
|
96
|
+
if (_InsforgeManager.instance) {
|
|
97
|
+
_InsforgeManager.instance.cleanup();
|
|
98
|
+
}
|
|
99
|
+
_InsforgeManager.instance = null;
|
|
100
|
+
}
|
|
101
|
+
// Update config (for hot reloads)
|
|
102
|
+
updateConfig(config) {
|
|
103
|
+
this.config = { ...this.config, ...config };
|
|
104
|
+
}
|
|
105
|
+
// Public initialization method
|
|
106
|
+
async initialize() {
|
|
107
|
+
if (!this.isLoaded) {
|
|
108
|
+
await this.loadAuthState();
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
// Get current state
|
|
112
|
+
getState() {
|
|
113
|
+
return {
|
|
114
|
+
user: this.user,
|
|
115
|
+
isLoaded: this.isLoaded,
|
|
116
|
+
isSignedIn: !!this.user
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
// Subscription mechanism
|
|
120
|
+
subscribe(listener) {
|
|
121
|
+
this.listeners.add(listener);
|
|
122
|
+
return () => this.listeners.delete(listener);
|
|
123
|
+
}
|
|
124
|
+
notifyListeners() {
|
|
125
|
+
const state = this.getState();
|
|
126
|
+
this.listeners.forEach((listener) => listener(state));
|
|
127
|
+
}
|
|
128
|
+
// Load auth state
|
|
129
|
+
async loadAuthState() {
|
|
82
130
|
try {
|
|
83
|
-
const sessionResult =
|
|
131
|
+
const sessionResult = this.sdk.auth.getCurrentSession();
|
|
84
132
|
const session = sessionResult.data?.session;
|
|
85
133
|
const token = session?.accessToken || null;
|
|
86
134
|
if (!token) {
|
|
87
|
-
|
|
88
|
-
if (onAuthChange) {
|
|
89
|
-
onAuthChange(null);
|
|
135
|
+
this.user = null;
|
|
136
|
+
if (this.config.onAuthChange) {
|
|
137
|
+
this.config.onAuthChange(null);
|
|
90
138
|
}
|
|
91
|
-
|
|
139
|
+
this.isLoaded = true;
|
|
140
|
+
this.notifyListeners();
|
|
92
141
|
return { success: false, error: "no_session" };
|
|
93
142
|
}
|
|
94
|
-
const userResult = await
|
|
143
|
+
const userResult = await this.sdk.auth.getCurrentUser();
|
|
95
144
|
if (userResult.data) {
|
|
96
145
|
const profile = userResult.data.profile;
|
|
97
146
|
const userData = {
|
|
@@ -100,332 +149,363 @@ function InsforgeProviderCore({
|
|
|
100
149
|
name: profile?.nickname || "",
|
|
101
150
|
avatarUrl: profile?.avatarUrl || ""
|
|
102
151
|
};
|
|
103
|
-
|
|
104
|
-
if (onAuthChange) {
|
|
105
|
-
onAuthChange(userData);
|
|
152
|
+
this.user = userData;
|
|
153
|
+
if (this.config.onAuthChange) {
|
|
154
|
+
this.config.onAuthChange(userData);
|
|
106
155
|
}
|
|
107
|
-
|
|
156
|
+
this.isLoaded = true;
|
|
157
|
+
this.notifyListeners();
|
|
108
158
|
return { success: true };
|
|
109
159
|
} else {
|
|
110
|
-
await
|
|
111
|
-
if (onSignOut) {
|
|
160
|
+
await this.sdk.auth.signOut();
|
|
161
|
+
if (this.config.onSignOut) {
|
|
112
162
|
try {
|
|
113
|
-
await onSignOut();
|
|
163
|
+
await this.config.onSignOut();
|
|
114
164
|
} catch (error) {
|
|
115
165
|
if (error instanceof Error) {
|
|
116
|
-
console.error("[
|
|
166
|
+
console.error("[InsforgeManager] Error clearing cookie:", error.message);
|
|
117
167
|
}
|
|
118
168
|
}
|
|
119
169
|
}
|
|
120
|
-
|
|
121
|
-
if (onAuthChange) {
|
|
122
|
-
onAuthChange(null);
|
|
170
|
+
this.user = null;
|
|
171
|
+
if (this.config.onAuthChange) {
|
|
172
|
+
this.config.onAuthChange(null);
|
|
123
173
|
}
|
|
124
|
-
|
|
174
|
+
this.isLoaded = true;
|
|
175
|
+
this.notifyListeners();
|
|
125
176
|
return { success: false, error: "invalid_token" };
|
|
126
177
|
}
|
|
127
178
|
} catch (error) {
|
|
128
|
-
console.error("[
|
|
129
|
-
await
|
|
130
|
-
if (onSignOut) {
|
|
179
|
+
console.error("[InsforgeManager] Token validation failed:", error);
|
|
180
|
+
await this.sdk.auth.signOut();
|
|
181
|
+
if (this.config.onSignOut) {
|
|
131
182
|
try {
|
|
132
|
-
await onSignOut();
|
|
183
|
+
await this.config.onSignOut();
|
|
133
184
|
} catch (error2) {
|
|
134
185
|
if (error2 instanceof Error) {
|
|
135
|
-
console.error("[
|
|
186
|
+
console.error("[InsforgeManager] Error clearing cookie:", error2.message);
|
|
136
187
|
}
|
|
137
188
|
}
|
|
138
189
|
}
|
|
139
|
-
|
|
140
|
-
if (onAuthChange) {
|
|
141
|
-
onAuthChange(null);
|
|
190
|
+
this.user = null;
|
|
191
|
+
if (this.config.onAuthChange) {
|
|
192
|
+
this.config.onAuthChange(null);
|
|
142
193
|
}
|
|
143
|
-
|
|
194
|
+
this.isLoaded = true;
|
|
195
|
+
this.notifyListeners();
|
|
144
196
|
return {
|
|
145
197
|
success: false,
|
|
146
198
|
error: error instanceof Error ? error.message : "Authentication failed"
|
|
147
199
|
};
|
|
148
200
|
}
|
|
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;
|
|
201
|
+
}
|
|
202
|
+
// Helper to handle auth success
|
|
203
|
+
async handleAuthSuccess(authToken, fallbackUser) {
|
|
204
|
+
const userResult = await this.sdk.auth.getCurrentUser();
|
|
205
|
+
if (userResult.data) {
|
|
206
|
+
const profile = userResult.data.profile;
|
|
207
|
+
const userData = {
|
|
208
|
+
id: userResult.data.user.id,
|
|
209
|
+
email: userResult.data.user.email,
|
|
210
|
+
name: profile?.nickname || "",
|
|
211
|
+
avatarUrl: profile?.avatarUrl || ""
|
|
212
|
+
};
|
|
213
|
+
this.user = userData;
|
|
214
|
+
if (this.config.onAuthChange) {
|
|
215
|
+
this.config.onAuthChange(userData);
|
|
183
216
|
}
|
|
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
|
-
}
|
|
217
|
+
if (this.config.onSignIn) {
|
|
218
|
+
try {
|
|
219
|
+
await this.config.onSignIn(authToken);
|
|
220
|
+
} catch (error) {
|
|
221
|
+
if (error instanceof Error) {
|
|
222
|
+
console.error("[InsforgeManager] Error syncing token to cookie:", error.message);
|
|
211
223
|
}
|
|
212
224
|
}
|
|
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
225
|
}
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
if (
|
|
235
|
-
|
|
236
|
-
|
|
226
|
+
this.notifyListeners();
|
|
227
|
+
} else if (fallbackUser) {
|
|
228
|
+
const userData = {
|
|
229
|
+
id: fallbackUser.id || "",
|
|
230
|
+
email: fallbackUser.email || "",
|
|
231
|
+
name: fallbackUser.name || "",
|
|
232
|
+
avatarUrl: ""
|
|
233
|
+
};
|
|
234
|
+
this.user = userData;
|
|
235
|
+
if (this.config.onAuthChange) {
|
|
236
|
+
this.config.onAuthChange(userData);
|
|
237
|
+
}
|
|
238
|
+
this.notifyListeners();
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
// Business methods
|
|
242
|
+
async signIn(email, password) {
|
|
243
|
+
const sdkResult = await this.sdk.auth.signInWithPassword({
|
|
244
|
+
email,
|
|
245
|
+
password
|
|
246
|
+
});
|
|
247
|
+
if (sdkResult.data) {
|
|
248
|
+
await this.handleAuthSuccess(
|
|
249
|
+
sdkResult.data.accessToken || "",
|
|
250
|
+
sdkResult.data.user ? {
|
|
251
|
+
id: sdkResult.data.user.id,
|
|
252
|
+
email: sdkResult.data.user.email,
|
|
253
|
+
name: sdkResult.data.user.name
|
|
254
|
+
} : void 0
|
|
255
|
+
);
|
|
256
|
+
return sdkResult.data;
|
|
257
|
+
} else {
|
|
258
|
+
return {
|
|
259
|
+
error: sdkResult.error?.message || "Invalid email or password",
|
|
260
|
+
statusCode: sdkResult.error?.statusCode,
|
|
261
|
+
errorCode: sdkResult.error?.error
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
async signUp(email, password) {
|
|
266
|
+
const sdkResult = await this.sdk.auth.signUp({ email, password });
|
|
267
|
+
if (sdkResult.data) {
|
|
268
|
+
if (sdkResult.data.accessToken) {
|
|
269
|
+
await this.handleAuthSuccess(
|
|
270
|
+
sdkResult.data.accessToken,
|
|
237
271
|
sdkResult.data.user ? {
|
|
238
272
|
id: sdkResult.data.user.id,
|
|
239
273
|
email: sdkResult.data.user.email,
|
|
240
274
|
name: sdkResult.data.user.name
|
|
241
275
|
} : void 0
|
|
242
276
|
);
|
|
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
277
|
}
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
278
|
+
return sdkResult.data;
|
|
279
|
+
} else {
|
|
280
|
+
return {
|
|
281
|
+
error: sdkResult.error?.message || "Sign up failed",
|
|
282
|
+
statusCode: sdkResult.error?.statusCode,
|
|
283
|
+
errorCode: sdkResult.error?.error
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
async signOut() {
|
|
288
|
+
await this.sdk.auth.signOut();
|
|
289
|
+
if (this.config.onSignOut) {
|
|
282
290
|
try {
|
|
283
|
-
await onSignOut();
|
|
291
|
+
await this.config.onSignOut();
|
|
284
292
|
} catch (error) {
|
|
285
293
|
if (error instanceof Error) {
|
|
286
|
-
console.error("[
|
|
294
|
+
console.error("[InsforgeManager] Error clearing cookie:", error.message);
|
|
287
295
|
}
|
|
288
296
|
}
|
|
289
297
|
}
|
|
290
|
-
if (refreshIntervalRef
|
|
291
|
-
clearInterval(refreshIntervalRef
|
|
298
|
+
if (this.refreshIntervalRef) {
|
|
299
|
+
clearInterval(this.refreshIntervalRef);
|
|
300
|
+
this.refreshIntervalRef = null;
|
|
292
301
|
}
|
|
293
|
-
|
|
294
|
-
if (onAuthChange) {
|
|
295
|
-
onAuthChange(null);
|
|
302
|
+
this.user = null;
|
|
303
|
+
if (this.config.onAuthChange) {
|
|
304
|
+
this.config.onAuthChange(null);
|
|
296
305
|
}
|
|
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
|
-
}
|
|
306
|
+
this.notifyListeners();
|
|
307
|
+
}
|
|
308
|
+
async updateUser(data) {
|
|
309
|
+
if (!this.user) {
|
|
310
|
+
return { error: "No user signed in" };
|
|
311
|
+
}
|
|
312
|
+
const profileUpdate = {
|
|
313
|
+
nickname: data.name,
|
|
314
|
+
avatarUrl: data.avatarUrl
|
|
315
|
+
};
|
|
316
|
+
const result = await this.sdk.auth.setProfile(profileUpdate);
|
|
317
|
+
if (result.data) {
|
|
318
|
+
const userResult = await this.sdk.auth.getCurrentUser();
|
|
319
|
+
if (userResult.data) {
|
|
320
|
+
const profile = userResult.data.profile;
|
|
321
|
+
const updatedUser = {
|
|
322
|
+
id: userResult.data.user.id,
|
|
323
|
+
email: userResult.data.user.email,
|
|
324
|
+
name: profile?.nickname || "",
|
|
325
|
+
avatarUrl: profile?.avatarUrl || ""
|
|
326
|
+
};
|
|
327
|
+
this.user = updatedUser;
|
|
328
|
+
if (this.config.onAuthChange) {
|
|
329
|
+
this.config.onAuthChange(updatedUser);
|
|
322
330
|
}
|
|
323
|
-
|
|
324
|
-
} else {
|
|
325
|
-
return { error: result.error?.message || "Failed to update user" };
|
|
331
|
+
this.notifyListeners();
|
|
326
332
|
}
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
333
|
+
return null;
|
|
334
|
+
}
|
|
335
|
+
const error = result.error;
|
|
336
|
+
return { error: error?.message || "Failed to update user" };
|
|
337
|
+
}
|
|
338
|
+
async reloadAuth() {
|
|
339
|
+
return await this.loadAuthState();
|
|
340
|
+
}
|
|
341
|
+
async sendVerificationEmail(email) {
|
|
342
|
+
const sdkResult = await this.sdk.auth.sendVerificationEmail({ email });
|
|
343
|
+
return sdkResult.data;
|
|
344
|
+
}
|
|
345
|
+
async sendResetPasswordEmail(email) {
|
|
346
|
+
const sdkResult = await this.sdk.auth.sendResetPasswordEmail({ email });
|
|
347
|
+
return sdkResult.data;
|
|
348
|
+
}
|
|
349
|
+
async resetPassword(token, newPassword) {
|
|
350
|
+
const sdkResult = await this.sdk.auth.resetPassword({
|
|
351
|
+
newPassword,
|
|
352
|
+
otp: token
|
|
353
|
+
});
|
|
354
|
+
return sdkResult.data;
|
|
355
|
+
}
|
|
356
|
+
async verifyEmail(otp, email) {
|
|
357
|
+
const sdkResult = await this.sdk.auth.verifyEmail({ otp, email: email || void 0 });
|
|
358
|
+
if (sdkResult.data) {
|
|
340
359
|
return sdkResult.data;
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
360
|
+
} else {
|
|
361
|
+
return {
|
|
362
|
+
accessToken: "",
|
|
363
|
+
error: {
|
|
364
|
+
message: sdkResult.error?.message || "Email verification failed"
|
|
365
|
+
}
|
|
366
|
+
};
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
async exchangeResetPasswordToken(email, code) {
|
|
370
|
+
const sdkResult = await this.sdk.auth.exchangeResetPasswordToken({ email, code });
|
|
371
|
+
if (sdkResult.data) {
|
|
350
372
|
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;
|
|
373
|
+
} else {
|
|
374
|
+
return {
|
|
375
|
+
error: {
|
|
376
|
+
message: sdkResult.error?.message || "Failed to exchange reset password token"
|
|
377
|
+
}
|
|
378
|
+
};
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
async loginWithOAuth(provider, redirectTo) {
|
|
382
|
+
const sdkResult = await this.sdk.auth.signInWithOAuth({
|
|
383
|
+
provider,
|
|
384
|
+
redirectTo: redirectTo || window.location.origin + (this.config.afterSignInUrl || "/")
|
|
385
|
+
});
|
|
386
|
+
if (sdkResult.error) {
|
|
387
|
+
throw new Error(sdkResult.error.message);
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
async getPublicAuthConfig() {
|
|
391
|
+
try {
|
|
392
|
+
const result = await this.sdk.auth.getPublicAuthConfig();
|
|
393
|
+
if (result.data) {
|
|
394
|
+
return result.data;
|
|
375
395
|
} 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);
|
|
396
|
+
console.error("[InsforgeManager] Failed to get public auth config:", result.error);
|
|
397
|
+
return null;
|
|
393
398
|
}
|
|
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
|
-
|
|
399
|
+
} catch (error) {
|
|
400
|
+
console.error("[InsforgeManager] Failed to get public auth config:", error);
|
|
401
|
+
return null;
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
setUser(user) {
|
|
405
|
+
this.user = user;
|
|
406
|
+
if (this.config.onAuthChange) {
|
|
407
|
+
this.config.onAuthChange(user);
|
|
408
|
+
}
|
|
409
|
+
this.notifyListeners();
|
|
410
|
+
}
|
|
411
|
+
getConfig() {
|
|
412
|
+
return this.config;
|
|
413
|
+
}
|
|
414
|
+
getSDK() {
|
|
415
|
+
return this.sdk;
|
|
416
|
+
}
|
|
417
|
+
// Handle OAuth callback
|
|
418
|
+
handleOAuthCallback(isLoaded, user) {
|
|
419
|
+
if (!isLoaded || this.hasProcessedCallbackRef) {
|
|
420
|
+
return false;
|
|
421
|
+
}
|
|
422
|
+
const searchParams = new URLSearchParams(window.location.search);
|
|
423
|
+
const accessToken = searchParams.get("access_token");
|
|
424
|
+
if (accessToken && !!user) {
|
|
425
|
+
this.hasProcessedCallbackRef = true;
|
|
426
|
+
const url = new URL(window.location.href);
|
|
427
|
+
url.search = "";
|
|
428
|
+
window.history.replaceState({}, "", url.toString());
|
|
429
|
+
setTimeout(() => {
|
|
430
|
+
window.location.href = this.config.afterSignInUrl || "/";
|
|
431
|
+
}, 100);
|
|
432
|
+
return true;
|
|
421
433
|
}
|
|
434
|
+
return false;
|
|
435
|
+
}
|
|
436
|
+
// Cleanup
|
|
437
|
+
cleanup() {
|
|
438
|
+
if (this.refreshIntervalRef) {
|
|
439
|
+
clearInterval(this.refreshIntervalRef);
|
|
440
|
+
this.refreshIntervalRef = null;
|
|
441
|
+
}
|
|
442
|
+
this.listeners.clear();
|
|
443
|
+
}
|
|
444
|
+
};
|
|
445
|
+
function InsforgeProviderCore({
|
|
446
|
+
children,
|
|
447
|
+
baseUrl,
|
|
448
|
+
afterSignInUrl = "/",
|
|
449
|
+
onAuthChange,
|
|
450
|
+
onSignIn,
|
|
451
|
+
onSignOut
|
|
452
|
+
}) {
|
|
453
|
+
const manager = react.useMemo(
|
|
454
|
+
() => InsforgeManager.getInstance({
|
|
455
|
+
baseUrl,
|
|
456
|
+
afterSignInUrl,
|
|
457
|
+
onAuthChange,
|
|
458
|
+
onSignIn,
|
|
459
|
+
onSignOut
|
|
460
|
+
}),
|
|
461
|
+
[baseUrl, afterSignInUrl, onAuthChange, onSignIn, onSignOut]
|
|
422
462
|
);
|
|
463
|
+
const [state, setState] = react.useState(() => manager.getState());
|
|
464
|
+
react.useEffect(() => {
|
|
465
|
+
const unsubscribe = manager.subscribe((newState) => {
|
|
466
|
+
setState(newState);
|
|
467
|
+
});
|
|
468
|
+
void manager.initialize();
|
|
469
|
+
return unsubscribe;
|
|
470
|
+
}, [manager]);
|
|
471
|
+
react.useEffect(() => {
|
|
472
|
+
if (typeof window !== "undefined") {
|
|
473
|
+
manager.handleOAuthCallback(state.isLoaded, state.user);
|
|
474
|
+
}
|
|
475
|
+
}, [manager, state.isLoaded, state.user]);
|
|
476
|
+
const contextValue = react.useMemo(
|
|
477
|
+
() => ({
|
|
478
|
+
// State from Manager
|
|
479
|
+
user: state.user,
|
|
480
|
+
isLoaded: state.isLoaded,
|
|
481
|
+
isSignedIn: state.isSignedIn,
|
|
482
|
+
// Methods delegated to Manager
|
|
483
|
+
setUser: (user) => manager.setUser(user),
|
|
484
|
+
signIn: (email, password) => manager.signIn(email, password),
|
|
485
|
+
signUp: (email, password) => manager.signUp(email, password),
|
|
486
|
+
signOut: () => manager.signOut(),
|
|
487
|
+
updateUser: (data) => manager.updateUser(data),
|
|
488
|
+
reloadAuth: () => manager.reloadAuth(),
|
|
489
|
+
sendVerificationEmail: (email) => manager.sendVerificationEmail(email),
|
|
490
|
+
sendResetPasswordEmail: (email) => manager.sendResetPasswordEmail(email),
|
|
491
|
+
resetPassword: (token, newPassword) => manager.resetPassword(token, newPassword),
|
|
492
|
+
verifyEmail: (otp, email) => manager.verifyEmail(otp, email),
|
|
493
|
+
exchangeResetPasswordToken: (email, code) => manager.exchangeResetPasswordToken(email, code),
|
|
494
|
+
loginWithOAuth: (provider, redirectTo) => manager.loginWithOAuth(provider, redirectTo),
|
|
495
|
+
getPublicAuthConfig: () => manager.getPublicAuthConfig(),
|
|
496
|
+
// Config
|
|
497
|
+
baseUrl: manager.getConfig().baseUrl,
|
|
498
|
+
afterSignInUrl: manager.getConfig().afterSignInUrl || "/"
|
|
499
|
+
}),
|
|
500
|
+
[state, manager]
|
|
501
|
+
);
|
|
502
|
+
return /* @__PURE__ */ jsxRuntime.jsx(react$1.InsforgeContext.Provider, { value: contextValue, children });
|
|
423
503
|
}
|
|
424
504
|
function InsforgeProvider(props) {
|
|
425
505
|
return /* @__PURE__ */ jsxRuntime.jsx(NavigationProvider, { adapter: BrowserNavigationAdapter, children: /* @__PURE__ */ jsxRuntime.jsx(InsforgeProviderCore, { ...props }) });
|
|
426
506
|
}
|
|
427
507
|
function useInsforge() {
|
|
428
|
-
const context = react.useContext(InsforgeContext);
|
|
508
|
+
const context = react.useContext(react$1.InsforgeContext);
|
|
429
509
|
if (!context) {
|
|
430
510
|
return {
|
|
431
511
|
user: null,
|
|
@@ -433,20 +513,18 @@ function useInsforge() {
|
|
|
433
513
|
isSignedIn: false,
|
|
434
514
|
setUser: () => {
|
|
435
515
|
},
|
|
436
|
-
signIn:
|
|
437
|
-
signUp:
|
|
438
|
-
signOut:
|
|
439
|
-
},
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
},
|
|
449
|
-
getPublicAuthConfig: async () => null,
|
|
516
|
+
signIn: () => Promise.resolve({ error: "SSR mode" }),
|
|
517
|
+
signUp: () => Promise.resolve({ error: "SSR mode" }),
|
|
518
|
+
signOut: () => Promise.resolve(),
|
|
519
|
+
updateUser: () => Promise.resolve({ error: "SSR mode" }),
|
|
520
|
+
reloadAuth: () => Promise.resolve({ success: false, error: "SSR mode" }),
|
|
521
|
+
sendVerificationEmail: () => Promise.resolve(null),
|
|
522
|
+
sendResetPasswordEmail: () => Promise.resolve(null),
|
|
523
|
+
resetPassword: () => Promise.resolve(null),
|
|
524
|
+
verifyEmail: () => Promise.resolve(null),
|
|
525
|
+
exchangeResetPasswordToken: () => Promise.resolve({ error: { message: "SSR mode" } }),
|
|
526
|
+
loginWithOAuth: () => Promise.resolve(),
|
|
527
|
+
getPublicAuthConfig: () => Promise.resolve(null),
|
|
450
528
|
baseUrl: "",
|
|
451
529
|
afterSignInUrl: "/"
|
|
452
530
|
};
|
|
@@ -468,7 +546,7 @@ function usePublicAuthConfig() {
|
|
|
468
546
|
}
|
|
469
547
|
setIsLoaded(true);
|
|
470
548
|
}
|
|
471
|
-
fetchConfig();
|
|
549
|
+
void fetchConfig();
|
|
472
550
|
}, [getPublicAuthConfig]);
|
|
473
551
|
return { authConfig, isLoaded };
|
|
474
552
|
}
|
|
@@ -1437,7 +1515,7 @@ function SignIn({ onError, ...uiProps }) {
|
|
|
1437
1515
|
}
|
|
1438
1516
|
function handleOAuth(provider) {
|
|
1439
1517
|
try {
|
|
1440
|
-
loginWithOAuth(provider, redirectUrl || "");
|
|
1518
|
+
void loginWithOAuth(provider, redirectUrl || "");
|
|
1441
1519
|
} catch (err) {
|
|
1442
1520
|
const errorMessage = err instanceof Error ? err.message : "OAuth login failed";
|
|
1443
1521
|
setError(errorMessage);
|
|
@@ -1456,7 +1534,7 @@ function SignIn({ onError, ...uiProps }) {
|
|
|
1456
1534
|
password,
|
|
1457
1535
|
onEmailChange: setEmail,
|
|
1458
1536
|
onPasswordChange: setPassword,
|
|
1459
|
-
onSubmit: handleSubmit,
|
|
1537
|
+
onSubmit: (e) => void handleSubmit(e),
|
|
1460
1538
|
error,
|
|
1461
1539
|
loading,
|
|
1462
1540
|
oauthLoading,
|
|
@@ -1714,7 +1792,7 @@ function SignUp({ onError, ...uiProps }) {
|
|
|
1714
1792
|
}
|
|
1715
1793
|
function handleOAuth(provider) {
|
|
1716
1794
|
try {
|
|
1717
|
-
loginWithOAuth(provider, redirectUrl || "");
|
|
1795
|
+
void loginWithOAuth(provider, redirectUrl || "");
|
|
1718
1796
|
} catch (err) {
|
|
1719
1797
|
const errorMessage = err instanceof Error ? err.message : "OAuth login failed";
|
|
1720
1798
|
setError(errorMessage);
|
|
@@ -1733,7 +1811,7 @@ function SignUp({ onError, ...uiProps }) {
|
|
|
1733
1811
|
password,
|
|
1734
1812
|
onEmailChange: setEmail,
|
|
1735
1813
|
onPasswordChange: setPassword,
|
|
1736
|
-
onSubmit: handleSubmit,
|
|
1814
|
+
onSubmit: (e) => void handleSubmit(e),
|
|
1737
1815
|
error,
|
|
1738
1816
|
loading,
|
|
1739
1817
|
oauthLoading,
|
|
@@ -2043,7 +2121,7 @@ function ForgotPassword({ onError, ...uiProps }) {
|
|
|
2043
2121
|
confirmPassword,
|
|
2044
2122
|
onNewPasswordChange: setNewPassword,
|
|
2045
2123
|
onConfirmPasswordChange: setConfirmPassword,
|
|
2046
|
-
onSubmit: handleResetPasswordSubmit,
|
|
2124
|
+
onSubmit: (e) => void handleResetPasswordSubmit(e),
|
|
2047
2125
|
error,
|
|
2048
2126
|
loading,
|
|
2049
2127
|
success,
|
|
@@ -2218,7 +2296,7 @@ function ResetPassword({ onError, ...uiProps }) {
|
|
|
2218
2296
|
confirmPassword,
|
|
2219
2297
|
onNewPasswordChange: setNewPassword,
|
|
2220
2298
|
onConfirmPasswordChange: setConfirmPassword,
|
|
2221
|
-
onSubmit: handleSubmit,
|
|
2299
|
+
onSubmit: (e) => void handleSubmit(e),
|
|
2222
2300
|
error,
|
|
2223
2301
|
loading,
|
|
2224
2302
|
success: false,
|
|
@@ -2342,7 +2420,7 @@ function UserButton({ afterSignOutUrl = "/", mode = "detailed" }) {
|
|
|
2342
2420
|
setImageError(true);
|
|
2343
2421
|
}
|
|
2344
2422
|
};
|
|
2345
|
-
checkImageUrl();
|
|
2423
|
+
void checkImageUrl();
|
|
2346
2424
|
}, [user?.avatarUrl]);
|
|
2347
2425
|
react.useEffect(() => {
|
|
2348
2426
|
function handleClickOutside(event) {
|
|
@@ -2395,7 +2473,7 @@ function UserButton({ afterSignOutUrl = "/", mode = "detailed" }) {
|
|
|
2395
2473
|
isOpen && /* @__PURE__ */ jsxRuntime.jsx("div", { className: "if-userButton-menu", children: /* @__PURE__ */ jsxRuntime.jsxs(
|
|
2396
2474
|
"button",
|
|
2397
2475
|
{
|
|
2398
|
-
onClick: handleSignOut,
|
|
2476
|
+
onClick: () => void handleSignOut(),
|
|
2399
2477
|
className: "if-userButton-menuItem if-userButton-menuItem-signout",
|
|
2400
2478
|
children: [
|
|
2401
2479
|
/* @__PURE__ */ jsxRuntime.jsx(lucideReact.LogOut, { className: "if-userButton-menuItem-icon" }),
|