@clianta/sdk 1.6.5 → 1.6.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.
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * Clianta SDK v1.6.5
2
+ * Clianta SDK v1.6.7
3
3
  * (c) 2026 Clianta
4
4
  * Released under the MIT License.
5
5
  */
@@ -8,7 +8,7 @@
8
8
  * @see SDK_VERSION in core/config.ts
9
9
  */
10
10
  /** SDK Version */
11
- const SDK_VERSION = '1.6.5';
11
+ const SDK_VERSION = '1.6.7';
12
12
  /** Default API endpoint — reads from env or falls back to localhost */
13
13
  const getDefaultApiEndpoint = () => {
14
14
  // Next.js (process.env)
@@ -2312,19 +2312,26 @@ class AutoIdentifyPlugin extends BasePlugin {
2312
2312
  if (typeof window === 'undefined')
2313
2313
  return;
2314
2314
  // First check after 2 seconds (give auth providers time to init)
2315
- setTimeout(() => this.checkForAuthUser(), 2000);
2315
+ setTimeout(() => {
2316
+ try {
2317
+ this.checkForAuthUser();
2318
+ }
2319
+ catch { /* silently fail */ }
2320
+ }, 2000);
2316
2321
  // Then check periodically
2317
2322
  this.checkInterval = setInterval(() => {
2318
2323
  this.checkCount++;
2319
2324
  if (this.checkCount >= this.MAX_CHECKS) {
2320
- // Stop checking after MAX_CHECKS — user probably isn't logged in
2321
2325
  if (this.checkInterval) {
2322
2326
  clearInterval(this.checkInterval);
2323
2327
  this.checkInterval = null;
2324
2328
  }
2325
2329
  return;
2326
2330
  }
2327
- this.checkForAuthUser();
2331
+ try {
2332
+ this.checkForAuthUser();
2333
+ }
2334
+ catch { /* silently fail */ }
2328
2335
  }, this.CHECK_INTERVAL_MS);
2329
2336
  }
2330
2337
  destroy() {
@@ -2340,24 +2347,131 @@ class AutoIdentifyPlugin extends BasePlugin {
2340
2347
  checkForAuthUser() {
2341
2348
  if (!this.tracker || this.identifiedEmail)
2342
2349
  return;
2343
- // 1. Check cookies for JWTs
2344
- const cookieUser = this.checkCookies();
2345
- if (cookieUser) {
2346
- this.identifyUser(cookieUser);
2347
- return;
2350
+ // 0. Check well-known auth provider globals (most reliable)
2351
+ try {
2352
+ const providerUser = this.checkAuthProviders();
2353
+ if (providerUser) {
2354
+ this.identifyUser(providerUser);
2355
+ return;
2356
+ }
2348
2357
  }
2349
- // 2. Check localStorage
2350
- const localUser = this.checkStorage(localStorage);
2351
- if (localUser) {
2352
- this.identifyUser(localUser);
2353
- return;
2358
+ catch { /* provider check failed */ }
2359
+ try {
2360
+ // 1. Check cookies for JWTs
2361
+ const cookieUser = this.checkCookies();
2362
+ if (cookieUser) {
2363
+ this.identifyUser(cookieUser);
2364
+ return;
2365
+ }
2354
2366
  }
2355
- // 3. Check sessionStorage
2356
- const sessionUser = this.checkStorage(sessionStorage);
2357
- if (sessionUser) {
2358
- this.identifyUser(sessionUser);
2359
- return;
2367
+ catch { /* cookie access blocked */ }
2368
+ try {
2369
+ // 2. Check localStorage
2370
+ if (typeof localStorage !== 'undefined') {
2371
+ const localUser = this.checkStorage(localStorage);
2372
+ if (localUser) {
2373
+ this.identifyUser(localUser);
2374
+ return;
2375
+ }
2376
+ }
2377
+ }
2378
+ catch { /* localStorage access blocked */ }
2379
+ try {
2380
+ // 3. Check sessionStorage
2381
+ if (typeof sessionStorage !== 'undefined') {
2382
+ const sessionUser = this.checkStorage(sessionStorage);
2383
+ if (sessionUser) {
2384
+ this.identifyUser(sessionUser);
2385
+ return;
2386
+ }
2387
+ }
2388
+ }
2389
+ catch { /* sessionStorage access blocked */ }
2390
+ }
2391
+ /**
2392
+ * Check well-known auth provider globals on window
2393
+ * These are the most reliable — they expose user data directly
2394
+ */
2395
+ checkAuthProviders() {
2396
+ const win = window;
2397
+ // ─── Clerk ───
2398
+ // Clerk exposes window.Clerk after initialization
2399
+ try {
2400
+ const clerkUser = win.Clerk?.user;
2401
+ if (clerkUser) {
2402
+ const email = clerkUser.primaryEmailAddress?.emailAddress
2403
+ || clerkUser.emailAddresses?.[0]?.emailAddress;
2404
+ if (email) {
2405
+ return {
2406
+ email,
2407
+ firstName: clerkUser.firstName || undefined,
2408
+ lastName: clerkUser.lastName || undefined,
2409
+ };
2410
+ }
2411
+ }
2412
+ }
2413
+ catch { /* Clerk not available */ }
2414
+ // ─── Firebase Auth ───
2415
+ try {
2416
+ const fbAuth = win.firebase?.auth?.();
2417
+ const fbUser = fbAuth?.currentUser;
2418
+ if (fbUser?.email) {
2419
+ const parts = (fbUser.displayName || '').split(' ');
2420
+ return {
2421
+ email: fbUser.email,
2422
+ firstName: parts[0] || undefined,
2423
+ lastName: parts.slice(1).join(' ') || undefined,
2424
+ };
2425
+ }
2426
+ }
2427
+ catch { /* Firebase not available */ }
2428
+ // ─── Supabase ───
2429
+ try {
2430
+ const sbClient = win.__SUPABASE_CLIENT__ || win.supabase;
2431
+ if (sbClient?.auth) {
2432
+ // Supabase v2 stores session
2433
+ const session = sbClient.auth.session?.() || sbClient.auth.getSession?.();
2434
+ const user = session?.data?.session?.user || session?.user;
2435
+ if (user?.email) {
2436
+ const meta = user.user_metadata || {};
2437
+ return {
2438
+ email: user.email,
2439
+ firstName: meta.first_name || meta.full_name?.split(' ')[0] || undefined,
2440
+ lastName: meta.last_name || meta.full_name?.split(' ').slice(1).join(' ') || undefined,
2441
+ };
2442
+ }
2443
+ }
2444
+ }
2445
+ catch { /* Supabase not available */ }
2446
+ // ─── Auth0 SPA ───
2447
+ try {
2448
+ const auth0 = win.__auth0Client || win.auth0Client;
2449
+ if (auth0?.isAuthenticated?.()) {
2450
+ const user = auth0.getUser?.();
2451
+ if (user?.email) {
2452
+ return {
2453
+ email: user.email,
2454
+ firstName: user.given_name || user.name?.split(' ')[0] || undefined,
2455
+ lastName: user.family_name || user.name?.split(' ').slice(1).join(' ') || undefined,
2456
+ };
2457
+ }
2458
+ }
2360
2459
  }
2460
+ catch { /* Auth0 not available */ }
2461
+ // ─── Global clianta identify hook ───
2462
+ // Any auth system can set: window.__clianta_user = { email, firstName, lastName }
2463
+ try {
2464
+ const manualUser = win.__clianta_user;
2465
+ if (manualUser?.email && typeof manualUser.email === 'string' && manualUser.email.includes('@')) {
2466
+ return {
2467
+ email: manualUser.email,
2468
+ firstName: manualUser.firstName || undefined,
2469
+ lastName: manualUser.lastName || undefined,
2470
+ };
2471
+ }
2472
+ }
2473
+ catch { /* manual user not set */ }
2474
+ return null;
2361
2475
  }
2362
2476
  /**
2363
2477
  * Identify the user and stop checking