fix(device-auth): inline helpers into each hook handler (JSVM scope) #4

Merged
richiexec merged 1 commit from fix/device-auth-jsvm-scope into main 2026-07-19 17:48:47 +00:00
Collaborator

The deployed device-auth hooks throw at request time — confirmed from the PocketBase logs:

POST /api/device-auth/request  → ReferenceError: genCode is not defined
[cronAdd device_auth_cleanup]  → ReferenceError: nowSeconds is not defined
GET  /device?code=…            → ReferenceError: DEVICE_APPROVE_HTML is not defined

Root cause: PocketBase runs every routerAdd/cronAdd handler in its own isolated JSVM, so file-level const/function definitions aren't visible inside the callbacks. The helpers (nowSeconds, genCode, CODE_ALPHABET, the TTLs) and the DEVICE_APPROVE_HTML page were defined at file scope → every handler that referenced them ReferenceErrored. A runtime-only trap the original PR (#3) couldn't hit without a live PocketBase.

Fix: define what each handler needs inside it (local scope works). No behavior change; node --check clean on both files. info and poll already worked (they don't use the file-level helpers), which is what pinned the diagnosis.

Merge + redeploy; then POST /api/device-auth/request should return a code and /device?code=… should render the approve page. On-device TV↔phone test follows.

🤖 Generated with Claude Code

The deployed device-auth hooks throw at request time — confirmed from the PocketBase logs: ``` POST /api/device-auth/request → ReferenceError: genCode is not defined [cronAdd device_auth_cleanup] → ReferenceError: nowSeconds is not defined GET /device?code=… → ReferenceError: DEVICE_APPROVE_HTML is not defined ``` **Root cause:** PocketBase runs every `routerAdd`/`cronAdd` handler in its **own isolated JSVM**, so file-level `const`/`function` definitions aren't visible inside the callbacks. The helpers (`nowSeconds`, `genCode`, `CODE_ALPHABET`, the TTLs) and the `DEVICE_APPROVE_HTML` page were defined at file scope → every handler that referenced them `ReferenceError`ed. A runtime-only trap the original PR (#3) couldn't hit without a live PocketBase. **Fix:** define what each handler needs *inside* it (local scope works). No behavior change; `node --check` clean on both files. `info` and `poll` already worked (they don't use the file-level helpers), which is what pinned the diagnosis. Merge + redeploy; then `POST /api/device-auth/request` should return a code and `/device?code=…` should render the approve page. On-device TV↔phone test follows. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
claude added 1 commit 2026-07-19 17:48:20 +00:00
PocketBase runs every routerAdd/cronAdd handler in its OWN isolated JSVM, so the
file-level helpers/consts (nowSeconds, genCode, CODE_ALPHABET, the TTLs, and the
DEVICE_APPROVE_HTML page) were invisible inside the callbacks — every route threw
`ReferenceError: <name> is not defined` at request time (POST /request, the
cleanup cron, GET /device). A runtime-only trap that only surfaces on a live
PocketBase, which the original PR couldn't exercise.

Define what each handler needs *inside* it (local scope works). No behavior
change; syntax-checked with `node --check`.

Fixes the deploy of device-auth (amber-app #12).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
richiexec merged commit 3c8d60e614 into main 2026-07-19 17:48:47 +00:00
richiexec deleted branch fix/device-auth-jsvm-scope 2026-07-19 17:48:47 +00:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: richiexec/amber-backend#4
No description provided.