@dream-api/sdk 0.1.6 → 0.1.7
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/index.js +57 -3
- package/dist/index.mjs +57 -3
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -191,6 +191,7 @@ var ClerkManager = class {
|
|
|
191
191
|
constructor(onTokenChange) {
|
|
192
192
|
this.loaded = false;
|
|
193
193
|
this.token = null;
|
|
194
|
+
this.bootstrapUser = null;
|
|
194
195
|
this.onTokenChange = onTokenChange;
|
|
195
196
|
}
|
|
196
197
|
/**
|
|
@@ -198,6 +199,16 @@ var ClerkManager = class {
|
|
|
198
199
|
*/
|
|
199
200
|
async load() {
|
|
200
201
|
if (this.loaded || typeof window === "undefined") return;
|
|
202
|
+
const bootstrapToken = this.checkBootstrapToken();
|
|
203
|
+
if (bootstrapToken) {
|
|
204
|
+
this.token = bootstrapToken.token;
|
|
205
|
+
this.bootstrapUser = bootstrapToken.user;
|
|
206
|
+
this.onTokenChange?.(this.token);
|
|
207
|
+
this.loaded = true;
|
|
208
|
+
console.log("[DreamSDK] Bootstrap token found, user:", bootstrapToken.user.email);
|
|
209
|
+
this.removeTokenFromUrl();
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
201
212
|
const existingClerk = getClerk();
|
|
202
213
|
if (existingClerk) {
|
|
203
214
|
this.loaded = true;
|
|
@@ -221,6 +232,45 @@ var ClerkManager = class {
|
|
|
221
232
|
this.loaded = true;
|
|
222
233
|
await this.checkSession();
|
|
223
234
|
}
|
|
235
|
+
/**
|
|
236
|
+
* Check for bootstrap token in URL (from sign-up worker redirect)
|
|
237
|
+
*/
|
|
238
|
+
checkBootstrapToken() {
|
|
239
|
+
if (typeof window === "undefined") return null;
|
|
240
|
+
const params = new URLSearchParams(window.location.search);
|
|
241
|
+
const token = params.get("__dream_token");
|
|
242
|
+
if (!token) return null;
|
|
243
|
+
try {
|
|
244
|
+
const parts = token.split(".");
|
|
245
|
+
if (parts.length !== 3) return null;
|
|
246
|
+
const payload = JSON.parse(atob(parts[1].replace(/-/g, "+").replace(/_/g, "/")));
|
|
247
|
+
if (payload.exp && payload.exp < Math.floor(Date.now() / 1e3)) {
|
|
248
|
+
console.log("[DreamSDK] Bootstrap token expired");
|
|
249
|
+
return null;
|
|
250
|
+
}
|
|
251
|
+
return {
|
|
252
|
+
token,
|
|
253
|
+
user: {
|
|
254
|
+
id: payload.userId,
|
|
255
|
+
email: payload.email || "",
|
|
256
|
+
plan: payload.plan || "free",
|
|
257
|
+
publishableKey: payload.publishableKey || ""
|
|
258
|
+
}
|
|
259
|
+
};
|
|
260
|
+
} catch (err) {
|
|
261
|
+
console.error("[DreamSDK] Failed to parse bootstrap token:", err);
|
|
262
|
+
return null;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* Remove bootstrap token from URL (clean up after reading)
|
|
267
|
+
*/
|
|
268
|
+
removeTokenFromUrl() {
|
|
269
|
+
if (typeof window === "undefined") return;
|
|
270
|
+
const url = new URL(window.location.href);
|
|
271
|
+
url.searchParams.delete("__dream_token");
|
|
272
|
+
window.history.replaceState({}, "", url.toString());
|
|
273
|
+
}
|
|
224
274
|
/**
|
|
225
275
|
* Check if returning from auth and grab token
|
|
226
276
|
*/
|
|
@@ -240,6 +290,7 @@ var ClerkManager = class {
|
|
|
240
290
|
* Check if user is signed in
|
|
241
291
|
*/
|
|
242
292
|
isSignedIn() {
|
|
293
|
+
if (this.bootstrapUser && this.token) return true;
|
|
243
294
|
const clerk = getClerk();
|
|
244
295
|
return !!clerk?.user && !!clerk?.session;
|
|
245
296
|
}
|
|
@@ -247,6 +298,7 @@ var ClerkManager = class {
|
|
|
247
298
|
* Get current user info
|
|
248
299
|
*/
|
|
249
300
|
getUser() {
|
|
301
|
+
if (this.bootstrapUser) return this.bootstrapUser;
|
|
250
302
|
const clerk = getClerk();
|
|
251
303
|
if (!clerk?.user) return null;
|
|
252
304
|
const user = clerk.user;
|
|
@@ -284,11 +336,13 @@ var ClerkManager = class {
|
|
|
284
336
|
* Sign out
|
|
285
337
|
*/
|
|
286
338
|
async signOut() {
|
|
287
|
-
|
|
288
|
-
if (!clerk) return;
|
|
289
|
-
await clerk.signOut();
|
|
339
|
+
this.bootstrapUser = null;
|
|
290
340
|
this.token = null;
|
|
291
341
|
this.onTokenChange?.(null);
|
|
342
|
+
const clerk = getClerk();
|
|
343
|
+
if (clerk) {
|
|
344
|
+
await clerk.signOut();
|
|
345
|
+
}
|
|
292
346
|
}
|
|
293
347
|
/**
|
|
294
348
|
* Check if we're returning from auth (has clerk params/cookies)
|
package/dist/index.mjs
CHANGED
|
@@ -163,6 +163,7 @@ var ClerkManager = class {
|
|
|
163
163
|
constructor(onTokenChange) {
|
|
164
164
|
this.loaded = false;
|
|
165
165
|
this.token = null;
|
|
166
|
+
this.bootstrapUser = null;
|
|
166
167
|
this.onTokenChange = onTokenChange;
|
|
167
168
|
}
|
|
168
169
|
/**
|
|
@@ -170,6 +171,16 @@ var ClerkManager = class {
|
|
|
170
171
|
*/
|
|
171
172
|
async load() {
|
|
172
173
|
if (this.loaded || typeof window === "undefined") return;
|
|
174
|
+
const bootstrapToken = this.checkBootstrapToken();
|
|
175
|
+
if (bootstrapToken) {
|
|
176
|
+
this.token = bootstrapToken.token;
|
|
177
|
+
this.bootstrapUser = bootstrapToken.user;
|
|
178
|
+
this.onTokenChange?.(this.token);
|
|
179
|
+
this.loaded = true;
|
|
180
|
+
console.log("[DreamSDK] Bootstrap token found, user:", bootstrapToken.user.email);
|
|
181
|
+
this.removeTokenFromUrl();
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
173
184
|
const existingClerk = getClerk();
|
|
174
185
|
if (existingClerk) {
|
|
175
186
|
this.loaded = true;
|
|
@@ -193,6 +204,45 @@ var ClerkManager = class {
|
|
|
193
204
|
this.loaded = true;
|
|
194
205
|
await this.checkSession();
|
|
195
206
|
}
|
|
207
|
+
/**
|
|
208
|
+
* Check for bootstrap token in URL (from sign-up worker redirect)
|
|
209
|
+
*/
|
|
210
|
+
checkBootstrapToken() {
|
|
211
|
+
if (typeof window === "undefined") return null;
|
|
212
|
+
const params = new URLSearchParams(window.location.search);
|
|
213
|
+
const token = params.get("__dream_token");
|
|
214
|
+
if (!token) return null;
|
|
215
|
+
try {
|
|
216
|
+
const parts = token.split(".");
|
|
217
|
+
if (parts.length !== 3) return null;
|
|
218
|
+
const payload = JSON.parse(atob(parts[1].replace(/-/g, "+").replace(/_/g, "/")));
|
|
219
|
+
if (payload.exp && payload.exp < Math.floor(Date.now() / 1e3)) {
|
|
220
|
+
console.log("[DreamSDK] Bootstrap token expired");
|
|
221
|
+
return null;
|
|
222
|
+
}
|
|
223
|
+
return {
|
|
224
|
+
token,
|
|
225
|
+
user: {
|
|
226
|
+
id: payload.userId,
|
|
227
|
+
email: payload.email || "",
|
|
228
|
+
plan: payload.plan || "free",
|
|
229
|
+
publishableKey: payload.publishableKey || ""
|
|
230
|
+
}
|
|
231
|
+
};
|
|
232
|
+
} catch (err) {
|
|
233
|
+
console.error("[DreamSDK] Failed to parse bootstrap token:", err);
|
|
234
|
+
return null;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Remove bootstrap token from URL (clean up after reading)
|
|
239
|
+
*/
|
|
240
|
+
removeTokenFromUrl() {
|
|
241
|
+
if (typeof window === "undefined") return;
|
|
242
|
+
const url = new URL(window.location.href);
|
|
243
|
+
url.searchParams.delete("__dream_token");
|
|
244
|
+
window.history.replaceState({}, "", url.toString());
|
|
245
|
+
}
|
|
196
246
|
/**
|
|
197
247
|
* Check if returning from auth and grab token
|
|
198
248
|
*/
|
|
@@ -212,6 +262,7 @@ var ClerkManager = class {
|
|
|
212
262
|
* Check if user is signed in
|
|
213
263
|
*/
|
|
214
264
|
isSignedIn() {
|
|
265
|
+
if (this.bootstrapUser && this.token) return true;
|
|
215
266
|
const clerk = getClerk();
|
|
216
267
|
return !!clerk?.user && !!clerk?.session;
|
|
217
268
|
}
|
|
@@ -219,6 +270,7 @@ var ClerkManager = class {
|
|
|
219
270
|
* Get current user info
|
|
220
271
|
*/
|
|
221
272
|
getUser() {
|
|
273
|
+
if (this.bootstrapUser) return this.bootstrapUser;
|
|
222
274
|
const clerk = getClerk();
|
|
223
275
|
if (!clerk?.user) return null;
|
|
224
276
|
const user = clerk.user;
|
|
@@ -256,11 +308,13 @@ var ClerkManager = class {
|
|
|
256
308
|
* Sign out
|
|
257
309
|
*/
|
|
258
310
|
async signOut() {
|
|
259
|
-
|
|
260
|
-
if (!clerk) return;
|
|
261
|
-
await clerk.signOut();
|
|
311
|
+
this.bootstrapUser = null;
|
|
262
312
|
this.token = null;
|
|
263
313
|
this.onTokenChange?.(null);
|
|
314
|
+
const clerk = getClerk();
|
|
315
|
+
if (clerk) {
|
|
316
|
+
await clerk.signOut();
|
|
317
|
+
}
|
|
264
318
|
}
|
|
265
319
|
/**
|
|
266
320
|
* Check if we're returning from auth (has clerk params/cookies)
|