From 8809375ad62a3cefcf6623a00cddaec4472ca188 Mon Sep 17 00:00:00 2001 From: Vitor Date: Fri, 24 Jul 2026 18:37:54 -0300 Subject: [PATCH] fix(storefront): Refresh passport token before loading checkout app when near expiry MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When a recurring customer opens checkout with a passport token expiring within 2 minutes, the storefront-app would load with the stale token, causing fetchCustomer to 401 and leaving the user stuck on the "Complete seu cadastro" screen instead of the login or checkout step. Now checks synchronously if the stored token needs refresh before loading app.js. Only users in the ~2-minute expiry window experience a brief extra delay (Firebase authStateReady + /_api/passport/token). All other users — new, anonymous, or with a fresh token — load immediately with no change in behavior. Co-Authored-By: Claude Sonnet 4.6 --- packages/storefront/src/lib/scripts/vbeta-app.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/packages/storefront/src/lib/scripts/vbeta-app.ts b/packages/storefront/src/lib/scripts/vbeta-app.ts index 1e4c38857..aec84fc43 100644 --- a/packages/storefront/src/lib/scripts/vbeta-app.ts +++ b/packages/storefront/src/lib/scripts/vbeta-app.ts @@ -22,6 +22,7 @@ import { customer, customerName, logout, + authenticate, initializeFirebaseAuth, isAuthReady, } from '@@sf/state/customer-session'; @@ -304,6 +305,9 @@ if (!import.meta.env.SSR) { resolve(getAuth()); }); }); + const storedTokenExpiresAt = session.auth ? new Date(session.auth.expires).getTime() : 0; + const storedTokenNeedsRefresh = storedTokenExpiresAt > 0 + && storedTokenExpiresAt - Date.now() < 2 * 60 * 1000; if (window.location.hash.includes('account')) { initializingAuth .then(async (firebaseAuth) => { @@ -320,6 +324,16 @@ if (!import.meta.env.SSR) { console.error(err); loadAppScript(); }); + } else if (storedTokenNeedsRefresh) { + initializingAuth + .then(async (firebaseAuth) => { + await firebaseAuth.authStateReady(); + if (isAuthenticated.value) { + await authenticate().catch(() => {}); + } + loadAppScript(); + }) + .catch(() => loadAppScript()); } else { loadAppScript(); }