@molecule/api-resource-user 1.7.5 → 1.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +269 -73
- package/dist/handlers/create.d.ts.map +1 -1
- package/dist/handlers/create.js +5 -5
- package/dist/handlers/create.js.map +1 -1
- package/dist/index.d.ts +42 -29
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +42 -29
- package/dist/index.js.map +1 -1
- package/dist/provisioning.d.ts +245 -0
- package/dist/provisioning.d.ts.map +1 -0
- package/dist/provisioning.js +585 -0
- package/dist/provisioning.js.map +1 -0
- package/dist/schema.d.ts +11 -0
- package/dist/schema.d.ts.map +1 -1
- package/dist/schema.js +11 -0
- package/dist/schema.js.map +1 -1
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@ AUTO-GENERATED — DO NOT EDIT THIS FILE.
|
|
|
3
3
|
Generated by `mlcl sync-docs` from the package's src/index.ts JSDoc + mlcl/registry.json.
|
|
4
4
|
Edits here are overwritten on the next commit (molecule's pre-commit hook regenerates).
|
|
5
5
|
To change this document, edit the module-level JSDoc in src/index.ts.
|
|
6
|
-
Generated: 2026-10-
|
|
6
|
+
Generated: 2026-10-26T04:23:58.846Z
|
|
7
7
|
-->
|
|
8
8
|
|
|
9
9
|
# @molecule/api-resource-user
|
|
@@ -23,35 +23,26 @@ The `user` resource types, schema, and definition.
|
|
|
23
23
|
**`usersSecrets.id` IS the user's id — the value of `users.id`, never a fresh uuid.** The DDL
|
|
24
24
|
declares it `UNIQUE PRIMARY KEY` with no foreign key, so nothing enforces it: a row written
|
|
25
25
|
under another id is never found again — login answers "invalid credentials" while
|
|
26
|
-
`GET /api/users/me` still answers 200.
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
const deviceId = await get<DeviceService>('device')?.createOrUpdate(userId, deviceName)
|
|
47
|
-
const token = authorization.set(req, res, { userId, deviceId })
|
|
48
|
-
```
|
|
49
|
-
|
|
50
|
-
`get<DeviceService>('device')` is the typed accessor (never hand-type a
|
|
51
|
-
`get<{ createOrUpdate(): … }>('device')` generic: a wrong one still compiles and leaves the
|
|
52
|
-
field you invented — `device.id` — `undefined`), and `createOrUpdate` RESOLVES TO THE DEVICE ID
|
|
53
|
-
STRING, not to a row. `set()` throws when `deviceId` is missing rather than mint a token every
|
|
54
|
-
later request answers 401.
|
|
26
|
+
`GET /api/users/me` still answers 200.
|
|
27
|
+
|
|
28
|
+
**Server code that creates accounts — a seed, a migration, a content author, a service
|
|
29
|
+
account — uses `provisionUser`, never hand-written rows.** `provisionUser({ name, tableName },
|
|
30
|
+
{ props, password? })` validates the props with the resource schema (once, before the identity
|
|
31
|
+
lookup, so a schema `transform` decides the stored username/email), writes the `users` row and
|
|
32
|
+
its same-id `usersSecrets` row through the bonded providers — in one transaction where the
|
|
33
|
+
database supports one — and returns `{ status: 'created' | 'completed' | 'existing', id, props,
|
|
34
|
+
hasPassword }` with NO device, JWT or session. Omit `password` for a content-only author that
|
|
35
|
+
cannot log in (no `passwordHash` is written). It is idempotent by `username`/`email` and never
|
|
36
|
+
changes an existing account's credentials. Where the bond has no transactions (Cloudflare D1) a
|
|
37
|
+
credential-row failure leaves the user row and answers `{ status: 'partial', id, recovery }`;
|
|
38
|
+
finish it with `provisionUser(..., { props, completeId: id })` — a retry without `completeId`
|
|
39
|
+
answers `existing` and repairs nothing. **TRUSTED SERVER CALLERS ONLY:** it takes props
|
|
40
|
+
directly, so a public endpoint built on it is a mass-assignment hole.
|
|
41
|
+
|
|
42
|
+
For the HTTP path use `handlers.create` (signup), `handlers.logIn`, `handlers.updatePassword`,
|
|
43
|
+
`handlers.forgotPassword` / `handlers.resetPassword`. To touch one secret yourself use the
|
|
44
|
+
datastore on the same key — `findById('usersSecrets', userId)` (or `${tableName}Secrets` for a
|
|
45
|
+
custom `tableName`) — never SQL.
|
|
55
46
|
|
|
56
47
|
**RealWorld-style token API (`Authorization: Token <jwt>`)? CONFIGURATION on this same auth
|
|
57
48
|
path — never a credentials table of your own** (`usersSecrets.passwordHash` is the password
|
|
@@ -116,6 +107,27 @@ answers 400 `resource.error.unknownError` with
|
|
|
116
107
|
`TypeError: schema.safeParse is not a function`, and no row is written. Omit `schema`
|
|
117
108
|
instead of stubbing it.
|
|
118
109
|
|
|
110
|
+
**Issuing a session yourself (the sequence is device, then session — two calls, and the first
|
|
111
|
+
RETURNS the id):** the middleware accepts both `Authorization: Bearer <jwt>` and
|
|
112
|
+
`Authorization: Token <jwt>` (RealWorld's scheme), but only for a token whose `deviceId` names
|
|
113
|
+
an ACTIVE row in `devices` — a JWT you `sign()` with an invented `deviceId` is answered 401 on
|
|
114
|
+
every route.
|
|
115
|
+
|
|
116
|
+
```typescript
|
|
117
|
+
import { get } from '@molecule/api-bond'
|
|
118
|
+
import { authorization } from '@molecule/api-resource-user'
|
|
119
|
+
import type { DeviceService } from '@molecule/api-resource-device'
|
|
120
|
+
|
|
121
|
+
const deviceId = await get<DeviceService>('device')?.createOrUpdate(userId, deviceName)
|
|
122
|
+
const token = authorization.set(req, res, { userId, deviceId })
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
`get<DeviceService>('device')` is the typed accessor (never hand-type a
|
|
126
|
+
`get<{ createOrUpdate(): … }>('device')` generic: a wrong one still compiles and leaves the
|
|
127
|
+
field you invented — `device.id` — `undefined`), and `createOrUpdate` RESOLVES TO THE DEVICE ID
|
|
128
|
+
STRING, not to a row. `set()` throws when `deviceId` is missing rather than mint a token every
|
|
129
|
+
later request answers 401.
|
|
130
|
+
|
|
119
131
|
**Exact responses of the sign-up and sign-in handlers** (the body shapes the routes send;
|
|
120
132
|
a handler returns `{ statusCode, body }` and the mount applies it):
|
|
121
133
|
|
|
@@ -194,6 +206,37 @@ npm install @molecule/api-resource-user @molecule/api-bond @molecule/api-config
|
|
|
194
206
|
|
|
195
207
|
### Interfaces
|
|
196
208
|
|
|
209
|
+
#### `ProvisionUserInput`
|
|
210
|
+
|
|
211
|
+
Input accepted by {@link provisionUser}.
|
|
212
|
+
|
|
213
|
+
```typescript
|
|
214
|
+
interface ProvisionUserInput {
|
|
215
|
+
/**
|
|
216
|
+
* The account's SAFE props (`username`, `name`, `email`, `bio`, …). Validated by the
|
|
217
|
+
* resource schema; never a secret column. At least one of `username` / `email` is required —
|
|
218
|
+
* that pair is what makes repeated provisioning find the account it created.
|
|
219
|
+
*/
|
|
220
|
+
props: ProvisionUserProps
|
|
221
|
+
/**
|
|
222
|
+
* Optional plaintext password for an account that must be able to sign in. OMIT it for a
|
|
223
|
+
* content author or service account that must not log in: no hash is written, and `logIn`
|
|
224
|
+
* refuses password authentication for the account. The value is hashed through the bonded
|
|
225
|
+
* password provider and never stored, logged, or returned.
|
|
226
|
+
*/
|
|
227
|
+
password?: string
|
|
228
|
+
/**
|
|
229
|
+
* Completes a provisioning that returned `{ status: 'partial' }`: the exact `id` from that
|
|
230
|
+
* result, which is the proof that this caller created the account. The supplied `props` must
|
|
231
|
+
* identify that same account, it must not be OAuth-linked, and it must not already carry a
|
|
232
|
+
* stored password (an existing password is never overwritten). A concurrent completion or a
|
|
233
|
+
* password reset that already wrote a hash is reported as `existing`, never replaced. Omit this
|
|
234
|
+
* for a normal call.
|
|
235
|
+
*/
|
|
236
|
+
completeId?: string
|
|
237
|
+
}
|
|
238
|
+
```
|
|
239
|
+
|
|
197
240
|
#### `UserRequestHandlerMap`
|
|
198
241
|
|
|
199
242
|
Shape of the user request-handler map produced by `createRequestHandlerMap`.
|
|
@@ -239,6 +282,124 @@ interface UserRequestHandlerMap {
|
|
|
239
282
|
|
|
240
283
|
### Types
|
|
241
284
|
|
|
285
|
+
#### `ProvisionUserProps`
|
|
286
|
+
|
|
287
|
+
Safe user props accepted by {@link provisionUser} — the `users` table's columns without the
|
|
288
|
+
server-controlled `id` / `createdAt` / `updatedAt` (this call sets those).
|
|
289
|
+
|
|
290
|
+
The index signature lets an app pass the extra columns of its OWN resource schema
|
|
291
|
+
(`handlerResource.schema`); the schema still decides what is kept.
|
|
292
|
+
|
|
293
|
+
```typescript
|
|
294
|
+
type ProvisionUserProps = Omit<types.Props, 'createdAt' | 'id' | 'updatedAt'> &
|
|
295
|
+
Record<string, unknown>
|
|
296
|
+
```
|
|
297
|
+
|
|
298
|
+
#### `ProvisionUserFailureReason`
|
|
299
|
+
|
|
300
|
+
Why {@link provisionUser} could not complete.
|
|
301
|
+
|
|
302
|
+
```typescript
|
|
303
|
+
type ProvisionUserFailureReason =
|
|
304
|
+
/** Neither `props.username` nor `props.email` was supplied, so no account can be found on a re-run. */
|
|
305
|
+
| 'identityRequired'
|
|
306
|
+
/** `props.username` breaks the signup rule (letters, digits, dots, hyphens, underscores). */
|
|
307
|
+
| 'invalidUsername'
|
|
308
|
+
/** `password` is outside the signup bounds (8–1024 characters). */
|
|
309
|
+
| 'invalidPassword'
|
|
310
|
+
/** The datastore could not answer "does this account already exist" (or could not read its credentials). */
|
|
311
|
+
| 'lookupFailed'
|
|
312
|
+
/** `username` and `email` resolve to two DIFFERENT existing accounts. */
|
|
313
|
+
| 'conflict'
|
|
314
|
+
/** The props failed the resource schema, so nothing was written. */
|
|
315
|
+
| 'invalidProps'
|
|
316
|
+
/** The `users` insert reported no affected row and no racing account was found. */
|
|
317
|
+
| 'userNotCreated'
|
|
318
|
+
/** The user row was written but its `usersSecrets` row (or the password hash) failed. */
|
|
319
|
+
| 'credentialsFailed'
|
|
320
|
+
/** `completeId` names no account in this resource's table. */
|
|
321
|
+
| 'completeTargetMissing'
|
|
322
|
+
/** The supplied username/email does not identify the account named by `completeId`. */
|
|
323
|
+
| 'completeIdentityMismatch'
|
|
324
|
+
/** The account named by `completeId` has an OAuth identity, so its credentials are not ours to add. */
|
|
325
|
+
| 'completeNotOwned'
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
#### `ProvisionUserResult`
|
|
329
|
+
|
|
330
|
+
The outcome of a {@link provisionUser} call.
|
|
331
|
+
|
|
332
|
+
`created`, `completed` and `existing` carry the account's id (the value of `usersSecrets.id`)
|
|
333
|
+
and its sanitized `users` row. `partial` says the user row was written and its credential row
|
|
334
|
+
was not; `failed` says the call did not complete (and carries `id` only when a user row it
|
|
335
|
+
wrote may still exist).
|
|
336
|
+
|
|
337
|
+
```typescript
|
|
338
|
+
type ProvisionUserResult =
|
|
339
|
+
| {
|
|
340
|
+
/** The account was created by this call. */
|
|
341
|
+
status: 'created'
|
|
342
|
+
/** The user id — the same value stored as `usersSecrets.id`. */
|
|
343
|
+
id: string
|
|
344
|
+
/** The sanitized user row (`users` columns; secret columns stripped). */
|
|
345
|
+
props: SafeProps
|
|
346
|
+
/** Whether a password credential was stored for this new account. */
|
|
347
|
+
hasPassword: boolean
|
|
348
|
+
}
|
|
349
|
+
| {
|
|
350
|
+
/** A partial provisioning named by `completeId` was finished by this call. */
|
|
351
|
+
status: 'completed'
|
|
352
|
+
/** The existing account's id. */
|
|
353
|
+
id: string
|
|
354
|
+
/** The existing account's sanitized user row. */
|
|
355
|
+
props: SafeProps
|
|
356
|
+
/** Whether a password credential is stored for the account now. */
|
|
357
|
+
hasPassword: boolean
|
|
358
|
+
}
|
|
359
|
+
| {
|
|
360
|
+
/** The account already existed for the supplied username/email; nothing was written. */
|
|
361
|
+
status: 'existing'
|
|
362
|
+
/** The existing account's id. */
|
|
363
|
+
id: string
|
|
364
|
+
/** The existing account's sanitized user row. */
|
|
365
|
+
props: SafeProps
|
|
366
|
+
/** Whether the EXISTING account has a stored password hash (never the hash itself). */
|
|
367
|
+
hasPassword: boolean
|
|
368
|
+
}
|
|
369
|
+
| {
|
|
370
|
+
/** The user row was written; its credential row was not. */
|
|
371
|
+
status: 'partial'
|
|
372
|
+
/** The step that failed. */
|
|
373
|
+
reason: 'credentialsFailed'
|
|
374
|
+
/** A stable, translated key for the failure (switch on `reason`, not on `error`). */
|
|
375
|
+
errorKey: string
|
|
376
|
+
/** Diagnostic text for the server log — not a client-facing string. */
|
|
377
|
+
error: string
|
|
378
|
+
/** The id of the account this call created. Pass it back as `completeId` to finish it. */
|
|
379
|
+
id: string
|
|
380
|
+
/**
|
|
381
|
+
* What completing the row needs: whether the original call supplied a password (so the
|
|
382
|
+
* caller knows to supply it again — the plaintext was never stored).
|
|
383
|
+
*/
|
|
384
|
+
recovery: { id: string; passwordRequired: boolean }
|
|
385
|
+
}
|
|
386
|
+
| {
|
|
387
|
+
/** Provisioning did not complete and left no row this call wrote. */
|
|
388
|
+
status: 'failed'
|
|
389
|
+
/** Which step failed. */
|
|
390
|
+
reason: ProvisionUserFailureReason
|
|
391
|
+
/** A stable, translated key for the failure (switch on `reason`, not on `error`). */
|
|
392
|
+
errorKey: string
|
|
393
|
+
/** Diagnostic text for the server log — not a client-facing string. */
|
|
394
|
+
error: string
|
|
395
|
+
/**
|
|
396
|
+
* Present when a user row this call wrote may still exist, or when this call was completing
|
|
397
|
+
* the account named by `completeId` (so the caller can retry the same id).
|
|
398
|
+
*/
|
|
399
|
+
id?: string
|
|
400
|
+
}
|
|
401
|
+
```
|
|
402
|
+
|
|
242
403
|
#### `Props`
|
|
243
404
|
|
|
244
405
|
User props type inferred from schema — the row shape of the `users` table, which holds no
|
|
@@ -335,6 +496,46 @@ type Session = z.infer<typeof sessionSchema>
|
|
|
335
496
|
|
|
336
497
|
### Functions
|
|
337
498
|
|
|
499
|
+
#### `provisionUser(resource, input)`
|
|
500
|
+
|
|
501
|
+
Creates a user record and its same-id credential record for a trusted server caller, without
|
|
502
|
+
creating a session.
|
|
503
|
+
|
|
504
|
+
The props are validated ONCE by the resource schema — this package's `propsSchema` unless
|
|
505
|
+
`schema` is supplied — before the existing-account lookup, so any `transform`/`default` in a
|
|
506
|
+
caller's schema decides the stored and the looked-up username/email. The user row and its
|
|
507
|
+
`usersSecrets` row are then written under ONE freshly minted id, inside a transaction when the
|
|
508
|
+
bonded database supports one. With no `password`, the credential row carries no `passwordHash`
|
|
509
|
+
at all — the account cannot log in, and there is no default, generated, or returned password.
|
|
510
|
+
|
|
511
|
+
Existing accounts are found by `username` or `email` and returned unchanged (`status:
|
|
512
|
+
'existing'`), which makes re-running a seed safe; a supplied password is never applied to an
|
|
513
|
+
existing account. Where the bond has no interactive transactions and the credential write
|
|
514
|
+
fails, the result is `{ status: 'partial', id, recovery }` — pass that id back as `completeId`
|
|
515
|
+
(with the same identity props and the same password) to finish it.
|
|
516
|
+
|
|
517
|
+
Use {@link createResource} when the app constrains or extends user props; omit it otherwise.
|
|
518
|
+
The `tableName` convention is the one every handler in this package uses: the credential table
|
|
519
|
+
is `${tableName}Secrets`.
|
|
520
|
+
|
|
521
|
+
```typescript
|
|
522
|
+
function provisionUser(
|
|
523
|
+
{ name, tableName, schema = propsSchema }: types.HandlerResource,
|
|
524
|
+
{ props, password, completeId }: ProvisionUserInput,
|
|
525
|
+
): Promise<ProvisionUserResult>
|
|
526
|
+
```
|
|
527
|
+
|
|
528
|
+
- `resource` — The user resource configuration: `{ name, tableName, schema? }`.
|
|
529
|
+
- `resource.name` — The resource name used in error messages.
|
|
530
|
+
- `resource.tableName` — The database table for users.
|
|
531
|
+
- `resource.schema` — Optional schema validated before the write. Defaults to `propsSchema`.
|
|
532
|
+
- `input` — The account to provision.
|
|
533
|
+
- `input.props` — The SAFE user props to store (never a secret column).
|
|
534
|
+
- `input.password` — Optional plaintext password to hash into the credential row. Omit it for a content-only author, which then has no password and cannot log in.
|
|
535
|
+
- `input.completeId` — The id from an earlier `partial` result; finishes that provisioning.
|
|
536
|
+
|
|
537
|
+
**Returns:** `{ status: 'created' | 'completed' | 'existing', id, props, hasPassword }` on success, `{ status: 'partial', reason, errorKey, error, id, recovery }` when a user row was written but its credentials were not, or `{ status: 'failed', reason, errorKey, error, id? }` otherwise.
|
|
538
|
+
|
|
338
539
|
#### `createRequestHandlerMap(createRequestHandler)`
|
|
339
540
|
|
|
340
541
|
Creates the full request handler map for the User resource.
|
|
@@ -347,11 +548,7 @@ The map's `create` / `logIn` send HTTP responses. For a custom JSON envelope, co
|
|
|
347
548
|
their `{ statusCode, headers?, body }` result instead of reimplementing credential storage.
|
|
348
549
|
|
|
349
550
|
```typescript
|
|
350
|
-
function createRequestHandlerMap(
|
|
351
|
-
createRequestHandler: (
|
|
352
|
-
handler: Handler,
|
|
353
|
-
) => (req: MoleculeRequest, res: MoleculeResponse, next: MoleculeNextFunction) => Promise<void>,
|
|
354
|
-
): UserRequestHandlerMap
|
|
551
|
+
function createRequestHandlerMap(createRequestHandler: RequestHandlerCreator): UserRequestHandlerMap
|
|
355
552
|
```
|
|
356
553
|
|
|
357
554
|
- `createRequestHandler` — Factory from `@molecule/api-resource` that wraps handler configs into Express middleware.
|
|
@@ -372,10 +569,10 @@ two-factor responses. Read `handlers.create` / `handlers.logIn` for their comple
|
|
|
372
569
|
Retain each route's authorization and rate-limit middleware when adapting its envelope.
|
|
373
570
|
|
|
374
571
|
```typescript
|
|
375
|
-
function createResource
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
}): types.Resource
|
|
572
|
+
function createResource<
|
|
573
|
+
OAuthServers extends readonly [string, ...string[]] | undefined = undefined,
|
|
574
|
+
PlanKeys extends readonly [string, ...string[]] | undefined = undefined,
|
|
575
|
+
>(options?: { oauthServers?: OAuthServers; planKeys?: PlanKeys }): types.Resource
|
|
379
576
|
```
|
|
380
577
|
|
|
381
578
|
- `options` — Optional configuration.
|
|
@@ -391,7 +588,14 @@ Creates a full schema for user props.
|
|
|
391
588
|
OAuth servers and plan keys can be constrained by passing them as options.
|
|
392
589
|
|
|
393
590
|
```typescript
|
|
394
|
-
function createSchema
|
|
591
|
+
function createSchema<
|
|
592
|
+
OAuthServers extends readonly [string, ...string[]] | undefined = undefined,
|
|
593
|
+
PlanKeys extends readonly [string, ...string[]] | undefined = undefined,
|
|
594
|
+
>(options?: {
|
|
595
|
+
oauthServers?: OAuthServers
|
|
596
|
+
planKeys?: PlanKeys
|
|
597
|
+
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
598
|
+
}): z.ZodObject<
|
|
395
599
|
{
|
|
396
600
|
id: z.ZodString
|
|
397
601
|
createdAt: z.ZodString
|
|
@@ -451,7 +655,7 @@ const i18nRegistered: true
|
|
|
451
655
|
Default user resource definition.
|
|
452
656
|
|
|
453
657
|
```typescript
|
|
454
|
-
const resource: types.Resource
|
|
658
|
+
const resource: types.Resource
|
|
455
659
|
```
|
|
456
660
|
|
|
457
661
|
#### `routes`
|
|
@@ -490,6 +694,25 @@ Larger avatars must be hosted elsewhere and referenced by URL.
|
|
|
490
694
|
const MAX_AVATAR_LENGTH: number
|
|
491
695
|
```
|
|
492
696
|
|
|
697
|
+
#### `MIN_PASSWORD_LENGTH`
|
|
698
|
+
|
|
699
|
+
Shortest password the package accepts, for signup (`handlers.create`), password changes and
|
|
700
|
+
programmatic provisioning alike — one policy, checked by every write path.
|
|
701
|
+
|
|
702
|
+
```typescript
|
|
703
|
+
const MIN_PASSWORD_LENGTH: 8
|
|
704
|
+
```
|
|
705
|
+
|
|
706
|
+
#### `MAX_PASSWORD_LENGTH`
|
|
707
|
+
|
|
708
|
+
Longest password the package accepts (in characters), for signup (`handlers.create`),
|
|
709
|
+
password changes and programmatic provisioning alike. bcrypt reads only the first 72 BYTES,
|
|
710
|
+
so this cap is a request-size bound rather than a strength claim.
|
|
711
|
+
|
|
712
|
+
```typescript
|
|
713
|
+
const MAX_PASSWORD_LENGTH: 1024
|
|
714
|
+
```
|
|
715
|
+
|
|
493
716
|
#### `propsSchema`
|
|
494
717
|
|
|
495
718
|
Default schema for user props.
|
|
@@ -571,14 +794,14 @@ Schema for creating a user via OAuth.
|
|
|
571
794
|
```typescript
|
|
572
795
|
const createOAuthPropsSchema: z.ZodObject<
|
|
573
796
|
{
|
|
574
|
-
oauthServer: z.ZodOptional<z.ZodString> | z.ZodOptional<z.ZodEnum<{}>>
|
|
575
|
-
oauthId: z.ZodOptional<z.ZodString>
|
|
576
797
|
username: z.ZodOptional<z.ZodString>
|
|
577
798
|
name: z.ZodOptional<z.ZodString>
|
|
578
799
|
email: z.ZodOptional<z.ZodNullable<z.ZodString>>
|
|
579
800
|
emailVerified: z.ZodOptional<z.ZodBoolean>
|
|
580
801
|
avatar: z.ZodOptional<z.ZodNullable<z.ZodString>>
|
|
581
802
|
bio: z.ZodOptional<z.ZodNullable<z.ZodString>>
|
|
803
|
+
oauthServer: z.ZodOptional<z.ZodString> | z.ZodOptional<z.ZodEnum<{}>>
|
|
804
|
+
oauthId: z.ZodOptional<z.ZodString>
|
|
582
805
|
oauthData: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>
|
|
583
806
|
},
|
|
584
807
|
z.core.$strip
|
|
@@ -905,17 +1128,7 @@ user in (register, login, OAuth, password reset), as `handlers/logIn.ts` does: a
|
|
|
905
1128
|
_(description truncated — read the full text in `node_modules/@molecule/api-resource-user/dist/authorization.d.ts`.)_
|
|
906
1129
|
|
|
907
1130
|
```typescript
|
|
908
|
-
function set(
|
|
909
|
-
_req: MoleculeRequest,
|
|
910
|
-
res: MoleculeResponse,
|
|
911
|
-
session: {
|
|
912
|
-
userId: string
|
|
913
|
-
deviceId: string
|
|
914
|
-
id?: string | undefined
|
|
915
|
-
oauthServer?: string | undefined
|
|
916
|
-
oauthId?: string | undefined
|
|
917
|
-
},
|
|
918
|
-
): string | null
|
|
1131
|
+
function set(_req: MoleculeRequest, res: MoleculeResponse, session: Session): string | null
|
|
919
1132
|
```
|
|
920
1133
|
|
|
921
1134
|
- `_req` — The request object (unused; reserved for handler signature parity).
|
|
@@ -2098,24 +2311,7 @@ Get a user's current plan info.
|
|
|
2098
2311
|
Uses the bonded PlanService if available.
|
|
2099
2312
|
|
|
2100
2313
|
```typescript
|
|
2101
|
-
function getPlan(user:
|
|
2102
|
-
id: string
|
|
2103
|
-
createdAt: string
|
|
2104
|
-
updatedAt: string
|
|
2105
|
-
username?: string | undefined
|
|
2106
|
-
name?: string | undefined
|
|
2107
|
-
email?: string | null | undefined
|
|
2108
|
-
emailVerified?: boolean | undefined
|
|
2109
|
-
avatar?: string | null | undefined
|
|
2110
|
-
bio?: string | null | undefined
|
|
2111
|
-
twoFactorEnabled?: boolean | undefined
|
|
2112
|
-
oauthServer?: string | undefined
|
|
2113
|
-
oauthId?: string | undefined
|
|
2114
|
-
oauthData?: Record<string, unknown> | undefined
|
|
2115
|
-
planKey?: string | undefined
|
|
2116
|
-
planExpiresAt?: string | undefined
|
|
2117
|
-
planAutoRenews?: boolean | undefined
|
|
2118
|
-
}): Promise<Plan | PlanInfo | null>
|
|
2314
|
+
function getPlan(user: types.Props): Promise<PlanInfo | Plan | null>
|
|
2119
2315
|
```
|
|
2120
2316
|
|
|
2121
2317
|
- `user` — The user props (needs `planKey`, `planExpiresAt`, `planAutoRenews`).
|
|
@@ -2233,7 +2429,7 @@ Returns a shallow copy of a user row with {@link SENSITIVE_USER_COLUMNS}
|
|
|
2233
2429
|
removed. The input row is not mutated.
|
|
2234
2430
|
|
|
2235
2431
|
```typescript
|
|
2236
|
-
function stripSensitiveUserColumns(user: T): T
|
|
2432
|
+
function stripSensitiveUserColumns<T extends Record<string, unknown>>(user: T): T
|
|
2237
2433
|
```
|
|
2238
2434
|
|
|
2239
2435
|
- `user` — The full user row (as loaded by `findById`).
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"create.d.ts","sourceRoot":"","sources":["../../src/handlers/create.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAA;AAM/E,OAAO,KAAK,KAAK,KAAK,MAAM,aAAa,CAAA;AAQzC,mFAAmF;AACnF,MAAM,WAAW,aAAc,SAAQ,eAAe;IACpD,IAAI,EAAE,KAAK,CAAC,WAAW,GAAG;QACxB,QAAQ,CAAC,EAAE,MAAM,CAAA;QACjB,UAAU,CAAC,EAAE,MAAM,CAAA;KACpB,CAAA;CACF;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,eAAO,MAAM,MAAM,GAAI,6BAA2C,KAAK,CAAC,eAAe,MAOvE,KAAK,aAAa,EAAE,KAAK,gBAAgB;;;;;
|
|
1
|
+
{"version":3,"file":"create.d.ts","sourceRoot":"","sources":["../../src/handlers/create.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAA;AAM/E,OAAO,KAAK,KAAK,KAAK,MAAM,aAAa,CAAA;AAQzC,mFAAmF;AACnF,MAAM,WAAW,aAAc,SAAQ,eAAe;IACpD,IAAI,EAAE,KAAK,CAAC,WAAW,GAAG;QACxB,QAAQ,CAAC,EAAE,MAAM,CAAA;QACjB,UAAU,CAAC,EAAE,MAAM,CAAA;KACpB,CAAA;CACF;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,eAAO,MAAM,MAAM,GAAI,6BAA2C,KAAK,CAAC,eAAe,MAOvE,KAAK,aAAa,EAAE,KAAK,gBAAgB;;;;;aAvB9C,CAAC;;;;;;;;;;;;;;;;EAuQX,CAAA"}
|
package/dist/handlers/create.js
CHANGED
|
@@ -5,7 +5,7 @@ import { t } from '@molecule/api-i18n';
|
|
|
5
5
|
import { hash } from '@molecule/api-password';
|
|
6
6
|
import { create as resourceCreate } from '@molecule/api-resource';
|
|
7
7
|
import * as authorization from '../authorization.js';
|
|
8
|
-
import { propsSchema } from '../schema.js';
|
|
8
|
+
import { MAX_PASSWORD_LENGTH, MIN_PASSWORD_LENGTH, propsSchema } from '../schema.js';
|
|
9
9
|
import { normalizeEmail } from '../utilities/normalizeEmail.js';
|
|
10
10
|
import { stripSensitiveUserColumns } from '../utilities/stripSensitiveUserColumns.js';
|
|
11
11
|
import { isNestedBody, USERNAME_RE } from '../utilities/username.js';
|
|
@@ -97,23 +97,23 @@ export const create = ({ name, tableName, schema = propsSchema }) => {
|
|
|
97
97
|
},
|
|
98
98
|
};
|
|
99
99
|
}
|
|
100
|
-
if (body.password.length >
|
|
100
|
+
if (body.password.length > MAX_PASSWORD_LENGTH) {
|
|
101
101
|
return {
|
|
102
102
|
statusCode: 400,
|
|
103
103
|
body: {
|
|
104
104
|
error: t('user.error.passwordTooLong', undefined, {
|
|
105
|
-
defaultValue:
|
|
105
|
+
defaultValue: `Password must be at most ${MAX_PASSWORD_LENGTH} characters`,
|
|
106
106
|
}),
|
|
107
107
|
errorKey: 'user.error.passwordTooLong',
|
|
108
108
|
},
|
|
109
109
|
};
|
|
110
110
|
}
|
|
111
|
-
if (body.password.length <
|
|
111
|
+
if (body.password.length < MIN_PASSWORD_LENGTH) {
|
|
112
112
|
return {
|
|
113
113
|
statusCode: 400,
|
|
114
114
|
body: {
|
|
115
115
|
error: t('user.error.passwordTooShort', undefined, {
|
|
116
|
-
defaultValue:
|
|
116
|
+
defaultValue: `Password must be at least ${MIN_PASSWORD_LENGTH} characters`,
|
|
117
117
|
}),
|
|
118
118
|
errorKey: 'user.error.passwordTooShort',
|
|
119
119
|
},
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"create.js","sourceRoot":"","sources":["../../src/handlers/create.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,IAAI,IAAI,EAAE,MAAM,MAAM,CAAA;AAEjC,OAAO,EAAE,GAAG,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAA;AACjE,OAAO,EAAE,MAAM,IAAI,WAAW,EAAE,OAAO,EAAE,MAAM,wBAAwB,CAAA;AACvE,OAAO,EAAE,CAAC,EAAE,MAAM,oBAAoB,CAAA;AACtC,OAAO,EAAE,IAAI,EAAE,MAAM,wBAAwB,CAAA;AAE7C,OAAO,EAAE,MAAM,IAAI,cAAc,EAAE,MAAM,wBAAwB,CAAA;AAGjE,OAAO,KAAK,aAAa,MAAM,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,EAAE,EAAE,EAAE,GAAG,KAAK,EAAE,CAA4B,CAC7E,CAAA;gBACD,OAAO;oBACL,UAAU,EAAE,GAAG;oBACf,IAAI,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,WAAW,EAAE,IAAI,EAAE;iBACzC,CAAA;YACH,CAAC;iBAAM,CAAC;gBACN,MAAM,IAAI,KAAK,CAAC,kCAAkC,CAAC,CAAA;YACrD,CAAC;QACH,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,CAAA;YACnB,OAAO;gBACL,UAAU,EAAE,GAAG;gBACf,IAAI,EAAE;oBACJ,KAAK,EAAE,CAAC,CAAC,kCAAkC,CAAC;oBAC5C,QAAQ,EAAE,kCAAkC;iBAC7C;aACF,CAAA;QACH,CAAC;IACH,CAAC,CAAA;AACH,CAAC,CAAA"}
|
package/dist/index.d.ts
CHANGED
|
@@ -13,35 +13,26 @@
|
|
|
13
13
|
* **`usersSecrets.id` IS the user's id — the value of `users.id`, never a fresh uuid.** The DDL
|
|
14
14
|
* declares it `UNIQUE PRIMARY KEY` with no foreign key, so nothing enforces it: a row written
|
|
15
15
|
* under another id is never found again — login answers "invalid credentials" while
|
|
16
|
-
* `GET /api/users/me` still answers 200.
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
* const deviceId = await get<DeviceService>('device')?.createOrUpdate(userId, deviceName)
|
|
37
|
-
* const token = authorization.set(req, res, { userId, deviceId })
|
|
38
|
-
* ```
|
|
39
|
-
*
|
|
40
|
-
* `get<DeviceService>('device')` is the typed accessor (never hand-type a
|
|
41
|
-
* `get<{ createOrUpdate(): … }>('device')` generic: a wrong one still compiles and leaves the
|
|
42
|
-
* field you invented — `device.id` — `undefined`), and `createOrUpdate` RESOLVES TO THE DEVICE ID
|
|
43
|
-
* STRING, not to a row. `set()` throws when `deviceId` is missing rather than mint a token every
|
|
44
|
-
* later request answers 401.
|
|
16
|
+
* `GET /api/users/me` still answers 200.
|
|
17
|
+
*
|
|
18
|
+
* **Server code that creates accounts — a seed, a migration, a content author, a service
|
|
19
|
+
* account — uses `provisionUser`, never hand-written rows.** `provisionUser({ name, tableName },
|
|
20
|
+
* { props, password? })` validates the props with the resource schema (once, before the identity
|
|
21
|
+
* lookup, so a schema `transform` decides the stored username/email), writes the `users` row and
|
|
22
|
+
* its same-id `usersSecrets` row through the bonded providers — in one transaction where the
|
|
23
|
+
* database supports one — and returns `{ status: 'created' | 'completed' | 'existing', id, props,
|
|
24
|
+
* hasPassword }` with NO device, JWT or session. Omit `password` for a content-only author that
|
|
25
|
+
* cannot log in (no `passwordHash` is written). It is idempotent by `username`/`email` and never
|
|
26
|
+
* changes an existing account's credentials. Where the bond has no transactions (Cloudflare D1) a
|
|
27
|
+
* credential-row failure leaves the user row and answers `{ status: 'partial', id, recovery }`;
|
|
28
|
+
* finish it with `provisionUser(..., { props, completeId: id })` — a retry without `completeId`
|
|
29
|
+
* answers `existing` and repairs nothing. **TRUSTED SERVER CALLERS ONLY:** it takes props
|
|
30
|
+
* directly, so a public endpoint built on it is a mass-assignment hole.
|
|
31
|
+
*
|
|
32
|
+
* For the HTTP path use `handlers.create` (signup), `handlers.logIn`, `handlers.updatePassword`,
|
|
33
|
+
* `handlers.forgotPassword` / `handlers.resetPassword`. To touch one secret yourself use the
|
|
34
|
+
* datastore on the same key — `findById('usersSecrets', userId)` (or `${tableName}Secrets` for a
|
|
35
|
+
* custom `tableName`) — never SQL.
|
|
45
36
|
*
|
|
46
37
|
* **RealWorld-style token API (`Authorization: Token <jwt>`)? CONFIGURATION on this same auth
|
|
47
38
|
* path — never a credentials table of your own** (`usersSecrets.passwordHash` is the password
|
|
@@ -106,6 +97,27 @@
|
|
|
106
97
|
* `TypeError: schema.safeParse is not a function`, and no row is written. Omit `schema`
|
|
107
98
|
* instead of stubbing it.
|
|
108
99
|
*
|
|
100
|
+
* **Issuing a session yourself (the sequence is device, then session — two calls, and the first
|
|
101
|
+
* RETURNS the id):** the middleware accepts both `Authorization: Bearer <jwt>` and
|
|
102
|
+
* `Authorization: Token <jwt>` (RealWorld's scheme), but only for a token whose `deviceId` names
|
|
103
|
+
* an ACTIVE row in `devices` — a JWT you `sign()` with an invented `deviceId` is answered 401 on
|
|
104
|
+
* every route.
|
|
105
|
+
*
|
|
106
|
+
* ```typescript
|
|
107
|
+
* import { get } from '@molecule/api-bond'
|
|
108
|
+
* import { authorization } from '@molecule/api-resource-user'
|
|
109
|
+
* import type { DeviceService } from '@molecule/api-resource-device'
|
|
110
|
+
*
|
|
111
|
+
* const deviceId = await get<DeviceService>('device')?.createOrUpdate(userId, deviceName)
|
|
112
|
+
* const token = authorization.set(req, res, { userId, deviceId })
|
|
113
|
+
* ```
|
|
114
|
+
*
|
|
115
|
+
* `get<DeviceService>('device')` is the typed accessor (never hand-type a
|
|
116
|
+
* `get<{ createOrUpdate(): … }>('device')` generic: a wrong one still compiles and leaves the
|
|
117
|
+
* field you invented — `device.id` — `undefined`), and `createOrUpdate` RESOLVES TO THE DEVICE ID
|
|
118
|
+
* STRING, not to a row. `set()` throws when `deviceId` is missing rather than mint a token every
|
|
119
|
+
* later request answers 401.
|
|
120
|
+
*
|
|
109
121
|
* **Exact responses of the sign-up and sign-in handlers** (the body shapes the routes send;
|
|
110
122
|
* a handler returns `{ statusCode, body }` and the mount applies it):
|
|
111
123
|
* - `create` (`POST /api/users`, flat body): 201 `{ props, accessToken, user }` (`user` equals
|
|
@@ -285,6 +297,7 @@ export * as authorizers from './authorizers/index.js';
|
|
|
285
297
|
export * from './browser-guard.js';
|
|
286
298
|
export * as handlers from './handlers/index.js';
|
|
287
299
|
export * from './i18n.js';
|
|
300
|
+
export * from './provisioning.js';
|
|
288
301
|
export * from './requestHandlerMap.js';
|
|
289
302
|
export * from './resource.js';
|
|
290
303
|
export * from './routes.js';
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqSG;AAEH,OAAO,KAAK,aAAa,MAAM,oBAAoB,CAAA;AACnD,OAAO,KAAK,WAAW,MAAM,wBAAwB,CAAA;AACrD,cAAc,oBAAoB,CAAA;AAClC,OAAO,KAAK,QAAQ,MAAM,qBAAqB,CAAA;AAC/C,cAAc,WAAW,CAAA;AACzB,cAAc,mBAAmB,CAAA;AACjC,cAAc,wBAAwB,CAAA;AACtC,cAAc,eAAe,CAAA;AAC7B,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAC3B,cAAc,cAAc,CAAA;AAC5B,OAAO,KAAK,KAAK,MAAM,YAAY,CAAA;AACnC,OAAO,KAAK,SAAS,MAAM,sBAAsB,CAAA"}
|