|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * #29163 — the uncoded refusals on the account self-service doors, |
| 5 | + * `POST /change-email` and `POST /update-user`, answered with a code. |
| 6 | + * |
| 7 | + * ## The defect, at the seam |
| 8 | + * |
| 9 | + * better-auth (`dist/api/routes/update-user.mjs`, measured against the |
| 10 | + * installed `1.7.12`) refuses three conditions on these two doors with |
| 11 | + * `APIError.fromStatus("BAD_REQUEST", { message })` — a message and no `code`: |
| 12 | + * |
| 13 | + * POST /change-email {"newEmail": <the caller's own address>} |
| 14 | + * -> 400 {"message":"Email is the same"} |
| 15 | + * POST /change-email (no email transport on the deployment) |
| 16 | + * -> 400 {"message":"Verification email isn't enabled"} |
| 17 | + * POST /update-user {} (or only keys the user schema does not declare) |
| 18 | + * -> 400 {"message":"No fields to update"} |
| 19 | + * |
| 20 | + * Every other refusal on the same doors carries one: a malformed `newEmail` |
| 21 | + * answers `{"message":…,"code":"VALIDATION_ERROR"}`, an `email` key on |
| 22 | + * `/update-user` answers `EMAIL_CAN_NOT_BE_UPDATED`, an anonymous caller |
| 23 | + * `UNAUTHORIZED`. An ADR-0112 client branches on `code`, never on prose, so |
| 24 | + * these three could only be told apart by string-matching the vendor's English. |
| 25 | + * |
| 26 | + * ## What this does |
| 27 | + * |
| 28 | + * Runs in the `after` hook, where better-auth hands the thrown `APIError` over |
| 29 | + * on `ctx.context.returned`, and re-throws exactly these refusals as |
| 30 | + * `APIError('BAD_REQUEST', { code, message })` with the vendor's own message. |
| 31 | + * better-auth's hook runner replaces the returned value with an `APIError` an |
| 32 | + * after hook throws, so the status, the message and the moment of the refusal |
| 33 | + * (before anything is written or mailed) stay the vendor's; only the missing |
| 34 | + * `code` is added. The body keeps the door's own flat `{ message, code }` shape, |
| 35 | + * the shape its coded siblings already answer in. |
| 36 | + * |
| 37 | + * Each code is one the ledger already holds (ADR-0112), none new: |
| 38 | + * |
| 39 | + * - `VALIDATION_ERROR` (standard catalog) for the two request-content |
| 40 | + * refusals — the code this door already answers for a malformed address; |
| 41 | + * - `EMAIL_SERVICE_REQUIRED` (`@objectstack/plugin-auth`) for the missing |
| 42 | + * transport — the code plugin-auth's own invite import answers for the same |
| 43 | + * deployment condition (`admin-import-users.ts`). |
| 44 | + * |
| 45 | + * Recognition is deliberately narrow: one of the two paths, a 400 `APIError`, |
| 46 | + * no `code` of its own, and a message that IS one of the vendor's sentences |
| 47 | + * listed for that path. Every other answer — including a future vendor release |
| 48 | + * that codes these itself — passes through untouched, and the vendored library |
| 49 | + * is not patched. |
| 50 | + */ |
| 51 | + |
| 52 | +import type { RegisteredErrorCode, StandardErrorCode } from '@objectstack/spec/api'; |
| 53 | + |
| 54 | +/** The standard-catalog code for the two request-content refusals. */ |
| 55 | +const VALIDATION_ERROR_CODE: StandardErrorCode = 'VALIDATION_ERROR'; |
| 56 | + |
| 57 | +/** plugin-auth's ledgered code for a deployment with no email transport. */ |
| 58 | +const EMAIL_SERVICE_REQUIRED_CODE: RegisteredErrorCode = 'EMAIL_SERVICE_REQUIRED'; |
| 59 | + |
| 60 | +/** The ledgered codes this module stamps (see the module header for each). */ |
| 61 | +type SelfServiceRefusalCode = StandardErrorCode | RegisteredErrorCode; |
| 62 | + |
| 63 | +/** |
| 64 | + * Per door, the vendor's uncoded sentence and the code it answers with. Keyed |
| 65 | + * on better-auth's own `ctx.path` spelling. |
| 66 | + */ |
| 67 | +export const SELF_SERVICE_UNCODED_REFUSALS: Readonly<Record<string, Readonly<Record<string, SelfServiceRefusalCode>>>> = { |
| 68 | + '/change-email': { |
| 69 | + 'Email is the same': VALIDATION_ERROR_CODE, |
| 70 | + "Verification email isn't enabled": EMAIL_SERVICE_REQUIRED_CODE, |
| 71 | + }, |
| 72 | + '/update-user': { |
| 73 | + 'No fields to update': VALIDATION_ERROR_CODE, |
| 74 | + }, |
| 75 | +}; |
| 76 | + |
| 77 | +/** |
| 78 | + * Re-throw a listed uncoded refusal on `/change-email` or `/update-user` with |
| 79 | + * its code. A no-op on every other path and every other answer. Call it LAST |
| 80 | + * in the hook section that handles the path: the throw ends the hook. |
| 81 | + */ |
| 82 | +export async function codeSelfServiceRefusal(ctx: any): Promise<void> { |
| 83 | + const path = ctx?.path; |
| 84 | + if (typeof path !== 'string' || !Object.prototype.hasOwnProperty.call(SELF_SERVICE_UNCODED_REFUSALS, path)) return; |
| 85 | + const sentences = SELF_SERVICE_UNCODED_REFUSALS[path]; |
| 86 | + const returned = ctx?.context?.returned; |
| 87 | + const { APIError, isAPIError } = await import('better-auth/api'); |
| 88 | + if (!isAPIError(returned)) return; |
| 89 | + if (returned.statusCode !== 400) return; |
| 90 | + const body = returned.body as { code?: unknown; message?: unknown } | undefined; |
| 91 | + if (body?.code !== undefined) return; |
| 92 | + const message = body?.message; |
| 93 | + if (typeof message !== 'string' || !Object.prototype.hasOwnProperty.call(sentences, message)) return; |
| 94 | + throw new APIError('BAD_REQUEST', { code: sentences[message], message }); |
| 95 | +} |
0 commit comments