Skip to content

fix: persist verified user_id on auth login#393

Merged
tomasz-tomczyk merged 1 commit intomainfrom
fix-share-user-id
Apr 29, 2026
Merged

fix: persist verified user_id on auth login#393
tomasz-tomczyk merged 1 commit intomainfrom
fix-share-user-id

Conversation

@tomasz-tomczyk
Copy link
Copy Markdown
Owner

Summary

Two attribution bugs surfaced from a prod miss where shared comments came back with user_id = nil and author_identity = "imported" even though the user was logged in:

  1. crit auth login never saved auth_user_id — the device-flow token response only carried access_token, user_name, user_email. The daemon then stamped comments with a blank user_id and the server wrote them as imported. Fixed in companion crit-web PR fix: add --author flag to all reply-to examples #141 (server now returns user_id); this PR consumes it.

  2. Stale identity survived re-login — the existing code wrote auth_user_name/auth_user_email one at a time, so when a user logged in as account B after previously using account A, A's auth_user_id (and any field B's response omitted) stayed on disk. The payload then contained user A's id while the bearer token authenticated as user B; the server treated the mismatch as anonymous.

Changes

  • Add UserID to tokenResponse.
  • Extract saveAuthIdentity() that rewrites all three identity fields atomically: missing fields delete the key on disk so a stale value cannot survive a re-login.
  • Belt-and-braces: when the response omits user_id (older crit-web), fall through to lazyBackfillAuthUserID() which fetches via whoami.

Migration

Existing logged-in users need to log out + log in to pick up the verified user_id. Comments authored before then stay anonymous on the server (intentional — the explicit re-login is the migration).

Review

  • Code review: passed inline; new tests cover the regression
  • Parity audit: N/A (no frontend touched)

Test plan

  • gofmt -l . — clean
  • golangci-lint run ./... — 0 issues
  • go test -race -count=1 ./... — clean
  • New tests: TestSaveAuthIdentity_OverwritesStaleFields (the prod regression), TestSaveAuthIdentity_RemovesFieldsAbsentFromResponse, TestPollDeviceToken_ParsesUserID
  • Full crit↔crit-web share integration suite (./scripts/e2e-share.sh) — all tests pass

Follow-up: #392 (consolidate the five overlapping author/identity config fields).

See also: tomasz-tomczyk/crit-web#141 (server side)

🤖 Generated with Claude Code

Two bugs surfaced from a prod attribution miss:

1. crit auth login wrote auth_user_name and auth_user_email but never
   auth_user_id, because the device-flow token response didn't carry it.
   The daemon then stamped comments with a blank user_id and the server
   wrote them as "imported".

2. The identity fields were saved one at a time, so a stale auth_user_id
   from a previous account survived a re-login as a different user. The
   payload then contained user A's id while the bearer token authenticated
   as user B; the server treated the mismatch as anonymous.

Fix:
- Add UserID to tokenResponse so the CLI reads the new field that
  tomasz-tomczyk/crit-web#141 emits.
- Extract saveAuthIdentity() that rewrites all three identity fields
  atomically: missing fields delete the key on disk so a stale value
  cannot survive a re-login.
- Belt-and-braces: when the response omits user_id (older crit-web),
  fall through to lazyBackfillAuthUserID() which fetches via whoami.

Existing logged-in users need to log out + log in to pick up the
verified user_id; comments authored before then stay anonymous on the
server (intentional — the explicit re-login is the migration).

Follow-up: consolidate the five overlapping author/identity config
fields (#392).

See also: tomasz-tomczyk/crit-web#141 (server side)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 29, 2026

Codecov Report

❌ Patch coverage is 66.66667% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 66.64%. Comparing base (d8b83bf) to head (2339be9).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
auth.go 66.66% 6 Missing ⚠️

❌ Your patch status has failed because the patch coverage (66.66%) is below the target coverage (80.00%). You can increase the patch coverage or adjust the target coverage.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #393      +/-   ##
==========================================
+ Coverage   66.53%   66.64%   +0.10%     
==========================================
  Files          19       19              
  Lines        8197     8206       +9     
==========================================
+ Hits         5454     5469      +15     
+ Misses       2316     2311       -5     
+ Partials      427      426       -1     
Flag Coverage Δ
e2e 34.07% <0.00%> (+0.08%) ⬆️
unit 62.67% <66.66%> (+0.11%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@tomasz-tomczyk tomasz-tomczyk merged commit da47740 into main Apr 29, 2026
5 of 6 checks passed
@tomasz-tomczyk tomasz-tomczyk deleted the fix-share-user-id branch April 29, 2026 15:45
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