@gofreego/tsutils 0.1.24 → 0.1.25

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.d.mts CHANGED
@@ -175,13 +175,10 @@ interface ISessionManager {
175
175
  getSessionId(): string | undefined;
176
176
  /** True when a non-expired access token is stored. */
177
177
  isAuthenticated(): boolean;
178
- /** Called on app startup: restore the auth header or wipe an expired session. */
179
- initialize(): void;
180
178
  }
181
179
  declare class SessionManager implements ISessionManager {
182
180
  private static instance;
183
181
  private readonly key;
184
- private readonly client;
185
182
  private cache;
186
183
  private constructor();
187
184
  static getInstance(client: HttpClient): SessionManager;
@@ -193,9 +190,6 @@ declare class SessionManager implements ISessionManager {
193
190
  getRefreshToken(): string | undefined;
194
191
  getSessionId(): string | undefined;
195
192
  isAuthenticated(): boolean;
196
- initialize(): void;
197
- private setAuthToken;
198
- private clearAuthToken;
199
193
  }
200
194
 
201
195
  interface IAuthService {
package/dist/index.d.ts CHANGED
@@ -175,13 +175,10 @@ interface ISessionManager {
175
175
  getSessionId(): string | undefined;
176
176
  /** True when a non-expired access token is stored. */
177
177
  isAuthenticated(): boolean;
178
- /** Called on app startup: restore the auth header or wipe an expired session. */
179
- initialize(): void;
180
178
  }
181
179
  declare class SessionManager implements ISessionManager {
182
180
  private static instance;
183
181
  private readonly key;
184
- private readonly client;
185
182
  private cache;
186
183
  private constructor();
187
184
  static getInstance(client: HttpClient): SessionManager;
@@ -193,9 +190,6 @@ declare class SessionManager implements ISessionManager {
193
190
  getRefreshToken(): string | undefined;
194
191
  getSessionId(): string | undefined;
195
192
  isAuthenticated(): boolean;
196
- initialize(): void;
197
- private setAuthToken;
198
- private clearAuthToken;
199
193
  }
200
194
 
201
195
  interface IAuthService {
package/dist/index.js CHANGED
@@ -1429,8 +1429,8 @@ function formatDate(date, options = {
1429
1429
  var SESSION_KEY = "session_details";
1430
1430
  var SessionManager = class _SessionManager {
1431
1431
  constructor(client, key = SESSION_KEY) {
1432
+ // private readonly client: HttpClient
1432
1433
  this.cache = null;
1433
- this.client = client;
1434
1434
  this.key = key;
1435
1435
  }
1436
1436
  static getInstance(client) {
@@ -1442,7 +1442,6 @@ var SessionManager = class _SessionManager {
1442
1442
  save(session) {
1443
1443
  this.cache = session;
1444
1444
  LocalStorage.setItem(this.key, session);
1445
- this.setAuthToken(session.accessToken);
1446
1445
  }
1447
1446
  patch(updates) {
1448
1447
  const current = this.get();
@@ -1450,14 +1449,10 @@ var SessionManager = class _SessionManager {
1450
1449
  const updated = { ...current, ...updates };
1451
1450
  this.cache = updated;
1452
1451
  LocalStorage.setItem(this.key, updated);
1453
- if (updates.accessToken) {
1454
- this.setAuthToken(updates.accessToken);
1455
- }
1456
1452
  }
1457
1453
  clear() {
1458
1454
  this.cache = null;
1459
1455
  LocalStorage.removeItem(this.key);
1460
- this.clearAuthToken();
1461
1456
  }
1462
1457
  get() {
1463
1458
  if (this.cache !== null) return this.cache;
@@ -1478,20 +1473,6 @@ var SessionManager = class _SessionManager {
1478
1473
  if (!session?.accessToken) return false;
1479
1474
  return Number(session.expiresAt) > Date.now() / 1e3;
1480
1475
  }
1481
- initialize() {
1482
- const session = this.get();
1483
- if (session?.accessToken && Number(session.expiresAt) > Date.now() / 1e3) {
1484
- this.setAuthToken(session.accessToken);
1485
- } else {
1486
- this.clear();
1487
- }
1488
- }
1489
- setAuthToken(token) {
1490
- this.client.setDefaultHeader("Authorization", `Bearer ${token}`);
1491
- }
1492
- clearAuthToken() {
1493
- this.client.removeDefaultHeader("Authorization");
1494
- }
1495
1476
  };
1496
1477
 
1497
1478
  // src/auth/authService.ts
@@ -1514,6 +1495,7 @@ var AuthService = class _AuthService {
1514
1495
  );
1515
1496
  if (response.data.accessToken) {
1516
1497
  this.sessionManager.save(response.data);
1498
+ this.client.setDefaultHeader("Authorization", `Bearer ${response.data.accessToken}`);
1517
1499
  }
1518
1500
  return response.data;
1519
1501
  }
@@ -1531,6 +1513,7 @@ var AuthService = class _AuthService {
1531
1513
  accessToken: response.data.accessToken,
1532
1514
  refreshToken: response.data.refreshToken
1533
1515
  });
1516
+ this.client.setDefaultHeader("Authorization", `Bearer ${response.data.accessToken}`);
1534
1517
  }
1535
1518
  return response.data;
1536
1519
  }
@@ -1544,6 +1527,7 @@ var AuthService = class _AuthService {
1544
1527
  return response.data;
1545
1528
  } finally {
1546
1529
  this.sessionManager.clear();
1530
+ this.client.removeDefaultHeader("Authorization");
1547
1531
  }
1548
1532
  }
1549
1533
  async generateLoginToken(request) {
@@ -1560,6 +1544,7 @@ var AuthService = class _AuthService {
1560
1544
  );
1561
1545
  if (response.data.accessToken) {
1562
1546
  this.sessionManager.save(response.data);
1547
+ this.client.setDefaultHeader("Authorization", `Bearer ${response.data.accessToken}`);
1563
1548
  }
1564
1549
  return response.data;
1565
1550
  }
@@ -1576,7 +1561,12 @@ var AuthService = class _AuthService {
1576
1561
  return this.sessionManager.getSessionId();
1577
1562
  }
1578
1563
  initializeAuth() {
1579
- this.sessionManager.initialize();
1564
+ const session = this.sessionManager.get();
1565
+ if (session?.accessToken && Number(session.expiresAt) > Date.now() / 1e3) {
1566
+ this.client.setDefaultHeader("Authorization", `Bearer ${session.accessToken}`);
1567
+ } else {
1568
+ this.client.removeDefaultHeader("Authorization");
1569
+ }
1580
1570
  }
1581
1571
  };
1582
1572