@ragable/sdk 0.6.19 → 0.6.20

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.mjs CHANGED
@@ -1746,6 +1746,8 @@ var RagableAuth = class {
1746
1746
  const refreshed = await this._doRefresh(session.refresh_token);
1747
1747
  if (refreshed) {
1748
1748
  this.currentSession = refreshed;
1749
+ } else {
1750
+ await this.storage.removeItem(this.storageKey);
1749
1751
  }
1750
1752
  }
1751
1753
  }
@@ -1763,7 +1765,8 @@ var RagableAuth = class {
1763
1765
  const raw = await this.fetchAuth("/register", "POST", {
1764
1766
  email: credentials.email,
1765
1767
  password: credentials.password,
1766
- ...name !== void 0 ? { name } : {}
1768
+ ...name !== void 0 ? { name } : {},
1769
+ ...credentials.options?.data !== void 0 ? { data: credentials.options.data } : {}
1767
1770
  });
1768
1771
  const session = this.rawToSession(raw);
1769
1772
  await this.setSessionInternal(session, "SIGNED_IN");
@@ -1833,7 +1836,9 @@ var RagableAuth = class {
1833
1836
  const token = this.currentSession?.access_token;
1834
1837
  if (!token) throw new RagableError("Not authenticated", 401, null);
1835
1838
  const result = await this.fetchAuthWithBearer("/me", "PATCH", token, {
1839
+ ...attributes.email !== void 0 ? { email: attributes.email } : {},
1836
1840
  ...attributes.password !== void 0 ? { password: attributes.password } : {},
1841
+ ...attributes.data !== void 0 ? { data: attributes.data } : {},
1837
1842
  ...attributes.data?.name !== void 0 ? { name: attributes.data.name } : {}
1838
1843
  });
1839
1844
  if (this.currentSession) {
@@ -1858,6 +1863,17 @@ var RagableAuth = class {
1858
1863
  getAccessToken() {
1859
1864
  return this.currentSession?.access_token ?? null;
1860
1865
  }
1866
+ async getValidAccessToken() {
1867
+ if (!this.initialized) await this.initialize();
1868
+ const session = this.currentSession;
1869
+ if (!session) return null;
1870
+ const secondsUntilExpiry = session.expires_at - nowSeconds();
1871
+ if (secondsUntilExpiry <= this.refreshSkewSeconds) {
1872
+ const refreshed = await this.singleFlightRefresh(session.refresh_token);
1873
+ return refreshed?.access_token ?? null;
1874
+ }
1875
+ return session.access_token;
1876
+ }
1861
1877
  getCurrentSession() {
1862
1878
  return this.currentSession;
1863
1879
  }
@@ -2071,7 +2087,7 @@ function requireAuthGroupId(options) {
2071
2087
  }
2072
2088
  async function requireAccessToken(options, ragableAuth) {
2073
2089
  if (ragableAuth) {
2074
- const token = ragableAuth.getAccessToken();
2090
+ const token = await ragableAuth.getValidAccessToken();
2075
2091
  if (token) return token;
2076
2092
  }
2077
2093
  const getter = options.getAccessToken;
@@ -2690,10 +2706,7 @@ var RagableBrowser = class {
2690
2706
  });
2691
2707
  this.transport.setRefreshHandler(async () => {
2692
2708
  if (effectiveDataAuth(options) !== "user") return null;
2693
- const session = await this._ragableAuth.singleFlightRefresh(
2694
- this._ragableAuth.getCurrentSession()?.refresh_token ?? ""
2695
- );
2696
- return session?.access_token ?? null;
2709
+ return this._ragableAuth.getValidAccessToken();
2697
2710
  });
2698
2711
  if (!options.getAccessToken && effectiveDataAuth(options) === "user") {
2699
2712
  this._ragableAuth.initialize().catch(() => {