Repository navigation
fix(auth): use live DB role on token refresh - #1114
Conversation
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the ⚙️ Run configuration
You can disable this status message by setting the Use the checkbox below for a quick retry:
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. Comment |
|
@greptile review |
|
|
The main fix looks good. Before merging, please address the remaining Greptile finding.
Please keep the change limited to this finding. No unrelated formatting or refactoring. |
d080885 to
4f9b7cc
Compare
4f9b7cc to
3b44023
Compare
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>
3b44023 to
95ccd52
Compare
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/refreshcurrently copiesrole: decoded.rolefrom the old JWT instead of reading the user's currentrolefrom theuserstable query already executed during refresh. Readrolefrom the database lookup so refreshed tokens carry the user's current role while preserving existingis_activeandtokens_valid_afterchecks. Deliver via theno-mistakespipeline as its own PR withFixes #1077in the PR summary so the issue and PR are linked.What Changed
rolealongsideis_activeandtokens_valid_afterfrom theuserstable duringPOST /api/auth/refreshinmain/routes/auth.ts, and sign the refreshed JWT withuser.roleinstead of copyingdecoded.rolefrom the incoming token.tests/jwt-logout-lifecycle.test.tsto 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.
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" } } }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.
node tests/run-electron-node-test.cjs tests/jwt-logout-lifecycle.test.tsnpm run test:phase2npm run buildELECTRON_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-coveragenpm run test:staff-authz✅ **Document** - passed
✅ No issues found.
✅ **Lint** - passed
✅ No issues found.
✅ **Push** - passed
✅ No issues found.