Skip to content

fix(auth): use live DB role on token refresh - #1114

Merged
khaira777 merged 3 commits into
mainfrom
fm/flocafe-issue-1077-r1
Oct 13, 2026
Merged

khaira777 merged 3 commits into
mainfrom
fm/flocafe-issue-1077-r1

Conversation

@khaira777

@khaira777 khaira777 commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Intent

Verify and implement the surgical fix for FreeOpenSourcePOS/FloCafe issue #1077 ("re-read user role from database during JWT refresh"): in main/routes/auth.ts, POST /api/auth/refresh currently copies role: decoded.role from the old JWT instead of reading the user's current role from the users table query already executed during refresh. Read role from the database lookup so refreshed tokens carry the user's current role while preserving existing is_active and tokens_valid_after checks. Deliver via the no-mistakes pipeline as its own PR with Fixes #1077 in the PR summary so the issue and PR are linked.

What Changed

  • Select role alongside is_active and tokens_valid_after from the users table during POST /api/auth/refresh in main/routes/auth.ts, and sign the refreshed JWT with user.role instead of copying decoded.role from the incoming token.
  • Extend tests/jwt-logout-lifecycle.test.ts to verify that refreshed tokens carry the updated database role while still rejecting stale (tokens_valid_after) and inactive (is_active = 0) users.

Fixes #1077

Risk Assessment

✅ Low: The change is a minimal, surgical 2-line update in POST /api/auth/refresh to select and sign the user's current database role instead of copying decoded.role, accompanied by an end-to-end behavioral test covering role refresh, tokens_valid_after staleness, and inactive user rejection.

Testing

Built the backend, executed the focused JWT lifecycle, phase-2 auth, staff authorization, and script-coverage suites, and stood up a live dev-server.js instance against an isolated SQLite directory to drive POST /api/auth/refresh end-to-end after role changes, password rotation (tokens_valid_after), and staff deactivation (is_active = 0), capturing the HTTP and JWT claim transcript in jwt-refresh-role-live.json (no UI surface is modified by this backend route change); all scenarios and tests passed.

  • Live validation: ✅ go - 3 of 3 scenarios driven live against the product
Scenario Result Live Evidence
Staff member whose role is changed in the database receives the updated role claim in the new JWT when calling POST /api/auth/refresh ✅ pass live ~/.no-mistakes/evidence/01M4VGN14HW71G606NZZC4WH0Q/jwt-refresh-role-live.json
Adversarial: POST /api/auth/refresh rejects a pre-rotation token with 401 when tokens_valid_after advances after credential rotation ✅ pass live ~/.no-mistakes/evidence/01M4VGN14HW71G606NZZC4WH0Q/jwt-refresh-role-live.json
Adversarial: POST /api/auth/refresh rejects a token with 401 when the staff account is deactivated (is_active = 0) ✅ pass live ~/.no-mistakes/evidence/01M4VGN14HW71G606NZZC4WH0Q/jwt-refresh-role-live.json
Evidence: Live API verification transcript for POST /api/auth/refresh role re-read and stale/inactive guards

{ "health": { "status": "ok", "db": "ok", "service": "Flo Local API", "version": "2.4.7", "timestamp": "2026-10-13T20:57:57.625Z" }, "scenario_1_role_re_read_on_refresh": { "staffId": "4ae36c9f-c966-4bc0-a2bd-5111b93a0428", "initialJwtRoleClaim": "manager", "updatedStaffRecordRole": "server", "refreshStatus": 200, "refreshedJwtRoleClaim": "server" }, "scenario_2_stale_token_rejected_after_credential_rotation": { "rotatePasswordStatus": 200, "refreshWithStaleTokenStatus": 401, "refreshWithStaleTokenBody": { "error": "Invalid token" } }, "scenario_3_deactivated_user_rejected_on_refresh": { "deactivateStatus": 200, "deactivatedIsActive": 0, "refreshWithDeactivatedUserStatus": 401, "refreshWithDeactivatedUserBody": { "error": "Invalid token" } } }

{
  "health": {
    "status": "ok",
    "db": "ok",
    "service": "Flo Local API",
    "version": "2.4.7",
    "timestamp": "2026-10-13T20:57:57.625Z"
  },
  "scenario_1_role_re_read_on_refresh": {
    "staffId": "4ae36c9f-c966-4bc0-a2bd-5111b93a0428",
    "initialJwtRoleClaim": "manager",
    "updatedStaffRecordRole": "server",
    "refreshStatus": 200,
    "refreshedJwtRoleClaim": "server"
  },
  "scenario_2_stale_token_rejected_after_credential_rotation": {
    "rotatePasswordStatus": 200,
    "refreshWithStaleTokenStatus": 401,
    "refreshWithStaleTokenBody": {
      "error": "Invalid token"
    }
  },
  "scenario_3_deactivated_user_rejected_on_refresh": {
    "deactivateStatus": 200,
    "deactivatedIsActive": 0,
    "refreshWithDeactivatedUserStatus": 401,
    "refreshWithDeactivatedUserBody": {
      "error": "Invalid token"
    }
  }
}

Pipeline

Updates from git push no-mistakes

✅ **intent** - passed

✅ No issues found.

✅ **Rebase** - passed

✅ No issues found.

✅ **Review** - passed

✅ No issues found.

✅ **Test** - passed

✅ No issues found.

  • Live validation: ✅ go - 3 of 3 scenarios driven live against the product
Scenario Result Live Evidence
Staff member whose role is changed in the database receives the updated role claim in the new JWT when calling POST /api/auth/refresh ✅ pass live ~/.no-mistakes/evidence/01M4VGN14HW71G606NZZC4WH0Q/jwt-refresh-role-live.json
Adversarial: POST /api/auth/refresh rejects a pre-rotation token with 401 when tokens_valid_after advances after credential rotation ✅ pass live ~/.no-mistakes/evidence/01M4VGN14HW71G606NZZC4WH0Q/jwt-refresh-role-live.json
Adversarial: POST /api/auth/refresh rejects a token with 401 when the staff account is deactivated (is_active = 0) ✅ pass live ~/.no-mistakes/evidence/01M4VGN14HW71G606NZZC4WH0Q/jwt-refresh-role-live.json
  • node tests/run-electron-node-test.cjs tests/jwt-logout-lifecycle.test.ts
  • npm run test:phase2
  • npm run build
  • ELECTRON_RUN_AS_NODE=1 FLO_DEV_USER_DATA=<isolated-tmp> electron dev-server.js (live HTTP verification of POST /api/auth/setup/initialize, POST /api/staff, POST /api/auth/login, PUT /api/staff/:id, POST /api/auth/refresh, POST /api/staff/:id/deactivate)
  • npm run test:script-coverage
  • npm run test:staff-authz
✅ **Document** - passed

✅ No issues found.

✅ **Lint** - passed

✅ No issues found.

✅ **Push** - passed

✅ No issues found.

@coderabbitai

coderabbitai Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration
  • Configuration used: Repository: FreeOpenSourcePOS/FloCafe/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 694260ff-3cfc-48c5-8c4b-e66bb4f96dfc

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@khaira777 khaira777 changed the title fix(main): re-read user role from database during JWT refresh fix(auth): use live DB role on token refresh Oct 13, 2026
@khaira777

Copy link
Copy Markdown
Contributor Author

@greptile review

@greptile-apps

greptile-apps Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 4/5

[Critical impact] Token refresh now reads live user role from database.

The functional change appears safe, but the new test comment must meet the repository's comment rule before merging.

Findings

  1. P2 Issue tag in code ▶

Summary

Refresh now signs the user's current database role instead of copying the old token's role.

  • Refreshed tokens now carry the role saved for the user.
  • The lifecycle test keeps the same checks with tidier formatting.

Reviews (1) · Last reviewed commit: "fix(auth): use live DB role on token ref..." · Reviewed by Greptile

Comment thread tests/jwt-logout-lifecycle.test.ts Resolved
@khaira777

Copy link
Copy Markdown
Contributor Author

The main fix looks good. Before merging, please address the remaining Greptile finding.

  • Remove the (#1077) issue reference from the new comment in tests/jwt-logout-lifecycle.test.ts, as required by AGENTS.md.
  • Keep the existing test coverage and production logic unchanged.
  • Run the relevant checks and confirm they pass.

Please keep the change limited to this finding. No unrelated formatting or refactoring.

@khaira777
khaira777 force-pushed the fm/flocafe-issue-1077-r1 branch from d080885 to 4f9b7cc Compare October 13, 2026 21:00
@khaira777 khaira777 changed the title fix(auth): use live DB role on token refresh fix(auth): re-read user role from database during JWT refresh Oct 13, 2026
@khaira777 khaira777 changed the title fix(auth): re-read user role from database during JWT refresh fix(auth): use live DB role on token refresh Oct 13, 2026
@khaira777
khaira777 force-pushed the fm/flocafe-issue-1077-r1 branch from 4f9b7cc to 3b44023 Compare October 13, 2026 21:17
@khaira777
khaira777 enabled auto-merge (squash) October 13, 2026 21:18
khaira777 and others added 2 commits October 13, 2026 17:27
Read the user's current role from the users table lookup during POST /api/auth/refresh instead of copying decoded.role from the previous token, while preserving existing revocation, is_active, and tokens_valid_after checks.

Fixes #1077
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
@khaira777
khaira777 force-pushed the fm/flocafe-issue-1077-r1 branch from 3b44023 to 95ccd52 Compare October 13, 2026 21:27
@khaira777
khaira777 merged commit 9ef02cc into main Oct 13, 2026
38 of 41 checks passed
@khaira777
khaira777 deleted the fm/flocafe-issue-1077-r1 branch October 13, 2026 22:06
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.

fix(auth): re-read user role from database during JWT refresh

1 participant