@insforge/nextjs 0.5.6 → 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/README.md +119 -5
- package/dist/api.d.mts +12 -1
- package/dist/api.d.ts +12 -1
- package/dist/api.js +58 -2
- package/dist/api.js.map +1 -1
- package/dist/api.mjs +56 -1
- package/dist/api.mjs.map +1 -1
- package/dist/index.d.mts +5 -1
- package/dist/index.d.ts +5 -1
- package/dist/index.js +45 -5
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +45 -5
- package/dist/index.mjs.map +1 -1
- package/dist/middleware.d.mts +7 -1
- package/dist/middleware.d.ts +7 -1
- package/dist/middleware.js +52 -9
- package/dist/middleware.js.map +1 -1
- package/dist/middleware.mjs +48 -8
- package/dist/middleware.mjs.map +1 -1
- package/package.json +4 -4
package/dist/index.js
CHANGED
|
@@ -85,11 +85,15 @@ async function fetchOAuthProviders(baseUrl) {
|
|
|
85
85
|
}
|
|
86
86
|
}
|
|
87
87
|
function getTokenFromSDK() {
|
|
88
|
+
console.log("[InsforgeProvider] Getting token from SDK");
|
|
88
89
|
if (typeof window === "undefined") return null;
|
|
90
|
+
console.log("[InsforgeProvider] Window:", window);
|
|
89
91
|
try {
|
|
90
92
|
const token = localStorage.getItem("insforge-auth-token");
|
|
93
|
+
console.log("[InsforgeProvider] Token:", token);
|
|
91
94
|
return token;
|
|
92
95
|
} catch (error) {
|
|
96
|
+
console.error("[InsforgeProvider] Error getting token from SDK:", error);
|
|
93
97
|
return null;
|
|
94
98
|
}
|
|
95
99
|
}
|
|
@@ -116,7 +120,9 @@ async function syncTokenToCookie(token) {
|
|
|
116
120
|
function InsforgeProvider({
|
|
117
121
|
children,
|
|
118
122
|
baseUrl,
|
|
119
|
-
|
|
123
|
+
frontendUrl,
|
|
124
|
+
onAuthChange,
|
|
125
|
+
useBuiltInAuth = true
|
|
120
126
|
}) {
|
|
121
127
|
const [user, setUser] = (0, import_react.useState)(null);
|
|
122
128
|
const [session, setSession] = (0, import_react.useState)(null);
|
|
@@ -145,33 +151,63 @@ function InsforgeProvider({
|
|
|
145
151
|
setIsLoaded(true);
|
|
146
152
|
return;
|
|
147
153
|
}
|
|
154
|
+
const cachedUserStr = localStorage.getItem("insforge-user-profile");
|
|
155
|
+
if (cachedUserStr) {
|
|
156
|
+
try {
|
|
157
|
+
const cachedData = JSON.parse(cachedUserStr);
|
|
158
|
+
if (cachedData.user) {
|
|
159
|
+
console.log("[InsforgeProvider] Loading user from cache");
|
|
160
|
+
const userData = {
|
|
161
|
+
id: cachedData.user.id,
|
|
162
|
+
email: cachedData.user.email,
|
|
163
|
+
createdAt: cachedData.user.createdAt || (/* @__PURE__ */ new Date()).toISOString(),
|
|
164
|
+
updatedAt: cachedData.user.updatedAt || (/* @__PURE__ */ new Date()).toISOString(),
|
|
165
|
+
...cachedData.profile
|
|
166
|
+
};
|
|
167
|
+
setUser(userData);
|
|
168
|
+
setSession({
|
|
169
|
+
userId: cachedData.user.id,
|
|
170
|
+
token,
|
|
171
|
+
expiresAt: "",
|
|
172
|
+
createdAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
173
|
+
});
|
|
174
|
+
if (onAuthChange) {
|
|
175
|
+
onAuthChange(userData);
|
|
176
|
+
}
|
|
177
|
+
setIsLoaded(true);
|
|
178
|
+
}
|
|
179
|
+
} catch (e) {
|
|
180
|
+
console.warn("[InsforgeProvider] Failed to parse cached user data:", e);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
148
183
|
try {
|
|
149
184
|
await syncTokenToCookie(token);
|
|
150
185
|
} catch (error) {
|
|
151
186
|
}
|
|
152
187
|
const userResult = await insforge.auth.getCurrentUser();
|
|
153
188
|
if (userResult.data) {
|
|
189
|
+
console.log("[InsforgeProvider] User data refreshed from API");
|
|
154
190
|
const userData = {
|
|
155
191
|
id: userResult.data.user.id,
|
|
156
192
|
email: userResult.data.user.email,
|
|
157
193
|
createdAt: userResult.data.user.createdAt || (/* @__PURE__ */ new Date()).toISOString(),
|
|
158
194
|
updatedAt: userResult.data.user.updatedAt || (/* @__PURE__ */ new Date()).toISOString(),
|
|
159
195
|
...userResult.data.profile
|
|
160
|
-
// Add profile data if available
|
|
161
196
|
};
|
|
162
197
|
setUser(userData);
|
|
163
198
|
setSession({
|
|
164
199
|
userId: userResult.data.user.id,
|
|
165
200
|
token,
|
|
166
201
|
expiresAt: "",
|
|
167
|
-
// Insforge tokens are long-lived
|
|
168
202
|
createdAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
169
203
|
});
|
|
204
|
+
localStorage.setItem("insforge-user-profile", JSON.stringify(userResult.data));
|
|
170
205
|
if (onAuthChange) {
|
|
171
206
|
onAuthChange(userData);
|
|
172
207
|
}
|
|
173
208
|
} else {
|
|
174
209
|
localStorage.removeItem("insforge-auth-token");
|
|
210
|
+
localStorage.removeItem("insforge-user-profile");
|
|
175
211
|
try {
|
|
176
212
|
await fetch("/api/auth", { method: "DELETE" });
|
|
177
213
|
} catch (error) {
|
|
@@ -183,7 +219,9 @@ function InsforgeProvider({
|
|
|
183
219
|
}
|
|
184
220
|
}
|
|
185
221
|
} catch (error) {
|
|
222
|
+
console.error("[InsforgeProvider] Token validation failed:", error);
|
|
186
223
|
localStorage.removeItem("insforge-auth-token");
|
|
224
|
+
localStorage.removeItem("insforge-user-profile");
|
|
187
225
|
try {
|
|
188
226
|
await fetch("/api/auth", { method: "DELETE" });
|
|
189
227
|
} catch (error2) {
|
|
@@ -274,6 +312,7 @@ function InsforgeProvider({
|
|
|
274
312
|
);
|
|
275
313
|
const signOut = (0, import_react.useCallback)(async () => {
|
|
276
314
|
await insforge.auth.signOut();
|
|
315
|
+
localStorage.removeItem("insforge-user-profile");
|
|
277
316
|
await fetch("/api/auth", { method: "DELETE" }).catch(() => {
|
|
278
317
|
});
|
|
279
318
|
if (refreshIntervalRef.current) {
|
|
@@ -361,6 +400,7 @@ function InsforgeProvider({
|
|
|
361
400
|
session,
|
|
362
401
|
isLoaded,
|
|
363
402
|
isSignedIn: !!user,
|
|
403
|
+
setUser,
|
|
364
404
|
signIn,
|
|
365
405
|
signUp,
|
|
366
406
|
signOut,
|
|
@@ -395,8 +435,8 @@ function useAuth() {
|
|
|
395
435
|
|
|
396
436
|
// src/hooks/useUser.ts
|
|
397
437
|
function useUser() {
|
|
398
|
-
const { user, isLoaded, updateUser } = useInsforge();
|
|
399
|
-
return { user, isLoaded, updateUser };
|
|
438
|
+
const { user, isLoaded, updateUser, setUser } = useInsforge();
|
|
439
|
+
return { user, isLoaded, updateUser, setUser };
|
|
400
440
|
}
|
|
401
441
|
|
|
402
442
|
// src/hooks/useSession.ts
|