eocene-auth-sdk 0.5.1 → 0.5.3

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.ts CHANGED
@@ -455,8 +455,10 @@ declare class AuthClient {
455
455
  markActive(on: boolean): void;
456
456
  private restoreLocalSession;
457
457
  private localSessionNeedsRefresh;
458
+ hasPersistedSession(): boolean;
458
459
  restoreIfNeeded(): boolean;
459
460
  restoreSession(): Promise<boolean>;
461
+ private bootstrapRefreshToken;
460
462
  private performRestoreSession;
461
463
  logout(): void;
462
464
  setPassword(password: string, currentPassword?: string, recoveryBind?: {
package/dist/index.js CHANGED
@@ -3078,24 +3078,67 @@ var WaasClient = class {
3078
3078
  };
3079
3079
 
3080
3080
  // src/client/storage.ts
3081
+ function safeGet(storage, key) {
3082
+ if (!storage) return null;
3083
+ try {
3084
+ return storage.getItem(key);
3085
+ } catch {
3086
+ return null;
3087
+ }
3088
+ }
3089
+ function safeSet(storage, key, value) {
3090
+ if (!storage) return false;
3091
+ try {
3092
+ storage.setItem(key, value);
3093
+ return true;
3094
+ } catch {
3095
+ return false;
3096
+ }
3097
+ }
3098
+ function safeRemove(storage, key) {
3099
+ if (!storage) return;
3100
+ try {
3101
+ storage.removeItem(key);
3102
+ } catch {
3103
+ }
3104
+ }
3105
+ function localOrNull() {
3106
+ return typeof localStorage !== "undefined" ? localStorage : void 0;
3107
+ }
3108
+ function sessionOrNull() {
3109
+ return typeof sessionStorage !== "undefined" ? sessionStorage : void 0;
3110
+ }
3081
3111
  function loadPersistedState(storageKey = DEFAULT_STORAGE_KEY) {
3082
- if (typeof sessionStorage === "undefined") return null;
3112
+ const localRaw = safeGet(localOrNull(), storageKey);
3113
+ if (localRaw) {
3114
+ try {
3115
+ return JSON.parse(localRaw);
3116
+ } catch {
3117
+ safeRemove(localOrNull(), storageKey);
3118
+ }
3119
+ }
3120
+ const sessionRaw = safeGet(sessionOrNull(), storageKey);
3121
+ if (!sessionRaw) return null;
3083
3122
  try {
3084
- const raw = sessionStorage.getItem(storageKey);
3085
- if (!raw) return null;
3086
- return JSON.parse(raw);
3123
+ const parsed = JSON.parse(sessionRaw);
3124
+ if (safeSet(localOrNull(), storageKey, sessionRaw)) {
3125
+ safeRemove(sessionOrNull(), storageKey);
3126
+ }
3127
+ return parsed;
3087
3128
  } catch {
3129
+ safeRemove(sessionOrNull(), storageKey);
3088
3130
  return null;
3089
3131
  }
3090
3132
  }
3091
- function persistDeviceState(state, storageKey = DEFAULT_STORAGE_KEY) {
3092
- if (typeof sessionStorage === "undefined") return;
3133
+ function persistDeviceState(state, storageKey = DEFAULT_STORAGE_KEY, opts) {
3093
3134
  if (!state?.sessionToken) {
3094
- sessionStorage.removeItem(storageKey);
3135
+ clearPersistedState(storageKey);
3095
3136
  return;
3096
3137
  }
3138
+ const existingRefresh = opts?.refreshToken !== void 0 ? opts.refreshToken : loadPersistedState(storageKey)?.refreshToken;
3097
3139
  const payload = {
3098
3140
  sessionToken: state.sessionToken,
3141
+ ...existingRefresh ? { refreshToken: existingRefresh } : {},
3099
3142
  principalId: state.principalId,
3100
3143
  email: state.email,
3101
3144
  walletId: state.walletId,
@@ -3103,11 +3146,16 @@ function persistDeviceState(state, storageKey = DEFAULT_STORAGE_KEY) {
3103
3146
  epoch: state.epoch,
3104
3147
  passwordEnabled: state.passwordEnabled
3105
3148
  };
3106
- sessionStorage.setItem(storageKey, JSON.stringify(payload));
3149
+ const serialized = JSON.stringify(payload);
3150
+ if (safeSet(localOrNull(), storageKey, serialized)) {
3151
+ safeRemove(sessionOrNull(), storageKey);
3152
+ } else {
3153
+ safeSet(sessionOrNull(), storageKey, serialized);
3154
+ }
3107
3155
  }
3108
3156
  function clearPersistedState(storageKey = DEFAULT_STORAGE_KEY) {
3109
- if (typeof sessionStorage === "undefined") return;
3110
- sessionStorage.removeItem(storageKey);
3157
+ safeRemove(localOrNull(), storageKey);
3158
+ safeRemove(sessionOrNull(), storageKey);
3111
3159
  }
3112
3160
 
3113
3161
  // src/client/telegramMiniApp.ts
@@ -3227,8 +3275,12 @@ var AuthClient = class {
3227
3275
  getProvider: async () => this.getProvider()
3228
3276
  };
3229
3277
  }
3230
- persist() {
3231
- persistDeviceState(this.client.getState(), this.storageKey);
3278
+ persist(refreshToken) {
3279
+ persistDeviceState(
3280
+ this.client.getState(),
3281
+ this.storageKey,
3282
+ refreshToken !== void 0 ? { refreshToken } : void 0
3283
+ );
3232
3284
  }
3233
3285
  async fetchOAuthProviders() {
3234
3286
  const base = this.config.controlBaseUrl.replace(/\/$/, "");
@@ -3364,6 +3416,7 @@ var AuthClient = class {
3364
3416
  this.persist();
3365
3417
  this.active = true;
3366
3418
  this.provider = null;
3419
+ await this.bootstrapRefreshToken(decodeURIComponent(sessionToken));
3367
3420
  if (typeof sessionStorage !== "undefined") {
3368
3421
  sessionStorage.removeItem(this.oauthPendingKey);
3369
3422
  }
@@ -3406,6 +3459,10 @@ var AuthClient = class {
3406
3459
  const expMs = token ? sessionTokenExpMs(token) : null;
3407
3460
  return expMs == null || expMs <= Date.now() + RESTORE_REFRESH_WITHIN_MS;
3408
3461
  }
3462
+ hasPersistedSession() {
3463
+ const saved = loadPersistedState(this.storageKey);
3464
+ return Boolean(saved?.sessionToken || saved?.refreshToken);
3465
+ }
3409
3466
  restoreIfNeeded() {
3410
3467
  return this.restoreLocalSession();
3411
3468
  }
@@ -3416,6 +3473,32 @@ var AuthClient = class {
3416
3473
  });
3417
3474
  return this.restorePromise;
3418
3475
  }
3476
+ async bootstrapRefreshToken(sessionToken) {
3477
+ if (!this.config.projectId || !sessionToken) return;
3478
+ const base = resolveControlAbsoluteUrl(this.config.controlBaseUrl);
3479
+ const controller = typeof AbortController !== "undefined" ? new AbortController() : null;
3480
+ const timer = controller ? setTimeout(() => controller.abort(), 3e3) : null;
3481
+ try {
3482
+ const res = await fetch(`${base}/auth/session/refresh`, {
3483
+ method: "POST",
3484
+ credentials: "include",
3485
+ headers: {
3486
+ "content-type": "application/json",
3487
+ authorization: `Bearer ${sessionToken}`
3488
+ },
3489
+ body: JSON.stringify({ projectId: this.config.projectId }),
3490
+ ...controller ? { signal: controller.signal } : {}
3491
+ });
3492
+ if (!res.ok) return;
3493
+ const result = await res.json().catch(() => null);
3494
+ if (result?.refreshToken && this.active) {
3495
+ this.persist(result.refreshToken);
3496
+ }
3497
+ } catch {
3498
+ } finally {
3499
+ if (timer != null) clearTimeout(timer);
3500
+ }
3501
+ }
3419
3502
  async performRestoreSession() {
3420
3503
  const restoreLocalIfCurrent = () => this.restoreLocalSession() && !this.localSessionNeedsRefresh();
3421
3504
  if (restoreLocalIfCurrent()) return true;
@@ -3424,21 +3507,26 @@ var AuthClient = class {
3424
3507
  try {
3425
3508
  const base = resolveControlAbsoluteUrl(this.config.controlBaseUrl);
3426
3509
  const url = `${base}/auth/session/refresh`;
3427
- const body = JSON.stringify({ projectId: this.config.projectId });
3510
+ const saved = loadPersistedState(this.storageKey);
3511
+ const storedRefresh = saved?.refreshToken;
3512
+ const body = JSON.stringify({
3513
+ projectId: this.config.projectId,
3514
+ ...storedRefresh ? { refreshToken: storedRefresh } : {}
3515
+ });
3428
3516
  let res = await fetch(url, {
3429
3517
  method: "POST",
3430
3518
  credentials: "include",
3431
3519
  headers: { "content-type": "application/json" },
3432
3520
  body
3433
3521
  });
3434
- const legacyToken = loadPersistedState(this.storageKey)?.sessionToken;
3522
+ const legacyToken = saved?.sessionToken;
3435
3523
  const legacyExpMs = legacyToken ? sessionTokenExpMs(legacyToken) : null;
3436
3524
  if (res.status === 401 && legacyToken && legacyExpMs != null && legacyExpMs > Date.now()) {
3437
3525
  res = await fetch(url, {
3438
3526
  method: "POST",
3439
3527
  credentials: "include",
3440
3528
  headers: { "content-type": "application/json", authorization: `Bearer ${legacyToken}` },
3441
- body
3529
+ body: JSON.stringify({ projectId: this.config.projectId })
3442
3530
  });
3443
3531
  }
3444
3532
  const result = await res.json().catch(() => ({}));
@@ -3447,7 +3535,7 @@ var AuthClient = class {
3447
3535
  await this.client.ensureWallet({
3448
3536
  telegramInitData: getTelegramInitData() ?? void 0
3449
3537
  });
3450
- this.persist();
3538
+ this.persist(result.refreshToken);
3451
3539
  this.active = true;
3452
3540
  this.provider = null;
3453
3541
  return true;
@@ -3463,13 +3551,17 @@ var AuthClient = class {
3463
3551
  }
3464
3552
  logout() {
3465
3553
  const base = resolveControlAbsoluteUrl(this.config.controlBaseUrl);
3554
+ const storedRefresh = loadPersistedState(this.storageKey)?.refreshToken;
3466
3555
  try {
3467
3556
  void fetch(`${base}/auth/session/logout`, {
3468
3557
  method: "POST",
3469
3558
  credentials: "include",
3470
3559
  keepalive: true,
3471
3560
  headers: { "content-type": "application/json" },
3472
- body: JSON.stringify({ projectId: this.config.projectId })
3561
+ body: JSON.stringify({
3562
+ projectId: this.config.projectId,
3563
+ ...storedRefresh ? { refreshToken: storedRefresh } : {}
3564
+ })
3473
3565
  }).catch(() => {
3474
3566
  });
3475
3567
  } finally {
@@ -4604,6 +4696,12 @@ function AuthProvider({
4604
4696
  let alive = true;
4605
4697
  const c = clientRef.current;
4606
4698
  if (!c) return;
4699
+ if (!config.asDeveloper && !c.hasPersistedSession()) {
4700
+ c.restoreIfNeeded();
4701
+ setTick((n) => n + 1);
4702
+ setReady(true);
4703
+ return;
4704
+ }
4607
4705
  setReady(false);
4608
4706
  void c.restoreSession().then(() => {
4609
4707
  if (!alive || clientRef.current !== c) return;