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