@bounded-sh/client 0.0.30 → 0.0.32

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.
@@ -4352,7 +4352,8 @@ async function loginWithRedirect(opts) {
4352
4352
  // Stash the verifier + state so the redirect_uri page can finish the exchange. sessionStorage
4353
4353
  // is per-tab and cleared on close — appropriate for a short-lived auth handshake.
4354
4354
  sessionStorage.setItem(PKCE_STORE_KEY, JSON.stringify(pkce));
4355
- window.location.href = url;
4355
+ setAuthLoading(true); // reflect "signing in…" right up to navigation; the return page's
4356
+ window.location.href = url; // completeLoginFromRedirect() keeps it true until the user resolves.
4356
4357
  }
4357
4358
  /** UPGRADE the current anonymous (guest) session by linking a SOCIAL/redirect login
4358
4359
  * (e.g. Google) — Firebase `linkWithPopup`/redirect parity. Carries the guest's signed
@@ -4429,7 +4430,8 @@ async function completeLoginFromRedirect() {
4429
4430
  }
4430
4431
  }
4431
4432
  /** Popup variant: open the hosted login in a popup; resolves with the User once the popup's
4432
- * redirect_uri page posts the code back. The redirect_uri page must call completeLoginInPopup(). */
4433
+ * redirect_uri page posts the code back. The redirect_uri page just calls the usual
4434
+ * completeLoginFromRedirect() (which auto-detects the popup) — no separate call needed. */
4433
4435
  async function loginWithPopup(opts) {
4434
4436
  if (!hasDOM())
4435
4437
  throw new Error('loginWithPopup is web-only — on React Native use loginWithRedirect()');
@@ -4444,42 +4446,57 @@ async function loginWithPopup(opts) {
4444
4446
  // Use the RESOLVED redirect_uri (buildAuthorize defaulted it to the current page on web),
4445
4447
  // not opts.redirectUri which may be undefined.
4446
4448
  const expectedOrigin = new URL(pkce.redirectUri).origin;
4447
- return await new Promise((resolve, reject) => {
4448
- const onMsg = async (ev) => {
4449
- if (ev.origin !== expectedOrigin)
4450
- return; // only trust the redirect_uri origin
4451
- const d = ev.data;
4452
- if (!d || d.type !== 'bounded-oidc-code' || typeof d.code !== 'string')
4453
- return;
4454
- window.removeEventListener('message', onMsg);
4455
- try {
4456
- const raw = sessionStorage.getItem(PKCE_STORE_KEY);
4457
- if (!raw)
4458
- throw new Error('Missing PKCE state');
4459
- const pkce = JSON.parse(raw);
4460
- if (d.state !== pkce.state)
4461
- throw new Error('OAuth state mismatch');
4462
- const user = await exchangeCode(d.code, pkce.verifier, pkce.redirectUri);
4463
- sessionStorage.removeItem(PKCE_STORE_KEY);
4449
+ setAuthLoading(true); // reflect "signing in…" while the popup is open
4450
+ try {
4451
+ return await new Promise((resolve, reject) => {
4452
+ let settled = false;
4453
+ let timer;
4454
+ const cleanup = () => { settled = true; clearInterval(timer); window.removeEventListener('message', onMsg); };
4455
+ const onMsg = async (ev) => {
4456
+ if (ev.origin !== expectedOrigin)
4457
+ return; // only trust the redirect_uri origin
4458
+ const d = ev.data;
4459
+ if (!d || d.type !== 'bounded-oidc-code' || typeof d.code !== 'string')
4460
+ return;
4461
+ cleanup();
4464
4462
  try {
4465
- popup.close();
4463
+ const raw = sessionStorage.getItem(PKCE_STORE_KEY);
4464
+ if (!raw)
4465
+ throw new Error('Missing PKCE state');
4466
+ const pkce = JSON.parse(raw);
4467
+ if (d.state !== pkce.state)
4468
+ throw new Error('OAuth state mismatch');
4469
+ const user = await exchangeCode(d.code, pkce.verifier, pkce.redirectUri);
4470
+ sessionStorage.removeItem(PKCE_STORE_KEY);
4471
+ try {
4472
+ popup.close();
4473
+ }
4474
+ catch ( /* ignore */_a) { /* ignore */ }
4475
+ resolve(user);
4466
4476
  }
4467
- catch ( /* ignore */_a) { /* ignore */ }
4468
- resolve(user);
4469
- }
4470
- catch (e) {
4471
- reject(e);
4472
- }
4473
- };
4474
- window.addEventListener('message', onMsg);
4475
- const timer = setInterval(() => {
4476
- if (popup.closed) {
4477
- clearInterval(timer);
4478
- window.removeEventListener('message', onMsg);
4479
- reject(new Error('Login window closed'));
4480
- }
4481
- }, 500);
4482
- });
4477
+ catch (e) {
4478
+ reject(e);
4479
+ }
4480
+ };
4481
+ window.addEventListener('message', onMsg);
4482
+ // The popup posts its code and then closes ITSELF, so a naive "popup.closed -> reject"
4483
+ // races the just-posted message and would report a spurious "Login window closed" on a
4484
+ // SUCCESSFUL login. When we see it closed, wait a beat for that pending message to settle
4485
+ // before declaring the window closed.
4486
+ timer = setInterval(() => {
4487
+ if (popup.closed) {
4488
+ clearInterval(timer);
4489
+ setTimeout(() => { if (!settled) {
4490
+ cleanup();
4491
+ reject(new Error('Login window closed'));
4492
+ } }, 500);
4493
+ }
4494
+ }, 500);
4495
+ });
4496
+ }
4497
+ finally {
4498
+ setAuthLoading(false);
4499
+ }
4483
4500
  }
4484
4501
  /** Call this on the redirect_uri page when login was opened via loginWithPopup(): posts the
4485
4502
  * code back to the opener and closes. (For the redirect variant use completeLoginFromRedirect.) */
@@ -4771,7 +4788,13 @@ async function signInAnonymously() {
4771
4788
  coreConfig.authMethod = 'guest';
4772
4789
  setAuthProviderInstance(provider);
4773
4790
  setStoredAuthMethod('guest');
4774
- return provider.login();
4791
+ setAuthLoading(true); // reflect "signing in…" while the guest keypair session is minted
4792
+ try {
4793
+ return await provider.login();
4794
+ }
4795
+ finally {
4796
+ setAuthLoading(false);
4797
+ }
4775
4798
  }
4776
4799
  /**
4777
4800
  * Log in with Privy as a co-equal route — selectable at runtime alongside