@clianta/sdk 1.6.4 → 1.6.6

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.4
2
+ * Clianta SDK v1.6.6
3
3
  * (c) 2026 Clianta
4
4
  * Released under the MIT License.
5
5
  */
@@ -11,7 +11,7 @@ var _documentCurrentScript = typeof document !== 'undefined' ? document.currentS
11
11
  * @see SDK_VERSION in core/config.ts
12
12
  */
13
13
  /** SDK Version */
14
- const SDK_VERSION = '1.6.4';
14
+ const SDK_VERSION = '1.6.6';
15
15
  /** Default API endpoint — reads from env or falls back to localhost */
16
16
  const getDefaultApiEndpoint = () => {
17
17
  // Next.js (process.env)
@@ -50,6 +50,7 @@ const DEFAULT_PLUGINS = [
50
50
  'exitIntent',
51
51
  'errors',
52
52
  'performance',
53
+ 'autoIdentify',
53
54
  ];
54
55
  /** Default configuration values */
55
56
  const DEFAULT_CONFIG = {
@@ -2242,6 +2243,316 @@ class PopupFormsPlugin extends BasePlugin {
2242
2243
  }
2243
2244
  }
2244
2245
 
2246
+ /**
2247
+ * Clianta SDK - Auto-Identify Plugin
2248
+ * Automatically detects logged-in users by checking JWT tokens in
2249
+ * cookies, localStorage, and sessionStorage. Works with any auth provider:
2250
+ * Clerk, Firebase, Auth0, Supabase, NextAuth, Passport, custom JWT, etc.
2251
+ *
2252
+ * How it works:
2253
+ * 1. On init + periodically, scans for JWT tokens
2254
+ * 2. Decodes the JWT payload (base64, no secret needed)
2255
+ * 3. Extracts email/name from standard JWT claims
2256
+ * 4. Calls tracker.identify() automatically
2257
+ *
2258
+ * @see SDK_VERSION in core/config.ts
2259
+ */
2260
+ /** Known auth cookie patterns and their JWT locations */
2261
+ const AUTH_COOKIE_PATTERNS = [
2262
+ // Clerk
2263
+ '__session',
2264
+ '__clerk_db_jwt',
2265
+ // NextAuth
2266
+ 'next-auth.session-token',
2267
+ '__Secure-next-auth.session-token',
2268
+ // Supabase
2269
+ 'sb-access-token',
2270
+ // Auth0
2271
+ 'auth0.is.authenticated',
2272
+ // Firebase — uses localStorage, handled separately
2273
+ // Generic patterns
2274
+ 'token',
2275
+ 'jwt',
2276
+ 'access_token',
2277
+ 'session_token',
2278
+ 'auth_token',
2279
+ 'id_token',
2280
+ ];
2281
+ /** localStorage/sessionStorage key patterns for auth tokens */
2282
+ const STORAGE_KEY_PATTERNS = [
2283
+ // Supabase
2284
+ 'sb-',
2285
+ 'supabase.auth.',
2286
+ // Firebase
2287
+ 'firebase:authUser:',
2288
+ // Auth0
2289
+ 'auth0spajs',
2290
+ '@@auth0spajs@@',
2291
+ // Generic
2292
+ 'token',
2293
+ 'jwt',
2294
+ 'auth',
2295
+ 'user',
2296
+ 'session',
2297
+ ];
2298
+ /** Standard JWT claim fields for email */
2299
+ const EMAIL_CLAIMS = ['email', 'sub', 'preferred_username', 'user_email', 'mail'];
2300
+ const NAME_CLAIMS = ['name', 'full_name', 'display_name', 'given_name'];
2301
+ const FIRST_NAME_CLAIMS = ['given_name', 'first_name', 'firstName'];
2302
+ const LAST_NAME_CLAIMS = ['family_name', 'last_name', 'lastName'];
2303
+ class AutoIdentifyPlugin extends BasePlugin {
2304
+ constructor() {
2305
+ super(...arguments);
2306
+ this.name = 'autoIdentify';
2307
+ this.checkInterval = null;
2308
+ this.identifiedEmail = null;
2309
+ this.checkCount = 0;
2310
+ this.MAX_CHECKS = 30; // Stop checking after ~5 minutes
2311
+ this.CHECK_INTERVAL_MS = 10000; // Check every 10 seconds
2312
+ }
2313
+ init(tracker) {
2314
+ super.init(tracker);
2315
+ if (typeof window === 'undefined')
2316
+ return;
2317
+ // First check after 2 seconds (give auth providers time to init)
2318
+ setTimeout(() => {
2319
+ try {
2320
+ this.checkForAuthUser();
2321
+ }
2322
+ catch { /* silently fail */ }
2323
+ }, 2000);
2324
+ // Then check periodically
2325
+ this.checkInterval = setInterval(() => {
2326
+ this.checkCount++;
2327
+ if (this.checkCount >= this.MAX_CHECKS) {
2328
+ if (this.checkInterval) {
2329
+ clearInterval(this.checkInterval);
2330
+ this.checkInterval = null;
2331
+ }
2332
+ return;
2333
+ }
2334
+ try {
2335
+ this.checkForAuthUser();
2336
+ }
2337
+ catch { /* silently fail */ }
2338
+ }, this.CHECK_INTERVAL_MS);
2339
+ }
2340
+ destroy() {
2341
+ if (this.checkInterval) {
2342
+ clearInterval(this.checkInterval);
2343
+ this.checkInterval = null;
2344
+ }
2345
+ super.destroy();
2346
+ }
2347
+ /**
2348
+ * Main check — scan all sources for auth tokens
2349
+ */
2350
+ checkForAuthUser() {
2351
+ if (!this.tracker || this.identifiedEmail)
2352
+ return;
2353
+ try {
2354
+ // 1. Check cookies for JWTs
2355
+ const cookieUser = this.checkCookies();
2356
+ if (cookieUser) {
2357
+ this.identifyUser(cookieUser);
2358
+ return;
2359
+ }
2360
+ }
2361
+ catch { /* cookie access blocked */ }
2362
+ try {
2363
+ // 2. Check localStorage
2364
+ if (typeof localStorage !== 'undefined') {
2365
+ const localUser = this.checkStorage(localStorage);
2366
+ if (localUser) {
2367
+ this.identifyUser(localUser);
2368
+ return;
2369
+ }
2370
+ }
2371
+ }
2372
+ catch { /* localStorage access blocked */ }
2373
+ try {
2374
+ // 3. Check sessionStorage
2375
+ if (typeof sessionStorage !== 'undefined') {
2376
+ const sessionUser = this.checkStorage(sessionStorage);
2377
+ if (sessionUser) {
2378
+ this.identifyUser(sessionUser);
2379
+ return;
2380
+ }
2381
+ }
2382
+ }
2383
+ catch { /* sessionStorage access blocked */ }
2384
+ }
2385
+ /**
2386
+ * Identify the user and stop checking
2387
+ */
2388
+ identifyUser(user) {
2389
+ if (!this.tracker || this.identifiedEmail === user.email)
2390
+ return;
2391
+ this.identifiedEmail = user.email;
2392
+ this.tracker.identify(user.email, {
2393
+ firstName: user.firstName,
2394
+ lastName: user.lastName,
2395
+ });
2396
+ // Stop interval — we found the user
2397
+ if (this.checkInterval) {
2398
+ clearInterval(this.checkInterval);
2399
+ this.checkInterval = null;
2400
+ }
2401
+ }
2402
+ /**
2403
+ * Scan cookies for JWT tokens
2404
+ */
2405
+ checkCookies() {
2406
+ if (typeof document === 'undefined')
2407
+ return null;
2408
+ try {
2409
+ const cookies = document.cookie.split(';').map(c => c.trim());
2410
+ for (const cookie of cookies) {
2411
+ const [name, ...valueParts] = cookie.split('=');
2412
+ const value = valueParts.join('=');
2413
+ const cookieName = name.trim().toLowerCase();
2414
+ // Check if this cookie matches known auth patterns
2415
+ const isAuthCookie = AUTH_COOKIE_PATTERNS.some(pattern => cookieName.includes(pattern.toLowerCase()));
2416
+ if (isAuthCookie && value) {
2417
+ const user = this.extractUserFromToken(decodeURIComponent(value));
2418
+ if (user)
2419
+ return user;
2420
+ }
2421
+ }
2422
+ }
2423
+ catch {
2424
+ // Cookie access may fail in some environments
2425
+ }
2426
+ return null;
2427
+ }
2428
+ /**
2429
+ * Scan localStorage or sessionStorage for auth tokens
2430
+ */
2431
+ checkStorage(storage) {
2432
+ try {
2433
+ for (let i = 0; i < storage.length; i++) {
2434
+ const key = storage.key(i);
2435
+ if (!key)
2436
+ continue;
2437
+ const keyLower = key.toLowerCase();
2438
+ const isAuthKey = STORAGE_KEY_PATTERNS.some(pattern => keyLower.includes(pattern.toLowerCase()));
2439
+ if (isAuthKey) {
2440
+ const value = storage.getItem(key);
2441
+ if (!value)
2442
+ continue;
2443
+ // Try as direct JWT
2444
+ const user = this.extractUserFromToken(value);
2445
+ if (user)
2446
+ return user;
2447
+ // Try as JSON containing a token
2448
+ try {
2449
+ const json = JSON.parse(value);
2450
+ const user = this.extractUserFromJson(json);
2451
+ if (user)
2452
+ return user;
2453
+ }
2454
+ catch {
2455
+ // Not JSON, skip
2456
+ }
2457
+ }
2458
+ }
2459
+ }
2460
+ catch {
2461
+ // Storage access may fail (iframe, security restrictions)
2462
+ }
2463
+ return null;
2464
+ }
2465
+ /**
2466
+ * Try to extract user info from a JWT token string
2467
+ */
2468
+ extractUserFromToken(token) {
2469
+ // JWT format: header.payload.signature
2470
+ const parts = token.split('.');
2471
+ if (parts.length !== 3)
2472
+ return null;
2473
+ try {
2474
+ const payload = JSON.parse(atob(parts[1].replace(/-/g, '+').replace(/_/g, '/')));
2475
+ return this.extractUserFromClaims(payload);
2476
+ }
2477
+ catch {
2478
+ return null;
2479
+ }
2480
+ }
2481
+ /**
2482
+ * Extract user info from a JSON object (e.g., Firebase auth user stored in localStorage)
2483
+ */
2484
+ extractUserFromJson(data) {
2485
+ if (!data || typeof data !== 'object')
2486
+ return null;
2487
+ // Direct user object
2488
+ const user = this.extractUserFromClaims(data);
2489
+ if (user)
2490
+ return user;
2491
+ // Nested: { user: { email } } or { data: { user: { email } } }
2492
+ for (const key of ['user', 'data', 'session', 'currentUser', 'authUser', 'access_token', 'token']) {
2493
+ if (data[key]) {
2494
+ if (typeof data[key] === 'string') {
2495
+ // Might be a JWT inside JSON
2496
+ const tokenUser = this.extractUserFromToken(data[key]);
2497
+ if (tokenUser)
2498
+ return tokenUser;
2499
+ }
2500
+ else if (typeof data[key] === 'object') {
2501
+ const nestedUser = this.extractUserFromClaims(data[key]);
2502
+ if (nestedUser)
2503
+ return nestedUser;
2504
+ }
2505
+ }
2506
+ }
2507
+ return null;
2508
+ }
2509
+ /**
2510
+ * Extract user from JWT claims or user object
2511
+ */
2512
+ extractUserFromClaims(claims) {
2513
+ if (!claims || typeof claims !== 'object')
2514
+ return null;
2515
+ // Find email
2516
+ let email = null;
2517
+ for (const claim of EMAIL_CLAIMS) {
2518
+ const value = claims[claim];
2519
+ if (value && typeof value === 'string' && value.includes('@') && value.includes('.')) {
2520
+ email = value;
2521
+ break;
2522
+ }
2523
+ }
2524
+ if (!email)
2525
+ return null;
2526
+ // Find name
2527
+ let firstName;
2528
+ let lastName;
2529
+ for (const claim of FIRST_NAME_CLAIMS) {
2530
+ if (claims[claim] && typeof claims[claim] === 'string') {
2531
+ firstName = claims[claim];
2532
+ break;
2533
+ }
2534
+ }
2535
+ for (const claim of LAST_NAME_CLAIMS) {
2536
+ if (claims[claim] && typeof claims[claim] === 'string') {
2537
+ lastName = claims[claim];
2538
+ break;
2539
+ }
2540
+ }
2541
+ // If no first/last name, try full name
2542
+ if (!firstName) {
2543
+ for (const claim of NAME_CLAIMS) {
2544
+ if (claims[claim] && typeof claims[claim] === 'string') {
2545
+ const parts = claims[claim].split(' ');
2546
+ firstName = parts[0];
2547
+ lastName = lastName || parts.slice(1).join(' ') || undefined;
2548
+ break;
2549
+ }
2550
+ }
2551
+ }
2552
+ return { email, firstName, lastName };
2553
+ }
2554
+ }
2555
+
2245
2556
  /**
2246
2557
  * Clianta SDK - Plugins Index
2247
2558
  * Version is defined in core/config.ts as SDK_VERSION
@@ -2271,6 +2582,8 @@ function getPlugin(name) {
2271
2582
  return new PerformancePlugin();
2272
2583
  case 'popupForms':
2273
2584
  return new PopupFormsPlugin();
2585
+ case 'autoIdentify':
2586
+ return new AutoIdentifyPlugin();
2274
2587
  default:
2275
2588
  throw new Error(`Unknown plugin: ${name}`);
2276
2589
  }