Repository navigation
fix(platform): confirm the password before adding a passkey - #4336
Merged
Merged
Conversation
Registering a passkey failed with a bare "Session is not fresh" once the session was more than a day old. Better Auth's passkey plugin lets a session register one only within `session.freshAge` (24 h) of signing in, and everyone the post-grace two-factor wall catches stayed signed in through the whole grace window, so the wall's "Register a passkey instead" failed for exactly the people it is for. Better Auth has no client-callable way to freshen a session short of a full sign-in. - `POST /api/auth/reauthenticate` (a Better Auth plugin): a signed-in person confirms their password and the session is rotated into a fresh one. The password goes through the sign-in throttle (per-account lockout + per-IP flood guard, now shared in `password-attempts.ts`), counts on the same failure counter and audits `login_attempt` / `login_lockout` / `login_success` stamped `reauthentication`. The old token is deleted, so a copied cookie does not become fresh with it. - The passkey dialog shared by the wall and Settings > Account checks the session's age when it opens (`SESSION_FRESH_AGE_SECONDS`, now pinned in the auth config, with a five-minute margin for the ceremony) and asks for the password first; a registration the server still refuses as not fresh comes back to that step with the typed name kept. An account without a password is offered Sign in again. - EN/DE/FR messages and docs (the docs also name the account page's real Two-factor authentication and Passkeys sections, not a "Security" one), manual boxes AUTH-F29/F30/B17, and the checkStaleSessionReauthentication integration lane.
The REST error-code guard scans every module the /api/v1 handlers reach, auth.ts and its plugins included. INVALID_PASSWORD and PASSWORD_NOT_SET come from /api/auth/reauthenticate, which REST never mounts, so they join the other Better Auth-only codes in APP_ONLY_CODES. PASSWORD_NOT_SET is spelled out at its throw so the guard sees it; the endpoint test pins it to the shared constant the app reads.
This was referenced Oct 5, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adding a passkey failed with a bare "Session is not fresh" once the session was more than a day old. Better Auth's passkey plugin only lets a session signed in within
session.freshAge(24 h) register one (freshSessionMiddlewareon both registration calls). The post-grace 2FA enrollment wall hit this every time: the people it catches stayed signed in through the whole grace window. Better Auth has no client-callable way to freshen a session short of a full sign-in. For an account with an authenticator, a full sign-in drops the session cookie until the code is entered, and it leaves the old session alive.The passkey dialog is shared by the wall and Settings > Account > Passkeys. It now checks the session's age when it opens and asks for the password first. The new
POST /api/auth/reauthenticateconfirms the password and swaps in a fresh session.What changed
POST /api/auth/reauthenticate: a small Better Auth plugin inbackend/auth/reauthenticate.ts.backend/auth/password-attempts.ts. It counts on the same failure counter.login_attempt/login_lockout/login_success, stampedmetadata.reauthentication: true.PASSWORD_NOT_SET.freshAgeis pinned fromlib/shared/constants/session-freshness.ts(still Better Auth's 24 h default), so the app and the server agree on the window.SESSION_NOT_FRESH(clock skew, a dialog left open), the dialog returns to that step and keeps the typed name.useLockoutMessage.ßor quotes in the new strings, so no de-CH override is needed. The docs also now name the account page's real Two-factor authentication / Passkeys sections; the page has no "Security" section.AUTH-F29,AUTH-F30,AUTH-B17, plus an automation-register row.Checklist
bun run check— partially, locally. The platformtypecheckandoxlint --type-awareare clean. The affected server and UI suites,tests/guards, the i18n catalog tests,services/docstests,lint:linksandlint:manualare green. I left the full turbo run to CI. The machine was at load 100–130: threeaccount-form.passwordtests (untouched; that file mocks the passkey section) timed out at 5 s and pass with--testTimeout=60000.bun run lint:sast— Opengrep on the changed source files: 0 findings. The pre-commit hook ran it too.services/platform/messages/{en,de,fr}.yml.docs/{en,de,fr}/platform/admin/two-factor-authentication.md.Test plan
backend/auth/reauthenticate.test.ts: real Better Auth with the memory adapter. Stale → 403SESSION_NOT_FRESH; confirm → fresh session, old cookie 401; wrong password, throttle and no-password are refused without touching the session.backend/auth/password-attempts.test.ts,backend/domains/login_attempts/service.reauthentication.test.ts,app/lib/auth/session-freshness.test.ts.passkey-register-dialog.test.tsx: 7 of its 8 cases fail against the old dialog.ITEST_LANES=checkLoginThrottleAndAuditChain,checkTwoFactor,checkStaleSessionReauthentication→ 27/27, including the refactored sign-in lockout lane./log-in?redirectTo=…; six wrong passwords → 429 lock message.AUTH-F29,AUTH-F30,AUTH-B17inservices/platform/tests/manual/suites/auth.md.Notes
/two-factor/enable,/two-factor/disableand/users/update-passwordstill verify passwords without the lockout counter. That is pre-existing and left as is; this PR only makes the new door count.