@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.
@@ -4372,7 +4372,8 @@ async function loginWithRedirect(opts) {
4372
4372
  // Stash the verifier + state so the redirect_uri page can finish the exchange. sessionStorage
4373
4373
  // is per-tab and cleared on close — appropriate for a short-lived auth handshake.
4374
4374
  sessionStorage.setItem(PKCE_STORE_KEY, JSON.stringify(pkce));
4375
- window.location.href = url;
4375
+ setAuthLoading(true); // reflect "signing in…" right up to navigation; the return page's
4376
+ window.location.href = url; // completeLoginFromRedirect() keeps it true until the user resolves.
4376
4377
  }
4377
4378
  /** UPGRADE the current anonymous (guest) session by linking a SOCIAL/redirect login
4378
4379
  * (e.g. Google) — Firebase `linkWithPopup`/redirect parity. Carries the guest's signed
@@ -4449,7 +4450,8 @@ async function completeLoginFromRedirect() {
4449
4450
  }
4450
4451
  }
4451
4452
  /** Popup variant: open the hosted login in a popup; resolves with the User once the popup's
4452
- * redirect_uri page posts the code back. The redirect_uri page must call completeLoginInPopup(). */
4453
+ * redirect_uri page posts the code back. The redirect_uri page just calls the usual
4454
+ * completeLoginFromRedirect() (which auto-detects the popup) — no separate call needed. */
4453
4455
  async function loginWithPopup(opts) {
4454
4456
  if (!hasDOM())
4455
4457
  throw new Error('loginWithPopup is web-only — on React Native use loginWithRedirect()');
@@ -4464,42 +4466,57 @@ async function loginWithPopup(opts) {
4464
4466
  // Use the RESOLVED redirect_uri (buildAuthorize defaulted it to the current page on web),
4465
4467
  // not opts.redirectUri which may be undefined.
4466
4468
  const expectedOrigin = new URL(pkce.redirectUri).origin;
4467
- return await new Promise((resolve, reject) => {
4468
- const onMsg = async (ev) => {
4469
- if (ev.origin !== expectedOrigin)
4470
- return; // only trust the redirect_uri origin
4471
- const d = ev.data;
4472
- if (!d || d.type !== 'bounded-oidc-code' || typeof d.code !== 'string')
4473
- return;
4474
- window.removeEventListener('message', onMsg);
4475
- try {
4476
- const raw = sessionStorage.getItem(PKCE_STORE_KEY);
4477
- if (!raw)
4478
- throw new Error('Missing PKCE state');
4479
- const pkce = JSON.parse(raw);
4480
- if (d.state !== pkce.state)
4481
- throw new Error('OAuth state mismatch');
4482
- const user = await exchangeCode(d.code, pkce.verifier, pkce.redirectUri);
4483
- sessionStorage.removeItem(PKCE_STORE_KEY);
4469
+ setAuthLoading(true); // reflect "signing in…" while the popup is open
4470
+ try {
4471
+ return await new Promise((resolve, reject) => {
4472
+ let settled = false;
4473
+ let timer;
4474
+ const cleanup = () => { settled = true; clearInterval(timer); window.removeEventListener('message', onMsg); };
4475
+ const onMsg = async (ev) => {
4476
+ if (ev.origin !== expectedOrigin)
4477
+ return; // only trust the redirect_uri origin
4478
+ const d = ev.data;
4479
+ if (!d || d.type !== 'bounded-oidc-code' || typeof d.code !== 'string')
4480
+ return;
4481
+ cleanup();
4484
4482
  try {
4485
- popup.close();
4483
+ const raw = sessionStorage.getItem(PKCE_STORE_KEY);
4484
+ if (!raw)
4485
+ throw new Error('Missing PKCE state');
4486
+ const pkce = JSON.parse(raw);
4487
+ if (d.state !== pkce.state)
4488
+ throw new Error('OAuth state mismatch');
4489
+ const user = await exchangeCode(d.code, pkce.verifier, pkce.redirectUri);
4490
+ sessionStorage.removeItem(PKCE_STORE_KEY);
4491
+ try {
4492
+ popup.close();
4493
+ }
4494
+ catch ( /* ignore */_a) { /* ignore */ }
4495
+ resolve(user);
4486
4496
  }
4487
- catch ( /* ignore */_a) { /* ignore */ }
4488
- resolve(user);
4489
- }
4490
- catch (e) {
4491
- reject(e);
4492
- }
4493
- };
4494
- window.addEventListener('message', onMsg);
4495
- const timer = setInterval(() => {
4496
- if (popup.closed) {
4497
- clearInterval(timer);
4498
- window.removeEventListener('message', onMsg);
4499
- reject(new Error('Login window closed'));
4500
- }
4501
- }, 500);
4502
- });
4497
+ catch (e) {
4498
+ reject(e);
4499
+ }
4500
+ };
4501
+ window.addEventListener('message', onMsg);
4502
+ // The popup posts its code and then closes ITSELF, so a naive "popup.closed -> reject"
4503
+ // races the just-posted message and would report a spurious "Login window closed" on a
4504
+ // SUCCESSFUL login. When we see it closed, wait a beat for that pending message to settle
4505
+ // before declaring the window closed.
4506
+ timer = setInterval(() => {
4507
+ if (popup.closed) {
4508
+ clearInterval(timer);
4509
+ setTimeout(() => { if (!settled) {
4510
+ cleanup();
4511
+ reject(new Error('Login window closed'));
4512
+ } }, 500);
4513
+ }
4514
+ }, 500);
4515
+ });
4516
+ }
4517
+ finally {
4518
+ setAuthLoading(false);
4519
+ }
4503
4520
  }
4504
4521
  /** Call this on the redirect_uri page when login was opened via loginWithPopup(): posts the
4505
4522
  * code back to the opener and closes. (For the redirect variant use completeLoginFromRedirect.) */
@@ -4791,7 +4808,13 @@ async function signInAnonymously() {
4791
4808
  coreConfig.authMethod = 'guest';
4792
4809
  setAuthProviderInstance(provider);
4793
4810
  setStoredAuthMethod('guest');
4794
- return provider.login();
4811
+ setAuthLoading(true); // reflect "signing in…" while the guest keypair session is minted
4812
+ try {
4813
+ return await provider.login();
4814
+ }
4815
+ finally {
4816
+ setAuthLoading(false);
4817
+ }
4795
4818
  }
4796
4819
  /**
4797
4820
  * Log in with Privy as a co-equal route — selectable at runtime alongside