Repository navigation
feat: greet the logged-in staff member by name on the home page - #14
Conversation
GET /api/example now returns `user` beside `shop`. The name comes from an online-token exchange keyed on the session token's `sub`, run once per staff member per shop; only the id and full name are cached in KV (30-day TTL). The online token and email are never stored. A failed lookup returns `user: null` so the page still loads.
There was a problem hiding this comment.
devkind-code-review: devkindhq/shopify-on-cloudflare #14
Mode: comment-only · Head fe3bc3d5 · Checks (run at head in clean checkout): tsc --noEmit ✓ · lint:ci ✓ · vitest run 25/25 ✓ (7 files)
Solid PR. The design choices are all right: online-token exchange runs once per user per shop and only the name is cached (no email, no token persisted), user: null degradation never fails the shop payload, and the tests cover the cache hit, the no-token dev fallback, empty-name combinations, and the throw-before-cache path. KV key namespacing (staff:{shop}:{sub}) is documented as distinct from KVSessionStorage ids.
One finding, medium:
🟡 currentUser trusts payload.dest uncritically — no cross-check against the authenticated shop
src/lib/currentUser.ts:53 — decodeSessionToken verifies the JWT signature and expiry, so sub is trustworthy, but dest is then used directly as both the token-exchange shop argument and the KV cache key. Two consequences:
- A legit multi-domain scenario breaks: if the app is served on a custom primary domain,
destcan beadmin.shop.comwhile the app's hostName/shop records usemyshopify.com— the token exchange then fails for a valid token. The base repo has nocustom-shop-domainshandling anywhere, so this is latent, not a regression — but note thatrequireShop(the same request's gate) andcurrentUsercan disagree about which shop domain this request belongs to. - On uninstalled-and-reinstalled shops the cache key stays stable while the user id can be reused for a different person — 30-day TTL mitigates, just worth knowing.
Cheapest hardening: pass the shop domain through shopify.utils.sanitizeShop(payload.dest) and bail to null on mismatch with the shop the route already authenticated, rather than deriving a second independent shop identity from the header token. Not a merge blocker for a template example.
Posted by the scheduled overnight audit (Slack unavailable in cron — proceeding per standing audit policy).
| const shopify = createShopify(env); | ||
| const payload = await shopify.session.decodeSessionToken(token); | ||
| if (!payload.sub) return null; | ||
| const shopDomain = new URL(payload.dest).hostname; |
There was a problem hiding this comment.
🟡 payload.dest is used directly as the token-exchange shop and the KV key. Consider shopify.utils.sanitizeShop(...) + a cross-check against the shop requireShop authenticated on this request, so one request can't carry two disagreeing shop identities (also future-proofs custom primary domains where dest ≠ myshopify domain).
|
post-merge verify-fixes audit (Bucket A — bot review, human-approved, 68a4b1e): verdict on the single 🟡 finding — ❌ SHIPPED UNFIXED, by documented decision (latent, not a regression):
Suggested follow-up (small): |
What
The home page now greets whoever has the app open: "Welcome, {first name} {last name}" (falls back to plain "Welcome" if Shopify has no name).
How
src/lib/currentUser.ts(new): reads the staff user ID from the verified session token'ssubclaim, then does a one-off online-token exchange to getassociated_user.first_name/last_name.{ id, name }is cached inSESSION_KVunderstaff:{shop}:{userId}, with a 30-day TTL, so the exchange runs about once a month per person.nullwhen there's no session token (the local-devx-shop-domainfallback).GET /api/examplenow returns{ shop, user }.usersits next toshoprather than inside it because the shop is the same for every staff member and the user isn't. A failed lookup is logged and givesuser: null, so the shop card still loads.web/Pages/Home.tsx: a PolarisTextheading at the top of the shop card.No new bindings, secrets, scopes, or migrations.
Privacy note for template users
Staff names are personal data. They expire on their own after 30 days. If you add a GDPR
shop/redacthandler, delete every key understaff:{shop}:in it (there's a comment at the key definition).Tests
src/lib/currentUser.test.ts: no-token path, exchange + cache (and that the email isn't cached), cache hit skips Shopify, partial and empty names, and a missing associated user throws without caching.src/api.integration.test.ts: the dev header fallback returnsuser: null.vitest25/25 pass.tsc --noEmit,eslintandvite buildare clean.