Skip to content

fix(signup): capture first/last name reliably (escalation #382 root cause) - #4781

Merged
bokelley merged 4 commits into
mainfrom
bokelley/capture-name-at-signup
May 19, 2026
Merged

bokelley merged 4 commits into
mainfrom
bokelley/capture-name-at-signup

Conversation

@bokelley

Copy link
Copy Markdown
Contributor

Summary

Closes the root cause behind escalation #382 (Tom Hespos's "undefined undefined" credential) and the 12 other learners surfaced by the dry-run of the repair script that shipped in #4768.

Three coordinated changes — one to silently recover existing data, one to ask the user when we still can't, and one to keep the source of truth in sync:

  1. OAuth callback now applies the Slack-name fallback that the WorkOS webhook already had. Of the 11 nameless users on prod, 8 already have a slack_user_mappings.slack_real_name we can split into first/last (Lillie Ratliff, Bryan Szekely, Ángel Ruiz, Ken Ripley, Sydney Fuhrman, Maithili Jalihal, Brian Gilbert, Daniel Di Tullio). Next sign-in silently backfills them. server/src/http.ts:6504-6526.
  2. Onboarding gates on a name. For the residual case — no Slack mapping, no WorkOS profile — /onboarding.html shows a "What should we call you?" step before the rest of the flow. Submits to the existing PUT /api/me/name. server/public/onboarding.html.
  3. PUT /api/me/name pushes back to WorkOS via userManagement.updateUser, so the next webhook can't overwrite our local update with empty values. server/src/http.ts:7343-7398.

The cascade (WorkOS → existing DB → Slack) is extracted into server/src/utils/resolve-user-name.ts so the auth callback and the user.updated webhook can't drift; the webhook now calls the same helper.

Why this matters

Today: a learner can earn an AdCP credential without ever having a name in WorkOS or our users table — they sign in via Slack-linked WorkOS auth or magic-link, never fill in name fields, and the credential issues with "undefined undefined" on the certificate. The repair script in #4768 cleans up after the fact; this PR stops it happening at all.

Test plan

  • npx vitest run tests/unit/resolve-user-name.test.ts — 9 cases (existing DB preserve, Slack real_name fallback, slack_display_name fallback, single-word names, partial WorkOS data, whitespace-only WorkOS values, no row in DB, etc.)
  • npx vitest run tests/unit/workos-webhook-email-refresh.test.ts tests/unit/workos-webhook-user-deleted-wiring.test.ts — webhook regression coverage, 8/8 still pass after extracting the cascade
  • npm run typecheck clean
  • Precommit (test:unit + dynamic-imports + callapi-state-change + typecheck) green
  • Post-merge: dry-run the repair script from fix(certification): credential recipient no longer becomes "undefined undefined" #4768 to see how many of the 11 email-fallback users we can now upgrade after one sign-in cycle
    • fly ssh console -a adcp-docs -C 'node /app/dist/scripts/repair-credential-recipient-names.js'
  • Manually exercise the onboarding gate (sign in as a test user with null name, verify the form blocks, verify saved name shows up in WorkOS)

🤖 Generated with Claude Code

bokelley and others added 2 commits May 19, 2026 06:28
…ad "undefined undefined"

Closes the gap behind escalation #382 (Tom Hespos plus 12 other learners
with empty names on prod credentials):

1. Auth callback now applies the same Slack-name fallback the WorkOS
   webhook already had. 8 of the 11 nameless users on prod have a
   slack_user_mappings.slack_real_name we can split into first/last —
   next sign-in silently backfills them.

2. Onboarding gates on a name. When callback fallbacks still produce
   nothing (no Slack mapping, no WorkOS profile), /onboarding.html
   shows a "What should we call you?" step before the rest of the
   flow. Submits to the existing PUT /api/me/name.

3. PUT /api/me/name pushes back to WorkOS. Previously we wrote only
   to users + organization_memberships; WorkOS itself stayed NULL,
   which meant the next webhook could overwrite us with empty values.

The Slack-fallback cascade is extracted into a shared util so the
auth callback and the user.updated webhook can't drift.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Closes the residual gap from the OAuth-callback Slack fallback: admin
links and email-based auto-links happen out-of-band from the OAuth
callback, so a learner can be Slack-linked, earn a credential via Sage,
and never sign in via OAuth in between. mapUser now runs the same
cascade inline on every successful link.

- Pure DB operation; no cross-layer dependency on the WorkOS client
- COALESCE preserves user-set names (won't overwrite anything they
  set via /api/me/name)
- 6 integration tests cover: real_name fill, display_name fallback,
  single-word names, existing-name preservation, partial fills,
  no-op when Slack has nothing

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
* mapUser now does the cascade inline on every link.
*/
import { describe, it, expect, beforeAll, afterAll, beforeEach } from 'vitest';
import { getPool, initializeDatabase, closeDatabase } from '../../src/db/client.js';
bokelley and others added 2 commits May 19, 2026 07:05
…n test

'admin' isn't a valid mapping_source — the type is 'email_auto' |
'manual_admin' | 'user_claimed'. Check constraint rejected all 6 inserts
in CI. No production code affected.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Code review (must-fix):
- Extract splitFullName helper used by both resolveUserNameWithFallbacks
  and SlackDatabase.mapUser. mapUser previously used indexOf(' ') while
  the helper used split(/\s+/); double-space / tab inputs diverged.
- Onboarding gate now sets currentUser optimistically + a
  nameJustCaptured guard before re-entering init(). Prevents the gate
  from re-firing if /api/me momentarily still returns null first_name
  after the PUT (cache, read-after-write race).
- Drop firstName/lastName PII from the PUT /api/me/name info log line;
  userId is sufficient to correlate.

Security review (defense-in-depth):
- Strip C0/C1 controls + Unicode direction/format characters (RTL-
  override spoofing, ZWSP, BOM) from all name inputs. Centralized in a
  sanitizeName helper used by splitFullName (Slack paths) and the
  PUT /api/me/name handler.
- Cap each name half at 255 chars (matches users table column).
- Reject overlong raw input before sanitizing so the user sees a clear
  422 instead of silent truncation.

13 new unit tests for sanitizeName + splitFullName.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@bokelley
bokelley merged commit c67f1a2 into main May 19, 2026
13 checks passed
@bokelley
bokelley deleted the bokelley/capture-name-at-signup branch May 19, 2026 14:27
bokelley added a commit that referenced this pull request May 19, 2026
closes #4782) (#4799)

* fix(certification): block credential issuance when learner has no name (#4782)

Closes #4782 — the safety net behind escalation #382. Even after the
auth-callback Slack fallback and onboarding gate in #4781, an exotic
auth path could still drop a learner at credential-issuance time without
a name. Today that produced an email-on-certificate; now it produces a
one-turn conversational recovery.

- issueCertifierBadge returns a NAME_REQUIRED sentinel (without calling
  Certifier) when users.first_name is empty. The credential row is still
  awarded in user_credentials; only Certifier-side issuance is deferred.
- checkAndFormatCredentials now also retries previously-deferred issuances
  on every call, so the post-set_my_name retry finalizes the certificate.
- set_my_name (new learner-self tool in member-tools) wraps the same
  write-through-to-WorkOS path as the onboarding form. Sage's prompt rule
  ("Credential name recovery") tells her to ask for first+last on seeing
  the marker, call set_my_name, then check_credentials to finalize.
- check_credentials (new tool) runs an award/issue pass on demand. Used
  by the recovery flow and safe for "did I earn anything new?" prompts.
- NAME_REQUIRED_MARKER constant exported and referenced from all three
  sites (sentinel, warning line, Sage rule) so they can't drift — guarded
  by a unit test.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

* review: address code review findings on PR #4799

Code reviewer (must-fix):
- issueCertifierBadge now reads name via resolveUserNameWithFallbacks
  with the live DB pool, not the closure-bound memberContext that goes
  stale after set_my_name writes through. Closes the loop where Sage
  set the name but check_credentials still saw the gate fire.
- Deferred-issuance retry in checkAndFormatCredentials is bounded to a
  24-hour window so we never re-issue a legitimately-issued older
  credential whose certifier_credential_id was nulled out-of-band.

Code reviewer (should-fix):
- Move "Credential earned!" line *after* the gate check so a gated
  attempt no longer announces the credential before deferring.
- Dedupe toProcess via Set (awarded + deferred could overlap on a race
  between checkAndAwardCredentials and getUserCredentials).
- Narrow IssueResult union to literal 'NAME_REQUIRED' instead of
  typeof-the-constant (which collapsed to plain string).
- Marker test now also pins the source-reference count so a refactor
  that drops one of the three load-bearing sites fails loud.

Security reviewer (should-fix):
- Rate-limit set_my_name (per-user default cap: 60/10min) — bounds
  WorkOS updateUser calls.
- Rate-limit check_credentials (same cap) — bounds fan-out to Certifier
  issuance API.

Cache-invalidation scope: invalidateMemberContextCache() with no arg
clears the whole cache. This matches the pre-existing PUT /api/me/name
behavior in http.ts:7400 — narrowing both is a separate fix.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

---------

Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
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