glitch-javascript-sdk 2.6.7 → 2.6.9

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/cjs/index.js CHANGED
@@ -18766,12 +18766,6 @@ var Requests = /** @class */ (function () {
18766
18766
  var Storage = /** @class */ (function () {
18767
18767
  function Storage() {
18768
18768
  }
18769
- /**
18770
- * Sets a root level domain so the data can persist across
18771
- * subdomains
18772
- *
18773
- * @param rootDomain
18774
- */
18775
18769
  Storage.setRootDomain = function (rootDomain) {
18776
18770
  Storage.rootDomain = rootDomain;
18777
18771
  };
@@ -18779,55 +18773,58 @@ var Storage = /** @class */ (function () {
18779
18773
  return Storage.rootDomain ? "".concat(Storage.rootDomain, ":").concat(key) : key;
18780
18774
  };
18781
18775
  Storage.set = function (key, value) {
18782
- try {
18783
- var serializedValue = JSON.stringify(value);
18784
- window.localStorage.setItem(Storage.getStorageKey(key), serializedValue);
18785
- }
18786
- catch (e) {
18776
+ // 1. Always update in-memory fallback for the current process
18777
+ Storage.data[key] = value;
18778
+ // 2. Only attempt browser storage if window exists
18779
+ if (typeof window !== 'undefined') {
18787
18780
  try {
18788
18781
  var serializedValue = JSON.stringify(value);
18789
- window.sessionStorage.setItem(Storage.getStorageKey(key), serializedValue);
18782
+ window.localStorage.setItem(Storage.getStorageKey(key), serializedValue);
18790
18783
  }
18791
18784
  catch (e) {
18792
18785
  try {
18793
- this.setCookie(key, value, 31);
18786
+ var serializedValue = JSON.stringify(value);
18787
+ window.sessionStorage.setItem(Storage.getStorageKey(key), serializedValue);
18794
18788
  }
18795
18789
  catch (e) {
18790
+ try {
18791
+ this.setCookie(key, value, 31);
18792
+ }
18793
+ catch (e) { }
18796
18794
  }
18797
- Storage.data[key] = value;
18798
18795
  }
18799
18796
  }
18800
18797
  };
18801
18798
  Storage.get = function (key) {
18802
- try {
18803
- var serializedValue = window.localStorage.getItem(Storage.getStorageKey(key));
18804
- if (serializedValue !== null) {
18805
- return JSON.parse(serializedValue);
18806
- }
18807
- }
18808
- catch (e) {
18799
+ // 1. Try Browser Storage if available
18800
+ if (typeof window !== 'undefined') {
18809
18801
  try {
18810
- var serializedValue = window.sessionStorage.getItem(Storage.getStorageKey(key));
18811
- if (serializedValue !== null) {
18802
+ var serializedValue = window.localStorage.getItem(Storage.getStorageKey(key));
18803
+ if (serializedValue !== null)
18812
18804
  return JSON.parse(serializedValue);
18813
- }
18814
18805
  }
18815
18806
  catch (e) {
18816
- var value = null;
18817
18807
  try {
18818
- value = Storage.getCookie(key);
18819
- }
18820
- catch (e) {
18821
- }
18822
- if (!value) {
18823
- value = Storage.data[key];
18808
+ var serializedValue = window.sessionStorage.getItem(Storage.getStorageKey(key));
18809
+ if (serializedValue !== null)
18810
+ return JSON.parse(serializedValue);
18824
18811
  }
18825
- return value;
18812
+ catch (e) { }
18826
18813
  }
18827
18814
  }
18815
+ // 2. Try Cookie (getCookie is now SSR safe)
18816
+ var value = null;
18817
+ try {
18818
+ value = Storage.getCookie(key);
18819
+ }
18820
+ catch (e) { }
18821
+ // 3. Fallback to in-memory data
18822
+ if (!value) {
18823
+ value = Storage.data[key];
18824
+ }
18825
+ return value;
18828
18826
  };
18829
18827
  Storage.setAuthToken = function (token) {
18830
- // Always set the cookie if we have a root domain to ensure cross-subdomain sync
18831
18828
  if (Storage.rootDomain) {
18832
18829
  if (token) {
18833
18830
  this.setCookie('glitch_auth_token', token, 31);
@@ -18836,20 +18833,18 @@ var Storage = /** @class */ (function () {
18836
18833
  this.eraseCookie('glitch_auth_token');
18837
18834
  }
18838
18835
  }
18839
- // Still set localStorage for the current domain
18840
18836
  Storage.set('glitch_auth_token', token);
18841
18837
  };
18842
18838
  Storage.getAuthToken = function () {
18843
- // 1. Try Cookie first (best for cross-subdomain)
18844
18839
  var token = Storage.getCookie('glitch_auth_token');
18845
- // 2. Fallback to LocalStorage
18846
18840
  if (!token || token === 'null') {
18847
18841
  token = Storage.get('glitch_auth_token');
18848
18842
  }
18849
18843
  return (token === 'null' || !token) ? null : token;
18850
18844
  };
18851
18845
  Storage.eraseCookie = function (name) {
18852
- if (document) {
18846
+ // Use typeof check to prevent ReferenceError
18847
+ if (typeof document !== 'undefined') {
18853
18848
  document.cookie =
18854
18849
  name +
18855
18850
  '=; Secure; HttpOnly=false; SameSite=none; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';
@@ -18863,7 +18858,6 @@ var Storage = /** @class */ (function () {
18863
18858
  expires = '; expires=' + date.toUTCString();
18864
18859
  }
18865
18860
  if (typeof document !== 'undefined') {
18866
- // If rootDomain is .glitch.fun, this works for all subdomains
18867
18861
  document.cookie =
18868
18862
  name +
18869
18863
  '=' +
@@ -18875,7 +18869,8 @@ var Storage = /** @class */ (function () {
18875
18869
  }
18876
18870
  };
18877
18871
  Storage.getCookie = function (name) {
18878
- if (document) {
18872
+ // Use typeof check to prevent ReferenceError
18873
+ if (typeof document !== 'undefined') {
18879
18874
  var nameEQ = name + '=';
18880
18875
  var ca = document.cookie.split(';');
18881
18876
  for (var i = 0; i < ca.length; i++) {
@@ -18888,6 +18883,26 @@ var Storage = /** @class */ (function () {
18888
18883
  }
18889
18884
  return null;
18890
18885
  };
18886
+ Storage.setTokenExpiry = function (expiresInSeconds) {
18887
+ var expiryTime = Date.now() + (expiresInSeconds * 1000);
18888
+ Storage.set('glitch_token_expiry', expiryTime);
18889
+ if (Storage.rootDomain && typeof document !== 'undefined') {
18890
+ this.setCookie('glitch_token_expiry', expiryTime.toString(), 31);
18891
+ }
18892
+ };
18893
+ Storage.getTokenExpiry = function () {
18894
+ var expiry = Storage.getCookie('glitch_token_expiry');
18895
+ if (!expiry) {
18896
+ expiry = Storage.get('glitch_token_expiry');
18897
+ }
18898
+ return expiry ? parseInt(expiry) : null;
18899
+ };
18900
+ Storage.isTokenExpired = function () {
18901
+ var expiry = this.getTokenExpiry();
18902
+ if (!expiry)
18903
+ return false;
18904
+ return Date.now() > expiry;
18905
+ };
18891
18906
  Storage.rootDomain = '';
18892
18907
  Storage.data = {};
18893
18908
  return Storage;
@@ -30330,6 +30345,11 @@ var Session = /** @class */ (function () {
30330
30345
  }
30331
30346
  Session.isLoggedIn = function () {
30332
30347
  var authToken = Storage.getAuthToken();
30348
+ var expired = Storage.isTokenExpired();
30349
+ if (expired) {
30350
+ Session.end(); // Auto-clear if expired
30351
+ return false;
30352
+ }
30333
30353
  return authToken !== null && authToken !== 'null' && authToken !== undefined;
30334
30354
  };
30335
30355
  Session.getAuthToken = function () {
@@ -30353,6 +30373,8 @@ var Session = /** @class */ (function () {
30353
30373
  };
30354
30374
  Session.end = function () {
30355
30375
  Storage.setAuthToken(null);
30376
+ Storage.set('glitch_token_expiry', null); // Clear expiry
30377
+ Storage.eraseCookie('glitch_token_expiry');
30356
30378
  Storage.set(Session._id_key, null);
30357
30379
  Storage.set(Session._first_name_key, null);
30358
30380
  Storage.set(Session._last_name_key, null);
@@ -30361,6 +30383,7 @@ var Session = /** @class */ (function () {
30361
30383
  };
30362
30384
  Session.processAuthentication = function (data) {
30363
30385
  Storage.setAuthToken(data.token.access_token);
30386
+ Storage.setTokenExpiry(data.token.expires_in); // Save the timeout
30364
30387
  Storage.set(Session._id_key, data.id);
30365
30388
  Storage.set(Session._first_name_key, data.first_name);
30366
30389
  Storage.set(Session._last_name_key, data.last_name);