@livedesk/client 0.1.58 → 0.1.59

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.
@@ -406,6 +406,29 @@ function createFileStorage(filePath) {
406
406
  };
407
407
  }
408
408
 
409
+ function readSavedSessionFromFile() {
410
+ try {
411
+ const state = JSON.parse(readFileSync(CLIENT_AUTH_PATH, 'utf8'));
412
+ const raw = state?.[CLIENT_AUTH_STORAGE_KEY];
413
+ if (typeof raw !== 'string' || !raw.trim()) {
414
+ return null;
415
+ }
416
+ const session = JSON.parse(raw);
417
+ return session?.access_token ? session : null;
418
+ } catch {
419
+ return null;
420
+ }
421
+ }
422
+
423
+ function writeSavedSessionToFile(session) {
424
+ if (!session?.access_token || !session?.refresh_token) {
425
+ return false;
426
+ }
427
+ const storage = createFileStorage(CLIENT_AUTH_PATH);
428
+ storage.setItem(CLIENT_AUTH_STORAGE_KEY, JSON.stringify(session));
429
+ return true;
430
+ }
431
+
409
432
  async function createSupabaseClient() {
410
433
  const { createClient } = await import('@supabase/supabase-js');
411
434
  return createClient(SUPABASE_URL, SUPABASE_PUBLISHABLE_KEY, {
@@ -461,19 +484,22 @@ function formatDiscoveryError(error) {
461
484
 
462
485
  async function refreshSessionIfNeeded(supabase) {
463
486
  const { data: existing } = await supabase.auth.getSession();
464
- const session = existing?.session;
487
+ const session = existing?.session || readSavedSessionFromFile();
465
488
  if (!session?.access_token) {
466
489
  return null;
467
490
  }
468
491
  const expiresAt = Number(session.expires_at || 0);
469
492
  if (expiresAt <= 0 || expiresAt - Math.floor(Date.now() / 1000) > SESSION_REFRESH_SKEW_SECONDS) {
493
+ writeSavedSessionToFile(session);
470
494
  return session;
471
495
  }
472
496
  const { data, error } = await supabase.auth.refreshSession(session);
473
497
  if (error) {
474
498
  throw error;
475
499
  }
476
- return data?.session || session;
500
+ const refreshed = data?.session || session;
501
+ writeSavedSessionToFile(refreshed);
502
+ return refreshed;
477
503
  }
478
504
 
479
505
  function openBrowser(url) {
@@ -1560,6 +1586,7 @@ async function startConnectionChoiceServer(supabase, options = {}) {
1560
1586
  pendingStartup = normalizeStartupChoice(requestUrl.searchParams.get('startup'), pendingStartup);
1561
1587
  const { data: existing } = await supabase.auth.getSession();
1562
1588
  if (existing?.session?.access_token) {
1589
+ writeSavedSessionToFile(existing.session);
1563
1590
  applyStartupPreference(pendingStartup, startupArgs);
1564
1591
  const choice = { type: 'google', session: existing.session };
1565
1592
  complete(choice, 'LiveDesk client dashboard', 'Signed in. The client is starting automatically.');
@@ -1653,6 +1680,7 @@ async function startConnectionChoiceServer(supabase, options = {}) {
1653
1680
  server.close();
1654
1681
  return;
1655
1682
  }
1683
+ writeSavedSessionToFile(sessionData.session);
1656
1684
  applyStartupPreference(pendingStartup, startupArgs);
1657
1685
  const choice = { type: 'google', session: sessionData.session };
1658
1686
  complete(choice, 'LiveDesk client dashboard', 'Signed in. The client is starting automatically.');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@livedesk/client",
3
- "version": "0.1.58",
3
+ "version": "0.1.59",
4
4
  "description": "LiveDesk local remote client",
5
5
  "type": "module",
6
6
  "bin": {