Skip to content

fix(#2861): prize-barcode 401 — resolve identity from cds.context.user - #2867

Merged
jung-thomas merged 1 commit into
DEVfrom
worktree-fix-2861-prize-barcode
Oct 20, 2026
Merged

jung-thomas merged 1 commit into
DEVfrom
worktree-fix-2861-prize-barcode

Conversation

@jung-thomas

Copy link
Copy Markdown
Contributor

Problem

The App Space checkpoint "Claim your prize" barcode (GET /api/my-prize-barcode) still returned 401 for every logged-in visitor after the previous #2861 fix — the QR rendered as a broken image.

Root cause

On a raw Express bootstrap route, CAP's auth() middleware populates the authenticated identity on cds.context.user (the AsyncLocalStorage continuation), not on req.user. req.user is only set in CAP service handlers and in the mocked-basic-auth test harness.

The handler read bare req.user → every real XSUAA/IAS login resolved to anonymous → provision returned null → hard 401. The integration test stayed green because that harness does set req.user, hiding the production-only defect.

Fix

Uses the existing codebase idiom (the working /api/alerts/me, /auth/user, and devtoberfest Express routes) — no reinvention:

  • srv/lib/prize-barcode-handler.js — resolve req.user || cds.context.user, guard the anonymous sentinel, provision via provisionDbUser(user, ['ID']) (canonical get-or-create for raw Express routes; tiered iss/sub resolver, mints on first visit; takes the user object directly — no CAP-request .tx coupling, unlike getOrProvisionDbUser).
  • test/unit/prize-barcode-handler.test.js — rewritten to the new contract; adds the operative regression guard: identity present on cds.context.user, absent on req.user, must 200 not 401.
  • Corrected misleading "bootstrap bypasses the middleware chain" comments in srv/server.js and the integration test. The eager-bootstrap middleware mount is actually correct (cds.middlewares is a lazy getter, populated on first access — verified against @sap/cds 10.1.0 source).

Verification

  • Unit: 8/8 pass (incl. new cds.context.user-only regression guard)
  • Integration (real-server boot, in-memory SQLite): 2/2 pass
  • lint:package-boundaries: OK

Not yet verified against a live authenticated request on DEV (no tech-user creds locally); the DEV deploy of this PR plus a real App Space click is the final confirmation.

…l 401 fix)

The App Space checkpoint barcode still 401'd for logged-in visitors after the
previous #2861 fix. Root cause: on a raw Express bootstrap route CAP's auth()
middleware populates the authenticated identity on cds.context.user (the
AsyncLocalStorage continuation), NOT on req.user — req.user is only set in CAP
service handlers and in the mocked-basic-auth test harness. The handler read
bare req.user, so every real XSUAA/IAS login resolved to anonymous → provision
returned null → a hard 401. The integration test stayed green because that
harness DOES set req.user, hiding the production-only defect.

Fix (matches the existing idiom used by the working /api/alerts/me and
/auth/user routes, and the devtoberfest Express routes):
- prize-barcode-handler.js: resolve `req.user || cds.context.user`, guard the
  anonymous sentinel, and provision via `provisionDbUser(user, ['ID'])` — the
  canonical get-or-create for raw Express routes (tiered iss/sub resolver, mints
  on first visit; takes the user object directly, no CAP-request .tx coupling,
  unlike getOrProvisionDbUser).
- test/unit/prize-barcode-handler.test.js: rewritten to the new contract and
  adds the operative regression guard — identity present on cds.context.user,
  absent on req.user, must 200 (not 401).
- Corrected the misleading "bootstrap bypasses the middleware chain" comments in
  srv/server.js and the integration test; the eager-bootstrap middleware mount
  is correct (cds.middlewares is a lazy getter, populated on first access).

Verified: unit 8/8, integration (real-server boot, in-memory sqlite) 2/2,
package-boundaries OK.
@jung-thomas
jung-thomas merged commit adb80ce into DEV Oct 20, 2026
5 checks passed
@jung-thomas
jung-thomas deleted the worktree-fix-2861-prize-barcode branch October 20, 2026 23:49
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.

1 participant