Skip to content

Keep account emails unique and look up logins by email claim - #199

Open
NetworkTheoryAppliedResearchInstitute wants to merge 2 commits into
mainfrom
fix/unique-email-login
Open

NetworkTheoryAppliedResearchInstitute wants to merge 2 commits into
mainfrom
fix/unique-email-login

Conversation

@NetworkTheoryAppliedResearchInstitute

Copy link
Copy Markdown
Collaborator

Registration (/api/auth/register, POST /users) now writes a UserEmails claim (trimmed, lower-cased email to user id) and the user in one DynamoDB transaction. Both writes are conditional: a taken email returns 409 and an existing id is never overwritten. Login reads the claim, then the user by id, instead of the first row of a single scan.

Operator steps before deploy

  1. Create UserEmails (hash key email; command in backend/README.md) and grant GetItem/PutItem on it.
  2. Run node scripts/claim-user-emails.js, deploy, then run it again.

Accounts that already share an email stay unclaimed and are listed by id; they cannot log in until an operator chooses the owner.

Tests: duplicate registration across case and spacing, a held-write race, simulated condition failure, no overwrite by id, login beyond the first scan page, existing duplicates, backfill pagination.

Overlaps #197 (authRoutes.js, userRoutes.js; same 409 wording) and #194 (userRoutes.js).

🤖 Generated with Claude Code

Registration and POST /users now write a UserEmails claim, keyed by the
trimmed, lower-cased email, together with the user in one DynamoDB
transaction. The claim's attribute_not_exists condition makes a second
account for the same email fail with 409, including concurrent requests,
and the user put is conditional on a new id so no existing account is
replaced. Login reads the claim and fetches that user by id instead of
taking the first row of one unpaginated scan.

Add scripts/claim-user-emails.js to claim the emails of existing
accounts. It only adds claims; an email shared by several accounts stays
unclaimed and is reported by id, so login does not choose between them.
Document the table, permissions and rollout order in backend/README.md
and note the private table in CONFORMANCE.md.

Expose transactWrite through the DynamoDB wrapper. Tests cover duplicate
registration across case and spacing, a race in which both requests
reach the write before either commits, simulated condition failures,
refusing to replace an existing id, consistent reads at login, login
beyond the first scan page, existing duplicates, the backfill's
pagination and re-runs, and the SDK's cancellation reasons.

Signed-off-by: Jodson Graves <info@ntari.org>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The backfill left an email shared by several existing accounts with no
claim, so a later registration could take it. It now writes a claim
that names no account (needsReview: true): registration is refused,
and login finds no owner until an operator replaces the claim with the
owner's userId. Login treats such a claim as no account.

Rows imported from a federation peer (sourceNodeId) are never claimed.
The README now describes both, and notes that rows imported before
peers were marked carry no such field.

Tests: a shared email gets the held claim, a new registration for it
is refused, login finds no owner, and a re-run leaves the hold in
place; a peer-imported row is never claimed. Both fail against the
previous model. Full suite: node:test 67/67, Jest 54/54.

Signed-off-by: Jodson Graves <info@ntari.org>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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