Skip to content

feat: greet the logged-in staff member by name on the home page - #14

Merged
rome2o merged 1 commit into
mainfrom
feat/welcome-current-user
Oct 27, 2026
Merged

rome2o merged 1 commit into
mainfrom
feat/welcome-current-user

Conversation

@hassan-qureshi1

@hassan-qureshi1 hassan-qureshi1 commented Oct 21, 2026 •

Copy link
Copy Markdown
Collaborator

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's sub claim, then does a one-off online-token exchange to get associated_user.first_name / last_name.
    • Only { id, name } is cached in SESSION_KV under staff:{shop}:{userId}, with a 30-day TTL, so the exchange runs about once a month per person.
    • The online token and the staff email are never stored. The app keeps using its offline token for every Admin call.
    • Returns null when there's no session token (the local-dev x-shop-domain fallback).
  • GET /api/example now returns { shop, user }. user sits next to shop rather than inside it because the shop is the same for every staff member and the user isn't. A failed lookup is logged and gives user: null, so the shop card still loads.
  • web/Pages/Home.tsx: a Polaris Text heading 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/redact handler, delete every key under staff:{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 returns user: null.
  • vitest 25/25 pass. tsc --noEmit, eslint and vite build are clean.

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.

@superkindhq superkindhq Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  1. A legit multi-domain scenario breaks: if the app is served on a custom primary domain, dest can be admin.shop.com while the app's hostName/shop records use myshopify.com — the token exchange then fails for a valid token. The base repo has no custom-shop-domains handling anywhere, so this is latent, not a regression — but note that requireShop (the same request's gate) and currentUser can disagree about which shop domain this request belongs to.
  2. 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).

Comment thread src/lib/currentUser.ts
const shopify = createShopify(env);
const payload = await shopify.session.decodeSessionToken(token);
if (!payload.sub) return null;
const shopDomain = new URL(payload.dest).hostname;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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).

@rome2o
rome2o merged commit 68a4b1e into main Oct 27, 2026
4 checks passed
@superkindhq

superkindhq Bot commented Oct 27, 2026

Copy link
Copy Markdown

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):

  • currentUser still trusts payload.dest uncritically (src/lib/currentUser.ts:41 at merge tip): shopDomain = new URL(payload.dest).hostname is still used as both the token-exchange shop argument and the KV cache key, with no sanitizeShop() pass and no cross-check against the shop the route already authenticated. The original review flagged this as not a merge blocker for a template example and rome2o approved on that basis, so this is a conscious accept, not an oversight.
  • Risk stands as written: on a custom primary domain, dest can be admin.shop.com while app records use myshopify.com — the token exchange fails for a valid token; and the 30-day KV cache key is stable across uninstall/reinstall where sub reuse could serve a stale name.

Suggested follow-up (small): shopify.utils.sanitizeShop(payload.dest) + bail-to-null on mismatch with the route's authenticated shop. Filing a note here rather than a ticket since this repo is the base template — forks inherit the gap, so a fix here pays out multiply.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants