Which Layer Should Handle Token Refresh in Vue Storefront’s Offline Cache?
0 reputation · 22 Nov 2024, 08:09 UTC
0 reputation · 22 Nov 2024, 08:09 UTC
Maintain least‑privilege authentication while ensuring seamless user experience in a Vue Storefront PWA that uses offline GraphQL caching.
Vue Storefront stores query results, including authentication artifacts, in IndexedDB for offline support. When a backend session expires, the cached token becomes invalid, yet the framework does not automatically refresh it. The current design leaves token renewal responsibility ambiguous: should the service worker intercept 401 responses and refresh the token, or should the host application handle it?
Placing refresh logic in the service worker could centralize security and reduce exposed code, but may complicate cache invalidation and user flow. Delegating to the host app simplifies implementation but risks token leakage and inconsistent refresh timing.
33275 reputation · 22 Nov 2024, 10:46 UTC
Token refresh belongs in the host application and its API client, not in the Vue Storefront service worker. Treat the service worker as a transport and cache layer; let the application detect authentication failures, coordinate a single refresh call, update IndexedDB, and retry the original request.
A service worker can technically read IndexedDB and issue fetches, so centralizing refresh there is tempting. In practice, making it the auth authority creates race conditions: multiple tabs can refresh simultaneously, cached GraphQL responses can be replayed after logout, and cache invalidation becomes coupled to authentication state. The confirmed, safer pattern is to keep orchestration in the host app, where you can enforce a single-flight queue and explicitly invalidate entries.
Host-app refresh does not inherently break least privilege. The real risks are storage and scope: long-lived refresh tokens in IndexedDB, broad scopes, logging tokens, or blindly retrying non-idempotent mutations after a 401. Use short-lived access tokens, rotated or revocable refresh tokens, minimal scopes, and a single-flight refresh queue. For authenticated GraphQL, prefer network-first or bypass offline caching entirely; cache only public or normalized data offline.
Is your refresh token JS-readable in IndexedDB, or is it held in an HttpOnly cookie? If it is JS-readable, minimize its lifetime and clear it aggressively. If the backend can set HttpOnly, Secure, and SameSite cookies, the browser can attach credentials without exposing the refresh token to JavaScript; the app still coordinates refresh redirects, but the token never touches IndexedDB.
Vue Storefront’s offline and service-worker behavior differs by major version, theme, and middleware mode. Before changing auth flow, confirm whether your build uses a custom service worker, a Workbox configuration, or framework-managed caching. In a disposable environment, log only status codes—never tokens—to verify that exactly one refresh call occurs and the original request is retried once. Open two tabs during expiry to ensure the second tab waits for the first refresh. Inspect IndexedDB after logout and after refresh-token revocation to confirm artifacts are removed and not restored on next load. If using cookies, verify the Set-Cookie flags and confirm no refresh token appears in JS-accessible storage.
These recommendations reflect general PWA and Vue Storefront patterns; validate them against your specific version and backend auth contract before production rollout.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.