@molecule/api-resource-user 1.4.13 → 1.4.14
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 +1365 -475
- package/package.json +1 -1
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-16T00:27:43.818Z
|
|
7
7
|
-->
|
|
8
8
|
|
|
9
9
|
# @molecule/api-resource-user
|
|
@@ -81,12 +81,20 @@ interface UserRequestHandlerMap {
|
|
|
81
81
|
|
|
82
82
|
### Types
|
|
83
83
|
|
|
84
|
-
#### `
|
|
84
|
+
#### `Props`
|
|
85
85
|
|
|
86
|
-
|
|
86
|
+
User props type inferred from schema.
|
|
87
87
|
|
|
88
88
|
```typescript
|
|
89
|
-
type
|
|
89
|
+
type Props = z.infer<typeof propsSchema>
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
#### `SecretProps`
|
|
93
|
+
|
|
94
|
+
Secret Props type.
|
|
95
|
+
|
|
96
|
+
```typescript
|
|
97
|
+
type SecretProps = z.infer<typeof secretPropsSchema>
|
|
90
98
|
```
|
|
91
99
|
|
|
92
100
|
#### `CreateProps`
|
|
@@ -105,28 +113,20 @@ Create Secret Props type.
|
|
|
105
113
|
type CreateSecretProps = z.infer<typeof createSecretPropsSchema>
|
|
106
114
|
```
|
|
107
115
|
|
|
108
|
-
#### `
|
|
109
|
-
|
|
110
|
-
User props type inferred from schema.
|
|
111
|
-
|
|
112
|
-
```typescript
|
|
113
|
-
type Props = z.infer<typeof propsSchema>
|
|
114
|
-
```
|
|
115
|
-
|
|
116
|
-
#### `SecretProps`
|
|
116
|
+
#### `CreateOAuthProps`
|
|
117
117
|
|
|
118
|
-
|
|
118
|
+
Create O Auth Props type.
|
|
119
119
|
|
|
120
120
|
```typescript
|
|
121
|
-
type
|
|
121
|
+
type CreateOAuthProps = z.infer<typeof createOAuthPropsSchema>
|
|
122
122
|
```
|
|
123
123
|
|
|
124
|
-
#### `
|
|
124
|
+
#### `UpdateProps`
|
|
125
125
|
|
|
126
|
-
|
|
126
|
+
Update Props type.
|
|
127
127
|
|
|
128
128
|
```typescript
|
|
129
|
-
type
|
|
129
|
+
type UpdateProps = z.infer<typeof updatePropsSchema>
|
|
130
130
|
```
|
|
131
131
|
|
|
132
132
|
#### `UpdatePasswordSecretProps`
|
|
@@ -145,14 +145,6 @@ Update Plan Props type.
|
|
|
145
145
|
type UpdatePlanProps = z.infer<typeof updatePlanPropsSchema>
|
|
146
146
|
```
|
|
147
147
|
|
|
148
|
-
#### `UpdateProps`
|
|
149
|
-
|
|
150
|
-
Update Props type.
|
|
151
|
-
|
|
152
|
-
```typescript
|
|
153
|
-
type UpdateProps = z.infer<typeof updatePropsSchema>
|
|
154
|
-
```
|
|
155
|
-
|
|
156
148
|
#### `VerifyTwoFactorProps`
|
|
157
149
|
|
|
158
150
|
Verify Two Factor Props type.
|
|
@@ -169,6 +161,14 @@ Verify Two Factor Secret Props type.
|
|
|
169
161
|
type VerifyTwoFactorSecretProps = z.infer<typeof verifyTwoFactorSecretPropsSchema>
|
|
170
162
|
```
|
|
171
163
|
|
|
164
|
+
#### `Session`
|
|
165
|
+
|
|
166
|
+
User session data (userId, email, role, permissions, metadata) inferred from sessionSchema.
|
|
167
|
+
|
|
168
|
+
```typescript
|
|
169
|
+
type Session = z.infer<typeof sessionSchema>
|
|
170
|
+
```
|
|
171
|
+
|
|
172
172
|
### Functions
|
|
173
173
|
|
|
174
174
|
#### `createRequestHandlerMap(createRequestHandler)`
|
|
@@ -262,59 +262,46 @@ function createSchema(options?: { oauthServers?: OAuthServers; planKeys?: PlanKe
|
|
|
262
262
|
|
|
263
263
|
### Constants
|
|
264
264
|
|
|
265
|
-
#### `
|
|
265
|
+
#### `i18nRegistered`
|
|
266
266
|
|
|
267
|
-
|
|
267
|
+
The i18n registered.
|
|
268
268
|
|
|
269
269
|
```typescript
|
|
270
|
-
const
|
|
271
|
-
{
|
|
272
|
-
username: z.ZodOptional<z.ZodString>
|
|
273
|
-
name: z.ZodOptional<z.ZodString>
|
|
274
|
-
email: z.ZodOptional<z.ZodNullable<z.ZodString>>
|
|
275
|
-
emailVerified: z.ZodOptional<z.ZodBoolean>
|
|
276
|
-
avatar: z.ZodOptional<z.ZodNullable<z.ZodString>>
|
|
277
|
-
bio: z.ZodOptional<z.ZodNullable<z.ZodString>>
|
|
278
|
-
oauthServer: z.ZodOptional<z.ZodString> | z.ZodOptional<z.ZodEnum<{}>>
|
|
279
|
-
oauthId: z.ZodOptional<z.ZodString>
|
|
280
|
-
oauthData: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>
|
|
281
|
-
},
|
|
282
|
-
z.core.$strip
|
|
283
|
-
>
|
|
270
|
+
const i18nRegistered: true
|
|
284
271
|
```
|
|
285
272
|
|
|
286
|
-
#### `
|
|
273
|
+
#### `resource`
|
|
287
274
|
|
|
288
|
-
|
|
275
|
+
Default user resource definition.
|
|
289
276
|
|
|
290
277
|
```typescript
|
|
291
|
-
const
|
|
292
|
-
{
|
|
293
|
-
username: z.ZodOptional<z.ZodString>
|
|
294
|
-
name: z.ZodOptional<z.ZodString>
|
|
295
|
-
email: z.ZodOptional<z.ZodNullable<z.ZodString>>
|
|
296
|
-
},
|
|
297
|
-
z.core.$strip
|
|
298
|
-
>
|
|
278
|
+
const resource: types.Resource<unknown>
|
|
299
279
|
```
|
|
300
280
|
|
|
301
|
-
#### `
|
|
281
|
+
#### `routes`
|
|
302
282
|
|
|
303
|
-
|
|
283
|
+
Route definitions for the User resource.
|
|
284
|
+
Routes marked optional require additional packages to be installed.
|
|
285
|
+
|
|
286
|
+
Declarative route definitions used by the injection engine.
|
|
304
287
|
|
|
305
288
|
```typescript
|
|
306
|
-
const
|
|
307
|
-
{
|
|
308
|
-
|
|
309
|
-
|
|
289
|
+
const routes: (
|
|
290
|
+
| { method: 'post'; path: string; middlewares: string[]; handler: string; optional?: undefined }
|
|
291
|
+
| { method: 'get'; path: string; middlewares: string[]; handler: string; optional: string }
|
|
292
|
+
| { method: 'post'; path: string; middlewares: string[]; handler: string; optional: string }
|
|
293
|
+
| { method: 'get'; path: string; middlewares: string[]; handler: string; optional?: undefined }
|
|
294
|
+
| { method: 'patch'; path: string; middlewares: string[]; handler: string; optional?: undefined }
|
|
295
|
+
| { method: 'delete'; path: string; middlewares: string[]; handler: string; optional?: undefined }
|
|
296
|
+
)[]
|
|
310
297
|
```
|
|
311
298
|
|
|
312
|
-
#### `
|
|
299
|
+
#### `MAX_BIO_LENGTH`
|
|
313
300
|
|
|
314
|
-
|
|
301
|
+
Maximum length (in characters) of a user's `bio`.
|
|
315
302
|
|
|
316
303
|
```typescript
|
|
317
|
-
const
|
|
304
|
+
const MAX_BIO_LENGTH: 1000
|
|
318
305
|
```
|
|
319
306
|
|
|
320
307
|
#### `MAX_AVATAR_LENGTH`
|
|
@@ -327,14 +314,6 @@ Larger avatars must be hosted elsewhere and referenced by URL.
|
|
|
327
314
|
const MAX_AVATAR_LENGTH: number
|
|
328
315
|
```
|
|
329
316
|
|
|
330
|
-
#### `MAX_BIO_LENGTH`
|
|
331
|
-
|
|
332
|
-
Maximum length (in characters) of a user's `bio`.
|
|
333
|
-
|
|
334
|
-
```typescript
|
|
335
|
-
const MAX_BIO_LENGTH: 1000
|
|
336
|
-
```
|
|
337
|
-
|
|
338
317
|
#### `propsSchema`
|
|
339
318
|
|
|
340
319
|
Default schema for user props.
|
|
@@ -363,40 +342,6 @@ const propsSchema: z.ZodObject<
|
|
|
363
342
|
>
|
|
364
343
|
```
|
|
365
344
|
|
|
366
|
-
#### `resource`
|
|
367
|
-
|
|
368
|
-
Default user resource definition.
|
|
369
|
-
|
|
370
|
-
```typescript
|
|
371
|
-
const resource: types.Resource<unknown>
|
|
372
|
-
```
|
|
373
|
-
|
|
374
|
-
#### `resourceUserSecretDefinitions`
|
|
375
|
-
|
|
376
|
-
Secret definitions required by the user resource.
|
|
377
|
-
|
|
378
|
-
```typescript
|
|
379
|
-
const resourceUserSecretDefinitions: SecretDefinition[]
|
|
380
|
-
```
|
|
381
|
-
|
|
382
|
-
#### `routes`
|
|
383
|
-
|
|
384
|
-
Route definitions for the User resource.
|
|
385
|
-
Routes marked optional require additional packages to be installed.
|
|
386
|
-
|
|
387
|
-
Declarative route definitions used by the injection engine.
|
|
388
|
-
|
|
389
|
-
```typescript
|
|
390
|
-
const routes: (
|
|
391
|
-
| { method: 'post'; path: string; middlewares: string[]; handler: string; optional?: undefined }
|
|
392
|
-
| { method: 'get'; path: string; middlewares: string[]; handler: string; optional: string }
|
|
393
|
-
| { method: 'post'; path: string; middlewares: string[]; handler: string; optional: string }
|
|
394
|
-
| { method: 'get'; path: string; middlewares: string[]; handler: string; optional?: undefined }
|
|
395
|
-
| { method: 'patch'; path: string; middlewares: string[]; handler: string; optional?: undefined }
|
|
396
|
-
| { method: 'delete'; path: string; middlewares: string[]; handler: string; optional?: undefined }
|
|
397
|
-
)[]
|
|
398
|
-
```
|
|
399
|
-
|
|
400
345
|
#### `secretPropsSchema`
|
|
401
346
|
|
|
402
347
|
Secret properties stored in a separate table.
|
|
@@ -416,44 +361,48 @@ const secretPropsSchema: z.ZodObject<
|
|
|
416
361
|
>
|
|
417
362
|
```
|
|
418
363
|
|
|
419
|
-
#### `
|
|
364
|
+
#### `createPropsSchema`
|
|
420
365
|
|
|
421
|
-
|
|
366
|
+
Schema for creating a user via password.
|
|
422
367
|
|
|
423
368
|
```typescript
|
|
424
|
-
const
|
|
369
|
+
const createPropsSchema: z.ZodObject<
|
|
425
370
|
{
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
oauthServer: z.ZodOptional<z.ZodString>
|
|
430
|
-
oauthId: z.ZodOptional<z.ZodString>
|
|
371
|
+
username: z.ZodOptional<z.ZodString>
|
|
372
|
+
name: z.ZodOptional<z.ZodString>
|
|
373
|
+
email: z.ZodOptional<z.ZodNullable<z.ZodString>>
|
|
431
374
|
},
|
|
432
375
|
z.core.$strip
|
|
433
376
|
>
|
|
434
377
|
```
|
|
435
378
|
|
|
436
|
-
#### `
|
|
379
|
+
#### `createSecretPropsSchema`
|
|
437
380
|
|
|
438
|
-
Schema for
|
|
381
|
+
Schema for creating secret props (password hash only).
|
|
439
382
|
|
|
440
383
|
```typescript
|
|
441
|
-
const
|
|
442
|
-
{ passwordHash: z.ZodOptional<z.
|
|
384
|
+
const createSecretPropsSchema: z.ZodObject<
|
|
385
|
+
{ passwordHash: z.ZodOptional<z.ZodString> },
|
|
443
386
|
z.core.$strip
|
|
444
387
|
>
|
|
445
388
|
```
|
|
446
389
|
|
|
447
|
-
#### `
|
|
390
|
+
#### `createOAuthPropsSchema`
|
|
448
391
|
|
|
449
|
-
Schema for
|
|
392
|
+
Schema for creating a user via OAuth.
|
|
450
393
|
|
|
451
394
|
```typescript
|
|
452
|
-
const
|
|
395
|
+
const createOAuthPropsSchema: z.ZodObject<
|
|
453
396
|
{
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
397
|
+
oauthServer: z.ZodOptional<z.ZodString> | z.ZodOptional<z.ZodEnum<{}>>
|
|
398
|
+
oauthId: z.ZodOptional<z.ZodString>
|
|
399
|
+
username: z.ZodOptional<z.ZodString>
|
|
400
|
+
name: z.ZodOptional<z.ZodString>
|
|
401
|
+
email: z.ZodOptional<z.ZodNullable<z.ZodString>>
|
|
402
|
+
emailVerified: z.ZodOptional<z.ZodBoolean>
|
|
403
|
+
avatar: z.ZodOptional<z.ZodNullable<z.ZodString>>
|
|
404
|
+
bio: z.ZodOptional<z.ZodNullable<z.ZodString>>
|
|
405
|
+
oauthData: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>
|
|
457
406
|
},
|
|
458
407
|
z.core.$strip
|
|
459
408
|
>
|
|
@@ -476,6 +425,32 @@ const updatePropsSchema: z.ZodObject<
|
|
|
476
425
|
>
|
|
477
426
|
```
|
|
478
427
|
|
|
428
|
+
#### `updatePasswordSecretPropsSchema`
|
|
429
|
+
|
|
430
|
+
Schema for updating password secret props (partial password hash).
|
|
431
|
+
|
|
432
|
+
```typescript
|
|
433
|
+
const updatePasswordSecretPropsSchema: z.ZodObject<
|
|
434
|
+
{ passwordHash: z.ZodOptional<z.ZodOptional<z.ZodString>> },
|
|
435
|
+
z.core.$strip
|
|
436
|
+
>
|
|
437
|
+
```
|
|
438
|
+
|
|
439
|
+
#### `updatePlanPropsSchema`
|
|
440
|
+
|
|
441
|
+
Schema for updating a user's plan (partial planKey, planExpiresAt, planAutoRenews).
|
|
442
|
+
|
|
443
|
+
```typescript
|
|
444
|
+
const updatePlanPropsSchema: z.ZodObject<
|
|
445
|
+
{
|
|
446
|
+
planKey: z.ZodOptional<z.ZodOptional<z.ZodString> | z.ZodOptional<z.ZodEnum<{}>>>
|
|
447
|
+
planExpiresAt: z.ZodOptional<z.ZodOptional<z.ZodString>>
|
|
448
|
+
planAutoRenews: z.ZodOptional<z.ZodOptional<z.ZodBoolean>>
|
|
449
|
+
},
|
|
450
|
+
z.core.$strip
|
|
451
|
+
>
|
|
452
|
+
```
|
|
453
|
+
|
|
479
454
|
#### `verifyTwoFactorPropsSchema`
|
|
480
455
|
|
|
481
456
|
Schema for verifying two-factor authentication.
|
|
@@ -501,377 +476,1292 @@ const verifyTwoFactorSecretPropsSchema: z.ZodObject<
|
|
|
501
476
|
>
|
|
502
477
|
```
|
|
503
478
|
|
|
479
|
+
#### `sessionSchema`
|
|
480
|
+
|
|
481
|
+
Zod schema for JWT session payloads (userId, deviceId, optional OAuth fields).
|
|
482
|
+
|
|
483
|
+
```typescript
|
|
484
|
+
const sessionSchema: z.ZodObject<
|
|
485
|
+
{
|
|
486
|
+
id: z.ZodOptional<z.ZodString>
|
|
487
|
+
userId: z.ZodString
|
|
488
|
+
deviceId: z.ZodString
|
|
489
|
+
oauthServer: z.ZodOptional<z.ZodString>
|
|
490
|
+
oauthId: z.ZodOptional<z.ZodString>
|
|
491
|
+
},
|
|
492
|
+
z.core.$strip
|
|
493
|
+
>
|
|
494
|
+
```
|
|
495
|
+
|
|
496
|
+
#### `resourceUserSecretDefinitions`
|
|
497
|
+
|
|
498
|
+
Secret definitions required by the user resource.
|
|
499
|
+
|
|
500
|
+
```typescript
|
|
501
|
+
const resourceUserSecretDefinitions: SecretDefinition[]
|
|
502
|
+
```
|
|
503
|
+
|
|
504
504
|
### Namespaces
|
|
505
505
|
|
|
506
|
+
#### `z`
|
|
507
|
+
|
|
508
|
+
Re-exports the `zod` module — `z.<name>` is exactly `zod.<name>`, documented by that package (installed at `node_modules/zod`).
|
|
509
|
+
|
|
506
510
|
#### `authorization`
|
|
507
511
|
|
|
508
|
-
|
|
512
|
+
Re-exported as a namespace — reach every member below through it (e.g. `authorization.<name>`).
|
|
509
513
|
|
|
510
|
-
|
|
511
|
-
- `authorization.getAuthCookieOptions` — function: Base cookie attributes shared by EVERY auth cookie this resource sets and
|
|
512
|
-
- `authorization.invalidateDeviceExistsCache` — function: Evict a single device's positive entry from the device-exists cache so the
|
|
513
|
-
- `authorization.invalidateAllDeviceExistsCache` — function: Evict ALL positive entries from the device-exists cache.
|
|
514
|
-
- `authorization.set` — function: Set authorization headers and cookie for a session.
|
|
515
|
-
- `authorization.VerifyMiddlewareOptions` — interface: Options for {@link verifyMiddleware}.
|
|
516
|
-
- `authorization.verifyMiddleware` — function: Middleware that verifies the JWT token from the `Authorization` header and sets `res.locals.session`.
|
|
514
|
+
##### `authorization.getAuthCookieName(base)`
|
|
517
515
|
|
|
518
|
-
|
|
516
|
+
Resolve the actual cookie name for an auth cookie.
|
|
519
517
|
|
|
520
|
-
|
|
518
|
+
In production the auth-bearing cookies get the `__Host-` prefix. Browsers
|
|
519
|
+
reject any `__Host-`-prefixed cookie that carries a `Domain` attribute (and
|
|
520
|
+
require `Secure` + `Path=/`), so a sibling subdomain (e.g. a tenant preview
|
|
521
|
+
at `<token>.preview.molecule.dev`) can no longer create a colliding
|
|
522
|
+
`.molecule.dev`-scoped duplicate that shadows the platform's real session
|
|
523
|
+
cookie — closing the cross-tenant cookie-tossing / session-fixation hole.
|
|
521
524
|
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
- `authorizers.RateLimitAuthOptions` — interface: Configuration for {@link rateLimit}.
|
|
525
|
-
- `authorizers.rateLimit` — function: Creates an authorizer middleware that brute-force-protects an auth endpoint.
|
|
526
|
-
- `authorizers.loginAccountKey` — function: Account-identifier extractor for the login endpoint: the submitted username
|
|
527
|
-
- `authorizers.emailAccountKey` — function: Account-identifier extractor for the forgot-password endpoint: the submitted
|
|
528
|
-
- `authorizers.paramIdAccountKey` — function: Account-identifier extractor for the verify-two-factor endpoint: the target
|
|
529
|
-
- `authorizers.requireWebhookAuthenticity` — function: Middleware guarding the public `POST /users/payment-notification/:provider`
|
|
525
|
+
In dev/test the cookies are not `Secure` (plain HTTP origins), so a
|
|
526
|
+
`__Host-` cookie would be rejected by the browser; use the plain name there.
|
|
530
527
|
|
|
531
|
-
|
|
528
|
+
Exported so consumers (e.g. molecule-dev's guest-session / OAuth-state code)
|
|
529
|
+
set and read the exact same name the platform expects.
|
|
532
530
|
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
- `handlers.handlePaymentNotification` — function: Generic payment notification handler that works with any bonded PaymentProvider. Reads the
|
|
537
|
-
- `handlers.verifyPayment` — function: Generic payment verification handler that works with any bonded PaymentProvider. Reads the
|
|
538
|
-
- `handlers.CreateRequest` — interface: Request body for user creation, including password and optional device name.
|
|
539
|
-
- `handlers.create` — function: Creates a user with username and password. Validates username uniqueness and email format,
|
|
540
|
-
- `handlers.del` — function: Deletes a user and their associated data. Deletes the user row FIRST, then
|
|
541
|
-
- `handlers.ForgotPasswordRequest` — interface: Request body for password reset initiation.
|
|
542
|
-
- `handlers.forgotPassword` — function: Generates a UUID password reset token, stores it in the secrets table, and sends a reset
|
|
543
|
-
- `handlers.LogInRequest` — interface: Request body for user login, supporting password, reset token, and 2FA flows.
|
|
544
|
-
- `handlers.logIn` — function: Logs in a user by username or email. Supports password authentication, password reset token
|
|
545
|
-
- `handlers.LogInOAuthRequest` — interface: Request body for OAuth login, including the OAuth server name, authorization code, and PKCE verifier.
|
|
546
|
-
- `handlers.logInOAuth` — function: Logs in or creates a user via OAuth. Verifies the authorization code with the bonded OAuth
|
|
547
|
-
- `handlers.logout` — function: Logs the current user out. Revokes the session device server-side (so the JWT
|
|
548
|
-
- `handlers.oauthAuthorize` — function: OAuth initiation — `GET /users/oauth/:provider`.
|
|
549
|
-
- `handlers.read` — function: Reads a user by ID from the database. Attaches plan info (with expiration/renewal status) via the
|
|
550
|
-
- `handlers.readSelf` — function: Reads the AUTHENTICATED user from the session — no `:id` param. Backs
|
|
551
|
-
- `handlers.RESET_TOKEN_TTL_MS` — const: How long a password-reset token stays valid, in milliseconds. Shared with
|
|
552
|
-
- `handlers.ResetPasswordRequest` — interface: Request body for confirming a password reset using a token.
|
|
553
|
-
- `handlers.resetPassword` — function: Confirms a password reset by validating the one-time token previously generated by
|
|
554
|
-
- `handlers.update` — function: Updates a user's profile fields (username, name, email). Validates username format
|
|
555
|
-
- `handlers.UpdatePasswordRequest` — interface: Request body for password update, with current password verification and new password.
|
|
556
|
-
- `handlers.updatePassword` — function: Updates a user's password. If the user already has a password hash, the current password
|
|
557
|
-
- `handlers.UpdatePlanRequest` — interface: Request body for plan update, containing the target plan key.
|
|
558
|
-
- `handlers.updatePlan` — function: Updates a user's subscription plan. Uses the bonded PlanService to look up plan metadata and
|
|
559
|
-
- `handlers.VerifyTwoFactorRequest` — interface: Request body for two-factor authentication operations (setup, enable, or disable).
|
|
560
|
-
- `handlers.verifyTwoFactor` — function: Handles two-factor authentication lifecycle via `@molecule/api-two-factor`:
|
|
531
|
+
```typescript
|
|
532
|
+
function getAuthCookieName(base: string): string
|
|
533
|
+
```
|
|
561
534
|
|
|
562
|
-
|
|
535
|
+
- `base` — The base cookie name (e.g. `'token'`, `'sessionId'`).
|
|
563
536
|
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
- `types.CreateOAuthProps` — type: Create O Auth Props type.
|
|
567
|
-
- `types.CreateProps` — type: Create Props type.
|
|
568
|
-
- `types.CreateSecretProps` — type: Create Secret Props type.
|
|
569
|
-
- `types.Props` — type: User props type inferred from schema.
|
|
570
|
-
- `types.SecretProps` — type: Secret Props type.
|
|
571
|
-
- `types.Session` — type: User session data (userId, email, role, permissions, metadata) inferred from sessionSchema.
|
|
572
|
-
- `types.UpdatePasswordSecretProps` — type: Update Password Secret Props type.
|
|
573
|
-
- `types.UpdatePlanProps` — type: Update Plan Props type.
|
|
574
|
-
- `types.UpdateProps` — type: Update Props type.
|
|
575
|
-
- `types.VerifyTwoFactorProps` — type: Verify Two Factor Props type.
|
|
576
|
-
- `types.VerifyTwoFactorSecretProps` — type: Verify Two Factor Secret Props type.
|
|
577
|
-
- `types.Resource` — type: An object describing the `user` resource.
|
|
537
|
+
**Returns:** The environment-appropriate cookie name.
|
|
578
538
|
|
|
579
|
-
|
|
539
|
+
##### `authorization.getAuthCookieOptions()`
|
|
580
540
|
|
|
581
|
-
|
|
541
|
+
Base cookie attributes shared by EVERY auth cookie this resource sets and
|
|
542
|
+
clears — the single source of truth so a SET and its matching CLEAR always
|
|
543
|
+
agree (a mismatched attribute leaves an undeletable cookie) and every auth
|
|
544
|
+
cookie gets identical behaviour.
|
|
582
545
|
|
|
583
|
-
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
-
|
|
587
|
-
|
|
588
|
-
|
|
546
|
+
- **Cross-site preview** (see {@link isCrossSitePreview}) → `{ secure: true,
|
|
547
|
+
sameSite: 'none', partitioned: true, path: '/' }`. The molecule.dev live
|
|
548
|
+
preview renders the app in a CROSS-SITE iframe; browsers evaluate SameSite
|
|
549
|
+
against the TOP-LEVEL site, so a `Lax`/`Strict` cookie is neither stored nor
|
|
550
|
+
sent on ANY preview request — the httpOnly session cookie vanishes on every
|
|
551
|
+
iframe reload and the user is logged out of their own app. `SameSite=None`
|
|
552
|
+
makes it cross-site-sendable (which REQUIRES `Secure` — permitted over http
|
|
553
|
+
on the potentially-trustworthy `localhost` / `127.0.0.1` origins), and
|
|
554
|
+
`Partitioned` (CHIPS) keeps it working when third-party cookies are blocked
|
|
555
|
+
(Brave/Chrome), which `None` alone does not. Applies whether the platform is
|
|
556
|
+
dev or prod, because the preview app is cross-site either way.
|
|
557
|
+
- **Production, top-level** → `{ secure: true, sameSite: 'lax', path: '/' }`.
|
|
558
|
+
The correct, CSRF-hardened default; pairs with the `__Host-` prefix (see
|
|
559
|
+
{@link getAuthCookieName}). NEVER weaken it.
|
|
560
|
+
- **Development, top-level** → `{ secure: false, sameSite: 'lax', path: '/' }`.
|
|
561
|
+
The original dev posture (plain http, not `Secure`) — used by the molecule.dev
|
|
589
562
|
|
|
590
|
-
|
|
563
|
+
_(description truncated — read the full text in `node_modules/@molecule/api-resource-user/dist/authorization.d.ts`.)_
|
|
564
|
+
|
|
565
|
+
```typescript
|
|
566
|
+
function getAuthCookieOptions(): {
|
|
567
|
+
secure: boolean
|
|
568
|
+
sameSite: 'lax' | 'none'
|
|
569
|
+
partitioned?: boolean
|
|
570
|
+
path: string
|
|
571
|
+
}
|
|
572
|
+
```
|
|
573
|
+
|
|
574
|
+
**Returns:** The environment-appropriate base cookie options.
|
|
575
|
+
|
|
576
|
+
##### `authorization.invalidateDeviceExistsCache(deviceId)`
|
|
577
|
+
|
|
578
|
+
Evict a single device's positive entry from the device-exists cache so the
|
|
579
|
+
next authorization check for that device re-consults the database.
|
|
580
|
+
|
|
581
|
+
Call this from any in-process revocation path that deletes a specific device
|
|
582
|
+
row (e.g. a logout handler that knows the `deviceId`) so the revocation is
|
|
583
|
+
effective immediately on this instance rather than after the cache TTL.
|
|
584
|
+
|
|
585
|
+
```typescript
|
|
586
|
+
function invalidateDeviceExistsCache(deviceId: string): void
|
|
587
|
+
```
|
|
588
|
+
|
|
589
|
+
- `deviceId` — The device id whose cached "exists" result should be dropped.
|
|
590
|
+
|
|
591
|
+
##### `authorization.invalidateAllDeviceExistsCache()`
|
|
592
|
+
|
|
593
|
+
Evict ALL positive entries from the device-exists cache.
|
|
594
|
+
|
|
595
|
+
Call this from in-process bulk revocation paths that delete every device for
|
|
596
|
+
a user (password change/reset, account deletion, OAuth account-claim) where
|
|
597
|
+
the affected device ids are not enumerated — the cache is keyed by deviceId,
|
|
598
|
+
not userId, so a full clear is the correct way to guarantee none of the
|
|
599
|
+
just-deleted devices keep authenticating on this instance. The normal-path
|
|
600
|
+
per-request DB-cost reduction is preserved: the cache simply repopulates on
|
|
601
|
+
subsequent requests; only these rare revocation events pay a brief re-check.
|
|
602
|
+
|
|
603
|
+
```typescript
|
|
604
|
+
function invalidateAllDeviceExistsCache(): void
|
|
605
|
+
```
|
|
606
|
+
|
|
607
|
+
##### `authorization.set(_req, res, session)`
|
|
608
|
+
|
|
609
|
+
Set authorization headers and cookie for a session.
|
|
610
|
+
|
|
611
|
+
```typescript
|
|
612
|
+
function set(
|
|
613
|
+
_req: MoleculeRequest,
|
|
614
|
+
res: MoleculeResponse,
|
|
615
|
+
session: {
|
|
616
|
+
userId: string
|
|
617
|
+
deviceId: string
|
|
618
|
+
id?: string | undefined
|
|
619
|
+
oauthServer?: string | undefined
|
|
620
|
+
oauthId?: string | undefined
|
|
621
|
+
},
|
|
622
|
+
): string | null
|
|
623
|
+
```
|
|
624
|
+
|
|
625
|
+
- `_req` — The request object (unused; reserved for handler signature parity).
|
|
626
|
+
- `res` — The response object.
|
|
627
|
+
- `session` — The session.
|
|
628
|
+
|
|
629
|
+
**Returns:** The signed JWT token string, or null if signing failed.
|
|
630
|
+
|
|
631
|
+
##### `authorization.VerifyMiddlewareOptions`
|
|
632
|
+
|
|
633
|
+
Options for {@link verifyMiddleware}.
|
|
634
|
+
|
|
635
|
+
```typescript
|
|
636
|
+
interface VerifyMiddlewareOptions {
|
|
637
|
+
/**
|
|
638
|
+
* Decide whether an aging token may be transparently re-issued for this
|
|
639
|
+
* session. The auto-refresh calls `set()`, which rewrites the session cookies
|
|
640
|
+
* AND the non-httpOnly `mol_auth` presence hint — the signal the auth client
|
|
641
|
+
* uses to hydrate a signed-in user on the next page load. Return `false` to
|
|
642
|
+
* skip the refresh for sessions that must never mint that hint (e.g. an
|
|
643
|
+
* anonymous/guest session whose httpOnly cookie authenticates API calls but
|
|
644
|
+
* whose user must not appear signed-in client-side). A thrown error is
|
|
645
|
+
* treated as `false` (refresh is best-effort and fails safe: no hint).
|
|
646
|
+
* Defaults to refreshing every session.
|
|
647
|
+
* @param session - The verified session about to be refreshed.
|
|
648
|
+
* @param req - The current request.
|
|
649
|
+
* @returns Whether the session may be re-issued.
|
|
650
|
+
*/
|
|
651
|
+
shouldRefreshSession?: (session: Session, req: MoleculeRequest) => boolean | Promise<boolean>
|
|
652
|
+
}
|
|
653
|
+
```
|
|
654
|
+
|
|
655
|
+
##### `authorization.verifyMiddleware(options)`
|
|
656
|
+
|
|
657
|
+
Middleware that verifies the JWT token from the `Authorization` header and sets `res.locals.session`.
|
|
658
|
+
Falls back to cookie-based session for browser clients. Auto-refreshes tokens older than `JWT_REFRESH_TIME`.
|
|
659
|
+
|
|
660
|
+
```typescript
|
|
661
|
+
function verifyMiddleware(options?: VerifyMiddlewareOptions): MoleculeRequestHandler
|
|
662
|
+
```
|
|
663
|
+
|
|
664
|
+
- `options` — {@link VerifyMiddlewareOptions} controlling the auto-refresh.
|
|
665
|
+
|
|
666
|
+
**Returns:** An Express-compatible middleware function.
|
|
667
|
+
|
|
668
|
+
#### `authorizers`
|
|
669
|
+
|
|
670
|
+
Re-exported as a namespace — reach every member below through it (e.g. `authorizers.<name>`).
|
|
671
|
+
|
|
672
|
+
##### `authorizers.auth()`
|
|
673
|
+
|
|
674
|
+
Middleware that checks if the request has an authenticated session (`res.locals.session.userId`).
|
|
675
|
+
Calls `next()` on success or `next('Unauthorized')` on failure.
|
|
676
|
+
|
|
677
|
+
```typescript
|
|
678
|
+
function auth(): MoleculeRequestHandler
|
|
679
|
+
```
|
|
680
|
+
|
|
681
|
+
**Returns:** An Express-compatible middleware function.
|
|
682
|
+
|
|
683
|
+
##### `authorizers.authSelf()`
|
|
684
|
+
|
|
685
|
+
Middleware that checks if the authenticated user's ID matches the `:id` route parameter.
|
|
686
|
+
Ensures users can only access their own resource. Calls `next()` on match or `next('Unauthorized')` otherwise.
|
|
687
|
+
|
|
688
|
+
As a convenience, `:id === 'me'` is treated as the current authenticated
|
|
689
|
+
user — `req.params.id` is rewritten to the session's userId so downstream
|
|
690
|
+
handlers receive the real id. This lets pages call `/api/users/me`
|
|
691
|
+
without needing the user's UUID in scope.
|
|
692
|
+
|
|
693
|
+
```typescript
|
|
694
|
+
function authSelf(): MoleculeRequestHandler
|
|
695
|
+
```
|
|
696
|
+
|
|
697
|
+
**Returns:** An Express-compatible middleware function.
|
|
698
|
+
|
|
699
|
+
##### `authorizers.RateLimitAuthOptions`
|
|
700
|
+
|
|
701
|
+
Configuration for {@link rateLimit}.
|
|
702
|
+
|
|
703
|
+
```typescript
|
|
704
|
+
interface RateLimitAuthOptions {
|
|
705
|
+
/**
|
|
706
|
+
* Key namespace so each protected surface gets an isolated bucket
|
|
707
|
+
* (e.g. `'login'`, `'forgot-password'`, `'2fa'`). Buckets never collide
|
|
708
|
+
* across scopes.
|
|
709
|
+
*/
|
|
710
|
+
scope: string
|
|
711
|
+
|
|
712
|
+
/** Sliding window in milliseconds (default: 15 minutes). */
|
|
713
|
+
windowMs?: number
|
|
714
|
+
|
|
715
|
+
/**
|
|
716
|
+
* Maximum attempts per window for the per-account bucket before a 429
|
|
717
|
+
* (default: 10). This is the tight brute-force gate; a legitimate user makes
|
|
718
|
+
* only a handful of attempts, so it never trips for them.
|
|
719
|
+
*/
|
|
720
|
+
max?: number
|
|
721
|
+
|
|
722
|
+
/**
|
|
723
|
+
* Maximum attempts per window for the per-IP bucket (default: 60). Kept
|
|
724
|
+
* higher than {@link RateLimitAuthOptions.max} so shared public IPs aren't
|
|
725
|
+
* locked out; primarily catches single-source flooding and password-spray
|
|
726
|
+
* across many accounts, and is the only guard when no account is resolvable
|
|
727
|
+
* (e.g. an OAuth login carrying no email).
|
|
728
|
+
*/
|
|
729
|
+
ipMax?: number
|
|
730
|
+
|
|
731
|
+
/**
|
|
732
|
+
* Optional extractors for an account identifier (submitted email/username,
|
|
733
|
+
* the target user id, etc.). When one resolves, the request is ALSO bucketed
|
|
734
|
+
* per account — so a distributed attack against a single account is throttled
|
|
735
|
+
* even when the source IP rotates, and the second factor is temp-locked after
|
|
736
|
+
* `max` consecutive misses regardless of IP. Each extractor is tried; every
|
|
737
|
+
* value that resolves contributes a bucket.
|
|
738
|
+
*/
|
|
739
|
+
accountFrom?: ReadonlyArray<(req: MoleculeRequest) => string | undefined>
|
|
740
|
+
}
|
|
741
|
+
```
|
|
742
|
+
|
|
743
|
+
##### `authorizers.rateLimit(options)`
|
|
744
|
+
|
|
745
|
+
Creates an authorizer middleware that brute-force-protects an auth endpoint.
|
|
746
|
+
|
|
747
|
+
Consumes a rate-limit token per request, keyed by client IP and — where
|
|
748
|
+
resolvable — by account identifier, returning HTTP 429 once the low ceiling
|
|
749
|
+
is exceeded. This closes the unthrottled-auth default that left passwords and
|
|
750
|
+
the 6-digit TOTP second factor brute-forceable on `mlcl`-generated apps.
|
|
751
|
+
|
|
752
|
+
Availability is preserved over absolute enforcement: if no rate-limit
|
|
753
|
+
provider is bonded (an app deliberately removed it) or the limiter itself
|
|
754
|
+
errors, the request is allowed through (with a logged warning/error) so a
|
|
755
|
+
limiter outage never locks out every legitimate login. Generated apps wire
|
|
756
|
+
the default in-memory provider via the registry's `alwaysInclude`/
|
|
757
|
+
`defaultProvider`, so the throttle is active out of the box.
|
|
758
|
+
|
|
759
|
+
```typescript
|
|
760
|
+
function rateLimit(options: RateLimitAuthOptions): MoleculeRequestHandler
|
|
761
|
+
```
|
|
762
|
+
|
|
763
|
+
- `options` — Scope, window, ceiling, and optional account-key extractors.
|
|
764
|
+
|
|
765
|
+
**Returns:** A `MoleculeRequestHandler` to place ahead of the protected handler.
|
|
766
|
+
|
|
767
|
+
##### `authorizers.loginAccountKey(req)`
|
|
768
|
+
|
|
769
|
+
Account-identifier extractor for the login endpoint: the submitted username
|
|
770
|
+
or email. Bucketing on this temp-locks a single targeted account after `max`
|
|
771
|
+
failed attempts even across rotating IPs (which also covers TOTP-via-login
|
|
772
|
+
brute force, since the login handler verifies the second factor).
|
|
773
|
+
|
|
774
|
+
```typescript
|
|
775
|
+
function loginAccountKey(req: MoleculeRequest): string | undefined
|
|
776
|
+
```
|
|
777
|
+
|
|
778
|
+
- `req` — The incoming request.
|
|
779
|
+
|
|
780
|
+
**Returns:** The submitted username or email (if present).
|
|
781
|
+
|
|
782
|
+
##### `authorizers.emailAccountKey(req)`
|
|
783
|
+
|
|
784
|
+
Account-identifier extractor for the forgot-password endpoint: the submitted
|
|
785
|
+
email.
|
|
786
|
+
|
|
787
|
+
```typescript
|
|
788
|
+
function emailAccountKey(req: MoleculeRequest): string | undefined
|
|
789
|
+
```
|
|
790
|
+
|
|
791
|
+
- `req` — The incoming request.
|
|
792
|
+
|
|
793
|
+
**Returns:** The submitted email (if present).
|
|
794
|
+
|
|
795
|
+
##### `authorizers.paramIdAccountKey(req)`
|
|
796
|
+
|
|
797
|
+
Account-identifier extractor for the verify-two-factor endpoint: the target
|
|
798
|
+
user id in the route params. Temp-locks the second factor per account after
|
|
799
|
+
`max` consecutive misses.
|
|
800
|
+
|
|
801
|
+
```typescript
|
|
802
|
+
function paramIdAccountKey(req: MoleculeRequest): string | undefined
|
|
803
|
+
```
|
|
804
|
+
|
|
805
|
+
- `req` — The incoming request.
|
|
806
|
+
|
|
807
|
+
**Returns:** The target user id (if present).
|
|
808
|
+
|
|
809
|
+
##### `authorizers.requireWebhookAuthenticity()`
|
|
810
|
+
|
|
811
|
+
Middleware guarding the public `POST /users/payment-notification/:provider`
|
|
812
|
+
endpoint against forged server-to-server (Apple/Google IAP) notifications.
|
|
813
|
+
|
|
814
|
+
Self-verifying webhook providers (e.g. Stripe) authenticate the payload by
|
|
815
|
+
cryptographic signature INSIDE the handler (`constructEvent`), so they pass
|
|
816
|
+
through untouched.
|
|
817
|
+
|
|
818
|
+
Server-to-server providers (Apple/Google RTDN) carry no transport signature —
|
|
819
|
+
their endpoint is otherwise open to anyone — so a shared secret is REQUIRED by
|
|
820
|
+
default ("IAP apps are not open by default"). The secret is supplied via the
|
|
821
|
+
`x-payment-notification-secret` header or `?secret=` query param (configure
|
|
822
|
+
the provider's notification URL accordingly) and is compared in constant time
|
|
823
|
+
against `PAYMENT_NOTIFICATION_SECRET`. Deployments that rely solely on the
|
|
824
|
+
provider's in-handler receipt re-verification can opt out with
|
|
825
|
+
`PAYMENT_NOTIFICATION_REQUIRE_SECRET=false` — but the secure behavior ships ON
|
|
826
|
+
by default and is never relaxed based on request presence.
|
|
827
|
+
|
|
828
|
+
```typescript
|
|
829
|
+
function requireWebhookAuthenticity(): MoleculeRequestHandler
|
|
830
|
+
```
|
|
831
|
+
|
|
832
|
+
**Returns:** An Express-compatible middleware function.
|
|
833
|
+
|
|
834
|
+
#### `handlers`
|
|
835
|
+
|
|
836
|
+
Re-exported as a namespace — reach every member below through it (e.g. `handlers.<name>`).
|
|
837
|
+
|
|
838
|
+
##### `handlers.CreateRequest`
|
|
839
|
+
|
|
840
|
+
Request body for user creation, including password and optional device name.
|
|
841
|
+
|
|
842
|
+
```typescript
|
|
843
|
+
interface CreateRequest extends MoleculeRequest {
|
|
844
|
+
body: types.CreateProps & {
|
|
845
|
+
password?: string
|
|
846
|
+
deviceName?: string
|
|
847
|
+
}
|
|
848
|
+
}
|
|
849
|
+
```
|
|
850
|
+
|
|
851
|
+
##### `handlers.create(resource)`
|
|
852
|
+
|
|
853
|
+
Creates a user with username and password. Validates username uniqueness and email format,
|
|
854
|
+
hashes the password into a separate secrets table, creates a session device via the bonded
|
|
855
|
+
DeviceService, and sets JWT authorization on the response.
|
|
856
|
+
|
|
857
|
+
```typescript
|
|
858
|
+
function create({ name, tableName, schema }: types.Resource<unknown>): (
|
|
859
|
+
req: CreateRequest,
|
|
860
|
+
res: MoleculeResponse,
|
|
861
|
+
) => Promise<
|
|
862
|
+
| { statusCode: number; body: { error: string; errorKey: string; props?: undefined } }
|
|
863
|
+
| {
|
|
864
|
+
statusCode: number
|
|
865
|
+
body: { props: Props & Record<string, unknown>; error?: undefined; errorKey?: undefined }
|
|
866
|
+
}
|
|
867
|
+
| {
|
|
868
|
+
statusCode: number
|
|
869
|
+
body: {
|
|
870
|
+
props: Record<string, unknown>
|
|
871
|
+
accessToken: string | null
|
|
872
|
+
user: Record<string, unknown>
|
|
873
|
+
}
|
|
874
|
+
}
|
|
875
|
+
>
|
|
876
|
+
```
|
|
877
|
+
|
|
878
|
+
- `resource` — The user resource configuration (name, tableName, schema).
|
|
879
|
+
- `resource.name` — The resource name.
|
|
880
|
+
- `resource.tableName` — The database table name for users.
|
|
881
|
+
- `resource.schema` — The validation schema for user properties.
|
|
882
|
+
|
|
883
|
+
**Returns:** A request handler that responds with `{ statusCode: 201, body: { props } }` on success.
|
|
884
|
+
|
|
885
|
+
##### `handlers.del(resource)`
|
|
886
|
+
|
|
887
|
+
Deletes a user and their associated data. Deletes the user row FIRST, then
|
|
888
|
+
the secrets, the devices (via the bonded DeviceService) and the payment
|
|
889
|
+
records (via the bonded PaymentRecordService).
|
|
890
|
+
|
|
891
|
+
The row goes first so that a failed delete changes nothing: deleting the
|
|
892
|
+
secrets first meant a row delete that then failed left an account with no
|
|
893
|
+
password hash — no password sign-in, and a retried delete that no longer
|
|
894
|
+
asks for one. When the row delete fails nothing else has been touched; when
|
|
895
|
+
it succeeds, a failure in the clean-up after it leaves records that belong
|
|
896
|
+
to no account (logged), never an account without its password.
|
|
897
|
+
|
|
898
|
+
```typescript
|
|
899
|
+
function del({
|
|
900
|
+
name: _name,
|
|
901
|
+
tableName,
|
|
902
|
+
schema: _schema,
|
|
903
|
+
}: types.Resource<unknown>): (
|
|
904
|
+
req: MoleculeRequest,
|
|
905
|
+
) => Promise<
|
|
906
|
+
| { statusCode: number; body: { error: string; errorKey: string; props?: undefined } }
|
|
907
|
+
| { statusCode: number; body: { props: { id: string }; error?: undefined; errorKey?: undefined } }
|
|
908
|
+
>
|
|
909
|
+
```
|
|
910
|
+
|
|
911
|
+
- `resource` — The user resource configuration (name, tableName, schema).
|
|
912
|
+
- `resource.name` — The resource name.
|
|
913
|
+
- `resource.tableName` — The database table name for users.
|
|
914
|
+
- `resource.schema` — The validation schema for user properties.
|
|
915
|
+
|
|
916
|
+
**Returns:** A request handler that responds with `{ statusCode: 200, body: { props: { id } } }` on success.
|
|
917
|
+
|
|
918
|
+
##### `handlers.ForgotPasswordRequest`
|
|
919
|
+
|
|
920
|
+
Request body for password reset initiation.
|
|
921
|
+
|
|
922
|
+
```typescript
|
|
923
|
+
interface ForgotPasswordRequest extends MoleculeRequest {
|
|
924
|
+
body: {
|
|
925
|
+
email?: string
|
|
926
|
+
}
|
|
927
|
+
}
|
|
928
|
+
```
|
|
929
|
+
|
|
930
|
+
##### `handlers.forgotPassword(resource)`
|
|
931
|
+
|
|
932
|
+
Generates a UUID password reset token, stores it in the secrets table, and sends a reset
|
|
933
|
+
email via the bonded email provider (if available). Always returns 200 regardless of whether
|
|
934
|
+
the email exists, to prevent user enumeration.
|
|
935
|
+
|
|
936
|
+
```typescript
|
|
937
|
+
function forgotPassword({
|
|
938
|
+
name: _name,
|
|
939
|
+
tableName,
|
|
940
|
+
schema: _schema,
|
|
941
|
+
}: types.Resource<unknown>): (
|
|
942
|
+
req: ForgotPasswordRequest,
|
|
943
|
+
) => Promise<
|
|
944
|
+
| { statusCode: number; body: { error: string; errorKey: string; success?: undefined } }
|
|
945
|
+
| { statusCode: number; body: { success: boolean; error?: undefined; errorKey?: undefined } }
|
|
946
|
+
>
|
|
947
|
+
```
|
|
948
|
+
|
|
949
|
+
- `resource` — The user resource configuration (name, tableName, schema).
|
|
950
|
+
- `resource.name` — The resource name.
|
|
951
|
+
- `resource.tableName` — The database table name for users.
|
|
952
|
+
- `resource.schema` — The validation schema for user properties.
|
|
953
|
+
|
|
954
|
+
**Returns:** A request handler that responds with `{ statusCode: 200, body: { success: true } }`.
|
|
955
|
+
|
|
956
|
+
##### `handlers.LogInRequest`
|
|
957
|
+
|
|
958
|
+
Request body for user login, supporting password, reset token, and 2FA flows.
|
|
959
|
+
|
|
960
|
+
```typescript
|
|
961
|
+
interface LogInRequest extends MoleculeRequest {
|
|
962
|
+
body: {
|
|
963
|
+
username?: string
|
|
964
|
+
email?: string
|
|
965
|
+
password?: string
|
|
966
|
+
passwordResetToken?: string
|
|
967
|
+
twoFactorToken?: string
|
|
968
|
+
deviceName?: string
|
|
969
|
+
}
|
|
970
|
+
}
|
|
971
|
+
```
|
|
972
|
+
|
|
973
|
+
##### `handlers.logIn(resource)`
|
|
974
|
+
|
|
975
|
+
Logs in a user by username or email. Supports password authentication, password reset token
|
|
976
|
+
authentication (1-hour expiry), and optional two-factor verification via `@molecule/api-two-factor`.
|
|
977
|
+
On success, creates/updates a device via the bonded DeviceService, sets JWT authorization,
|
|
978
|
+
and notifies other devices about the new login.
|
|
979
|
+
|
|
980
|
+
```typescript
|
|
981
|
+
function logIn({ name: _name, tableName, schema: _schema }: types.Resource<unknown>): (
|
|
982
|
+
req: LogInRequest,
|
|
983
|
+
res: MoleculeResponse,
|
|
984
|
+
) => Promise<
|
|
985
|
+
| {
|
|
986
|
+
statusCode: number
|
|
987
|
+
body: {
|
|
988
|
+
error: string
|
|
989
|
+
errorKey: string
|
|
990
|
+
twoFactorRequired?: undefined
|
|
991
|
+
props?: undefined
|
|
992
|
+
accessToken?: undefined
|
|
993
|
+
user?: undefined
|
|
994
|
+
}
|
|
995
|
+
}
|
|
996
|
+
| {
|
|
997
|
+
statusCode: number
|
|
998
|
+
body: {
|
|
999
|
+
twoFactorRequired: boolean
|
|
1000
|
+
error?: undefined
|
|
1001
|
+
errorKey?: undefined
|
|
1002
|
+
props?: undefined
|
|
1003
|
+
accessToken?: undefined
|
|
1004
|
+
user?: undefined
|
|
1005
|
+
}
|
|
1006
|
+
}
|
|
1007
|
+
| {
|
|
1008
|
+
statusCode: number
|
|
1009
|
+
body: {
|
|
1010
|
+
props: Record<string, unknown>
|
|
1011
|
+
accessToken: string | null
|
|
1012
|
+
user: Record<string, unknown>
|
|
1013
|
+
error?: undefined
|
|
1014
|
+
errorKey?: undefined
|
|
1015
|
+
twoFactorRequired?: undefined
|
|
1016
|
+
}
|
|
1017
|
+
}
|
|
1018
|
+
>
|
|
1019
|
+
```
|
|
1020
|
+
|
|
1021
|
+
- `resource` — The user resource configuration (name, tableName, schema).
|
|
1022
|
+
- `resource.name` — The resource name.
|
|
1023
|
+
- `resource.tableName` — The database table name for users.
|
|
1024
|
+
- `resource.schema` — The validation schema for user properties.
|
|
1025
|
+
|
|
1026
|
+
**Returns:** A request handler that responds with `{ statusCode: 200, body: { props } }` on success, or `{ statusCode: 206, body: { twoFactorRequired: true } }` when 2FA is needed.
|
|
1027
|
+
|
|
1028
|
+
##### `handlers.LogInOAuthRequest`
|
|
1029
|
+
|
|
1030
|
+
Request body for OAuth login, including the OAuth server name, authorization code, and PKCE verifier.
|
|
1031
|
+
|
|
1032
|
+
```typescript
|
|
1033
|
+
interface LogInOAuthRequest extends MoleculeRequest {
|
|
1034
|
+
body: {
|
|
1035
|
+
server?: string
|
|
1036
|
+
code?: string
|
|
1037
|
+
state?: string
|
|
1038
|
+
codeVerifier?: string
|
|
1039
|
+
redirect_uri?: string
|
|
1040
|
+
deviceName?: string
|
|
1041
|
+
}
|
|
1042
|
+
}
|
|
1043
|
+
```
|
|
1044
|
+
|
|
1045
|
+
##### `handlers.logInOAuth(resource)`
|
|
1046
|
+
|
|
1047
|
+
Logs in or creates a user via OAuth. Verifies the authorization code with the bonded OAuth
|
|
1048
|
+
provider (`get('oauth', serverName)`), then either finds an existing user by oauthServer+oauthId
|
|
1049
|
+
or creates a new one with a unique username. Sets JWT authorization and creates a session device.
|
|
1050
|
+
|
|
1051
|
+
```typescript
|
|
1052
|
+
function logInOAuth({ name, tableName, schema }: types.Resource<unknown>): (
|
|
1053
|
+
req: LogInOAuthRequest,
|
|
1054
|
+
res: MoleculeResponse,
|
|
1055
|
+
) => Promise<
|
|
1056
|
+
| {
|
|
1057
|
+
statusCode: number
|
|
1058
|
+
body: { error: string; errorKey: string; twoFactorRequired?: undefined; props?: undefined }
|
|
1059
|
+
}
|
|
1060
|
+
| {
|
|
1061
|
+
statusCode: number
|
|
1062
|
+
body: { error: string; errorKey: string; twoFactorRequired: boolean; props?: undefined }
|
|
1063
|
+
}
|
|
1064
|
+
| {
|
|
1065
|
+
statusCode: number
|
|
1066
|
+
body: {
|
|
1067
|
+
props: Record<string, unknown>
|
|
1068
|
+
error?: undefined
|
|
1069
|
+
errorKey?: undefined
|
|
1070
|
+
twoFactorRequired?: undefined
|
|
1071
|
+
}
|
|
1072
|
+
}
|
|
1073
|
+
>
|
|
1074
|
+
```
|
|
1075
|
+
|
|
1076
|
+
- `resource` — The user resource configuration (name, tableName, schema).
|
|
1077
|
+
- `resource.name` — The resource name.
|
|
1078
|
+
- `resource.tableName` — The database table name for users.
|
|
1079
|
+
- `resource.schema` — The validation schema for user properties.
|
|
1080
|
+
|
|
1081
|
+
**Returns:** A request handler that responds with `{ statusCode: 200, body: { props } }` on success.
|
|
1082
|
+
|
|
1083
|
+
##### `handlers.logout()`
|
|
1084
|
+
|
|
1085
|
+
Logs the current user out. Revokes the session device server-side (so the JWT
|
|
1086
|
+
is rejected on its next use — covering EVERY copy of the token, not just this
|
|
1087
|
+
browser) and clears the credential cookies.
|
|
1088
|
+
|
|
1089
|
+
[M1-1] Under the in-memory-token model the httpOnly `token` cookie is the
|
|
1090
|
+
persistent credential, so logout MUST clear it (and the `mol_auth` presence
|
|
1091
|
+
hint) — otherwise the app's cookie-based session restore (`GET /users/me` on
|
|
1092
|
+
init) would silently log the user back in on the next page load, and a
|
|
1093
|
+
client-side-only logout would be cosmetic (the cookie would still authenticate
|
|
1094
|
+
direct API calls). Cookie-authed via `auth` (no `:id`; the session identifies
|
|
1095
|
+
the user).
|
|
1096
|
+
|
|
1097
|
+
```typescript
|
|
1098
|
+
function logout(): (
|
|
1099
|
+
_req: MoleculeRequest,
|
|
1100
|
+
res: MoleculeResponse,
|
|
1101
|
+
) => Promise<{ statusCode: number; body: { success: boolean } }>
|
|
1102
|
+
```
|
|
1103
|
+
|
|
1104
|
+
**Returns:** A request handler responding `{ statusCode: 200, body: { success: true } }`.
|
|
1105
|
+
|
|
1106
|
+
##### `handlers.oauthAuthorize()`
|
|
1107
|
+
|
|
1108
|
+
OAuth initiation — `GET /users/oauth/:provider`.
|
|
1109
|
+
|
|
1110
|
+
The missing first half of the OAuth flow whose second half is
|
|
1111
|
+
`logInOAuth` (`POST /users/log-in/oauth`): generates the CSRF `state` and
|
|
1112
|
+
a per-session PKCE S256 verifier, stores BOTH in httpOnly cookies (the
|
|
1113
|
+
exact names + attributes `logInOAuth` reads and clears —
|
|
1114
|
+
`getAuthCookieName('oauth_state')` / `('oauth_verifier')` with
|
|
1115
|
+
`getAuthCookieOptions()`), then 302-redirects the browser to the
|
|
1116
|
+
provider's authorization URL.
|
|
1117
|
+
|
|
1118
|
+
Provider knowledge stays in the bond: the handler asks the bonded OAuth
|
|
1119
|
+
provider (`get('oauth', :provider)`) for its authorize URL via its
|
|
1120
|
+
optional `getAuthorizeUrl` builder (see `@molecule/api-oauth`). A
|
|
1121
|
+
provider that is not bonded, lacks the builder, or is unconfigured
|
|
1122
|
+
(builder returns `null`, e.g. missing client id) yields a clean 404/503 —
|
|
1123
|
+
never a crash.
|
|
1124
|
+
|
|
1125
|
+
The `redirect_uri` sent to the provider is `APP_ORIGIN` plus an optional
|
|
1126
|
+
validated same-origin `redirect_to` path from the query string (the page
|
|
1127
|
+
that initiated the flow, e.g. `/login`, where the app's OAuth callback
|
|
1128
|
+
handling is mounted). The path is sanitized so this can never become an
|
|
1129
|
+
open redirect; the state cookie — not `redirect_uri` — is the CSRF
|
|
1130
|
+
control.
|
|
1131
|
+
|
|
1132
|
+
```typescript
|
|
1133
|
+
function oauthAuthorize(): (
|
|
1134
|
+
req: MoleculeRequest,
|
|
1135
|
+
res: MoleculeResponse,
|
|
1136
|
+
) => Promise<
|
|
1137
|
+
| { statusCode: number; body: { error: string; errorKey: string }; headers?: undefined }
|
|
1138
|
+
| { statusCode: number; headers: { Location: string }; body: undefined }
|
|
1139
|
+
>
|
|
1140
|
+
```
|
|
1141
|
+
|
|
1142
|
+
**Returns:** A request handler that responds `302` with a `Location` header pointing at the provider's authorization URL.
|
|
1143
|
+
|
|
1144
|
+
##### `handlers.billingPortal()`
|
|
1145
|
+
|
|
1146
|
+
Opens the bonded provider's hosted billing portal for the user, so they can
|
|
1147
|
+
update their payment method, see invoices, and cancel — the self-service
|
|
1148
|
+
surface an app otherwise has to build (and usually doesn't, leaving a
|
|
1149
|
+
subscriber with no way out but support).
|
|
1150
|
+
|
|
1151
|
+
Reads the provider name from the `:provider` route parameter and returns the
|
|
1152
|
+
portal URL for the client to open. Providers whose purchases are managed by
|
|
1153
|
+
the platform store (Apple/Google) implement no portal and answer 400 —
|
|
1154
|
+
their subscribers manage the subscription in the store app.
|
|
1155
|
+
|
|
1156
|
+
The user is returned to the APP origin on exit (never the API origin, whose
|
|
1157
|
+
host holds no session cookie); an app-relative `returnPath` in the request
|
|
1158
|
+
body sends them back to the exact page they opened the portal from.
|
|
1159
|
+
|
|
1160
|
+
```typescript
|
|
1161
|
+
function billingPortal(): (req: MoleculeRequest) => Promise<
|
|
1162
|
+
| {
|
|
1163
|
+
statusCode: number
|
|
1164
|
+
body: { error: string; errorKey: string; id?: undefined; url?: undefined }
|
|
1165
|
+
}
|
|
1166
|
+
| {
|
|
1167
|
+
statusCode: number
|
|
1168
|
+
body: { id: string; url: string; error?: undefined; errorKey?: undefined }
|
|
1169
|
+
}
|
|
1170
|
+
>
|
|
1171
|
+
```
|
|
1172
|
+
|
|
1173
|
+
**Returns:** A request handler that responds `{ url, id }` with the portal session.
|
|
1174
|
+
|
|
1175
|
+
##### `handlers.handlePaymentNotification(resource)`
|
|
1176
|
+
|
|
1177
|
+
Generic payment notification handler that works with any bonded PaymentProvider. Reads the
|
|
1178
|
+
provider name from the `:provider` route parameter and dispatches based on `notificationFlow`:
|
|
1179
|
+
|
|
1180
|
+
- `'webhook'` (Stripe-style): Calls `handleWebhookEvent()` to parse signed webhook payloads,
|
|
1181
|
+
then looks up the user via PaymentRecordService by customer ID.
|
|
1182
|
+
- Default (Apple/Google-style): Calls `parseNotification()` for server-to-server notifications,
|
|
1183
|
+
then looks up the user via PaymentRecordService by transaction ID.
|
|
1184
|
+
|
|
1185
|
+
Updates the user's plan on renewal/change or clears it on cancellation/expiry/refund/revocation.
|
|
1186
|
+
Always returns 200 to acknowledge receipt.
|
|
1187
|
+
|
|
1188
|
+
```typescript
|
|
1189
|
+
function handlePaymentNotification({
|
|
1190
|
+
name,
|
|
1191
|
+
tableName,
|
|
1192
|
+
schema: _schema,
|
|
1193
|
+
}: types.Resource<unknown>): (
|
|
1194
|
+
req: MoleculeRequest,
|
|
1195
|
+
) => Promise<{ statusCode: number; body: string } | { statusCode: number; body: { error: string } }>
|
|
1196
|
+
```
|
|
1197
|
+
|
|
1198
|
+
- `resource` — The user resource configuration (name, tableName, schema).
|
|
1199
|
+
- `resource.name` — The resource name.
|
|
1200
|
+
- `resource.tableName` — The database table name for users.
|
|
1201
|
+
- `resource.schema` — The validation schema for user properties.
|
|
1202
|
+
|
|
1203
|
+
**Returns:** A request handler for payment provider notifications.
|
|
1204
|
+
|
|
1205
|
+
##### `handlers.verifyPayment(resource)`
|
|
1206
|
+
|
|
1207
|
+
Generic payment verification handler that works with any bonded PaymentProvider. Reads the
|
|
1208
|
+
provider name from the `:provider` route parameter and dispatches based on `verifyFlow`:
|
|
1209
|
+
|
|
1210
|
+
- `'subscription'` (Stripe-style): Calls `verifySubscription(subscriptionId)`.
|
|
1211
|
+
- Default (Apple/Google-style): Calls `verifyReceipt()` or `verifyPurchase()` with a receipt
|
|
1212
|
+
string and the plan's platform product ID.
|
|
1213
|
+
|
|
1214
|
+
On successful verification, updates the user's plan (planKey, planExpiresAt, planAutoRenews)
|
|
1215
|
+
and stores the payment record via the bonded PaymentRecordService.
|
|
1216
|
+
|
|
1217
|
+
```typescript
|
|
1218
|
+
function verifyPayment({ name, tableName, schema: _schema }: types.Resource<unknown>): (
|
|
1219
|
+
req: MoleculeRequest,
|
|
1220
|
+
) => Promise<
|
|
1221
|
+
| { statusCode: number; body: { error: string; errorKey: string; props?: undefined } }
|
|
1222
|
+
| {
|
|
1223
|
+
statusCode: number
|
|
1224
|
+
body: {
|
|
1225
|
+
props: Pick<Props, 'updatedAt'> & {
|
|
1226
|
+
planKey?: string | undefined
|
|
1227
|
+
planExpiresAt?: string | undefined
|
|
1228
|
+
planAutoRenews?: boolean | undefined
|
|
1229
|
+
}
|
|
1230
|
+
error?: undefined
|
|
1231
|
+
errorKey?: undefined
|
|
1232
|
+
}
|
|
1233
|
+
}
|
|
1234
|
+
>
|
|
1235
|
+
```
|
|
1236
|
+
|
|
1237
|
+
- `resource` — The user resource configuration (name, tableName, schema).
|
|
1238
|
+
- `resource.name` — The resource name.
|
|
1239
|
+
- `resource.tableName` — The database table name for users.
|
|
1240
|
+
- `resource.schema` — The validation schema for user properties.
|
|
1241
|
+
|
|
1242
|
+
**Returns:** A request handler for payment verification.
|
|
1243
|
+
|
|
1244
|
+
##### `handlers.read(resource)`
|
|
1245
|
+
|
|
1246
|
+
Reads a user by ID from the database. Attaches plan info (with expiration/renewal status) via the
|
|
1247
|
+
bonded PlanService if available. When reading the authenticated user's own record, also includes
|
|
1248
|
+
two-factor authentication status from the secrets table.
|
|
1249
|
+
|
|
1250
|
+
```typescript
|
|
1251
|
+
function read({
|
|
1252
|
+
name: _name,
|
|
1253
|
+
tableName,
|
|
1254
|
+
schema: _schema,
|
|
1255
|
+
}: types.Resource<unknown>): (
|
|
1256
|
+
req: MoleculeRequest,
|
|
1257
|
+
res: MoleculeResponse,
|
|
1258
|
+
) => Promise<{ statusCode: number; body: Record<string, unknown> }>
|
|
1259
|
+
```
|
|
1260
|
+
|
|
1261
|
+
- `resource` — The user resource configuration (name, tableName, schema).
|
|
1262
|
+
- `resource.name` — The resource name.
|
|
1263
|
+
- `resource.tableName` — The database table name for users.
|
|
1264
|
+
- `resource.schema` — The validation schema for user properties.
|
|
1265
|
+
|
|
1266
|
+
**Returns:** A request handler that responds with `{ statusCode: 200, body: { props, plan?, twoFactorEnabled? } }`.
|
|
1267
|
+
|
|
1268
|
+
##### `handlers.readSelf(resource)`
|
|
1269
|
+
|
|
1270
|
+
Reads the AUTHENTICATED user from the session — no `:id` param. Backs
|
|
1271
|
+
`GET /users/me`, which the app's auth client calls on init to RESTORE the
|
|
1272
|
+
session after a full page load.
|
|
1273
|
+
|
|
1274
|
+
[M1-1] The bearer token is held in memory (a localStorage copy is
|
|
1275
|
+
XSS-exfiltratable and is forbidden), so it is lost on reload; the httpOnly
|
|
1276
|
+
session cookie is the persistent credential. The `auth` middleware reads that
|
|
1277
|
+
cookie into `res.locals.session`, and this handler returns the current user
|
|
1278
|
+
so the client can re-establish `authenticated` state WITHOUT the token ever
|
|
1279
|
+
being exposed to JavaScript. Always self, so it mirrors `read`'s self branch
|
|
1280
|
+
(plan + two-factor status).
|
|
1281
|
+
|
|
1282
|
+
```typescript
|
|
1283
|
+
function readSelf({
|
|
1284
|
+
tableName,
|
|
1285
|
+
}: types.Resource<unknown>): (
|
|
1286
|
+
_req: MoleculeRequest,
|
|
1287
|
+
res: MoleculeResponse,
|
|
1288
|
+
) => Promise<{ statusCode: number; body: Record<string, unknown> }>
|
|
1289
|
+
```
|
|
1290
|
+
|
|
1291
|
+
- `resource` — The user resource configuration (name, tableName, schema).
|
|
1292
|
+
- `resource.tableName` — The database table name for users.
|
|
1293
|
+
|
|
1294
|
+
**Returns:** A request handler responding `{ statusCode: 200, body: { props, plan?, twoFactorEnabled? } }`, or 401 when unauthenticated.
|
|
1295
|
+
|
|
1296
|
+
##### `handlers.RESET_TOKEN_TTL_MS`
|
|
1297
|
+
|
|
1298
|
+
How long a password-reset token stays valid, in milliseconds. Shared with
|
|
1299
|
+
`logIn`, which accepts the same token as a login credential and must apply
|
|
1300
|
+
the SAME gate — the same constant AND the same boundary (`tokenAge` equal to
|
|
1301
|
+
the TTL is still valid in both), or one path becomes the looser way in for
|
|
1302
|
+
the other's one-hour credential.
|
|
1303
|
+
|
|
1304
|
+
```typescript
|
|
1305
|
+
const RESET_TOKEN_TTL_MS: number
|
|
1306
|
+
```
|
|
1307
|
+
|
|
1308
|
+
##### `handlers.ResetPasswordRequest`
|
|
1309
|
+
|
|
1310
|
+
Request body for confirming a password reset using a token.
|
|
1311
|
+
|
|
1312
|
+
```typescript
|
|
1313
|
+
interface ResetPasswordRequest extends MoleculeRequest {
|
|
1314
|
+
body: {
|
|
1315
|
+
token?: string
|
|
1316
|
+
password?: string
|
|
1317
|
+
}
|
|
1318
|
+
}
|
|
1319
|
+
```
|
|
1320
|
+
|
|
1321
|
+
##### `handlers.resetPassword(resource)`
|
|
1322
|
+
|
|
1323
|
+
Confirms a password reset by validating the one-time token previously generated by
|
|
1324
|
+
`forgotPassword`, then hashing and storing the new password and clearing the token.
|
|
1325
|
+
|
|
1326
|
+
Token TTL is one hour. The handler responds with 400 for missing fields, 400 for
|
|
1327
|
+
passwords that fail length validation (matching `updatePassword`), and 401 for
|
|
1328
|
+
tokens that are unknown, expired, or already consumed.
|
|
1329
|
+
|
|
1330
|
+
On success, all existing device sessions are invalidated to force a fresh login,
|
|
1331
|
+
mirroring the behavior of `updatePassword`.
|
|
1332
|
+
|
|
1333
|
+
The new password and the token clear are written as ONE WHERE-guarded update
|
|
1334
|
+
matching the verified token hash, so the link is consumed atomically: of two
|
|
1335
|
+
concurrent requests holding the same link, only the one that flips the token
|
|
1336
|
+
to NULL matches a row — the other is refused with 401 and cannot overwrite
|
|
1337
|
+
the winner's password.
|
|
1338
|
+
|
|
1339
|
+
```typescript
|
|
1340
|
+
function resetPassword({
|
|
1341
|
+
name: _name,
|
|
1342
|
+
tableName,
|
|
1343
|
+
schema: _schema,
|
|
1344
|
+
}: types.Resource<unknown>): (
|
|
1345
|
+
req: ResetPasswordRequest,
|
|
1346
|
+
) => Promise<
|
|
1347
|
+
| { statusCode: number; body: { error: string; errorKey: string; success?: undefined } }
|
|
1348
|
+
| { statusCode: number; body: { success: boolean; error?: undefined; errorKey?: undefined } }
|
|
1349
|
+
>
|
|
1350
|
+
```
|
|
1351
|
+
|
|
1352
|
+
- `resource` — The user resource configuration (name, tableName, schema).
|
|
1353
|
+
- `resource.tableName` — The database table name for users.
|
|
1354
|
+
|
|
1355
|
+
**Returns:** A request handler that responds with `{ statusCode: 200, body: { success: true } }` on success.
|
|
1356
|
+
|
|
1357
|
+
##### `handlers.update(resource)`
|
|
1358
|
+
|
|
1359
|
+
Updates a user's profile fields (username, name, email). Validates username format
|
|
1360
|
+
(alphanumeric, max 255 chars), email format, and uniqueness of both fields against
|
|
1361
|
+
other users before delegating to the resource update utility.
|
|
1362
|
+
|
|
1363
|
+
```typescript
|
|
1364
|
+
function update({ name, tableName, schema: _schema }: types.Resource<unknown>): (
|
|
1365
|
+
req: MoleculeRequest,
|
|
1366
|
+
) => Promise<
|
|
1367
|
+
| { statusCode: number; body: { error: string; errorKey: string; props?: undefined } }
|
|
1368
|
+
| {
|
|
1369
|
+
statusCode: number
|
|
1370
|
+
body: {
|
|
1371
|
+
props: Pick<Props, 'updatedAt'> & Record<string, unknown>
|
|
1372
|
+
error?: undefined
|
|
1373
|
+
errorKey?: undefined
|
|
1374
|
+
}
|
|
1375
|
+
}
|
|
1376
|
+
>
|
|
1377
|
+
```
|
|
1378
|
+
|
|
1379
|
+
- `resource` — The user resource configuration (name, tableName, schema).
|
|
1380
|
+
- `resource.name` — The resource name.
|
|
1381
|
+
- `resource.tableName` — The database table name for users.
|
|
1382
|
+
- `resource.schema` — The validation schema for user properties.
|
|
1383
|
+
|
|
1384
|
+
**Returns:** A request handler that responds with the updated user props on success.
|
|
1385
|
+
|
|
1386
|
+
##### `handlers.UpdatePasswordRequest`
|
|
1387
|
+
|
|
1388
|
+
Request body for password update, with current password verification and new password.
|
|
1389
|
+
|
|
1390
|
+
```typescript
|
|
1391
|
+
interface UpdatePasswordRequest extends MoleculeRequest {
|
|
1392
|
+
body: {
|
|
1393
|
+
currentPassword?: string
|
|
1394
|
+
newPassword?: string
|
|
1395
|
+
}
|
|
1396
|
+
}
|
|
1397
|
+
```
|
|
1398
|
+
|
|
1399
|
+
##### `handlers.updatePassword(resource)`
|
|
1400
|
+
|
|
1401
|
+
Updates a user's password. If the user already has a password hash, the current password
|
|
1402
|
+
must be provided and verified first. The new password is hashed via `@molecule/api-password`
|
|
1403
|
+
and stored in the secrets table.
|
|
1404
|
+
|
|
1405
|
+
```typescript
|
|
1406
|
+
function updatePassword({
|
|
1407
|
+
name: _name,
|
|
1408
|
+
tableName,
|
|
1409
|
+
schema: _schema,
|
|
1410
|
+
}: types.Resource<unknown>): (
|
|
1411
|
+
req: UpdatePasswordRequest,
|
|
1412
|
+
) => Promise<
|
|
1413
|
+
| { statusCode: number; body: { error: string; errorKey: string; success?: undefined } }
|
|
1414
|
+
| { statusCode: number; body: { success: boolean; error?: undefined; errorKey?: undefined } }
|
|
1415
|
+
>
|
|
1416
|
+
```
|
|
1417
|
+
|
|
1418
|
+
- `resource` — The user resource configuration (name, tableName, schema).
|
|
1419
|
+
- `resource.name` — The resource name.
|
|
1420
|
+
- `resource.tableName` — The database table name for users.
|
|
1421
|
+
- `resource.schema` — The validation schema for user properties.
|
|
1422
|
+
|
|
1423
|
+
**Returns:** A request handler that responds with `{ statusCode: 200, body: { success: true } }` on success.
|
|
1424
|
+
|
|
1425
|
+
##### `handlers.UpdatePlanRequest`
|
|
1426
|
+
|
|
1427
|
+
Request body for plan update, containing the target plan key.
|
|
1428
|
+
|
|
1429
|
+
```typescript
|
|
1430
|
+
interface UpdatePlanRequest extends MoleculeRequest {
|
|
1431
|
+
body: {
|
|
1432
|
+
planKey?: string
|
|
1433
|
+
/**
|
|
1434
|
+
* Units of the plan to buy — seats, on a per-seat plan. Omitted means one.
|
|
1435
|
+
*
|
|
1436
|
+
* Read from the request because only the app knows what a unit means: the
|
|
1437
|
+
* plan catalogue describes a price, not how many of it this buyer wants.
|
|
1438
|
+
* Ignored by flat-priced plans, whose providers receive no quantity at all.
|
|
1439
|
+
*/
|
|
1440
|
+
quantity?: number
|
|
1441
|
+
}
|
|
1442
|
+
}
|
|
1443
|
+
```
|
|
1444
|
+
|
|
1445
|
+
##### `handlers.updatePlan(resource)`
|
|
1446
|
+
|
|
1447
|
+
Updates a user's subscription plan. Uses the bonded PlanService to look up plan metadata and
|
|
1448
|
+
delegates to the appropriate bonded PaymentProvider based on the plan's `platformKey`. Handles
|
|
1449
|
+
cross-platform subscription cancellation when switching providers. Returns a checkout URL
|
|
1450
|
+
(status 201) for new subscriptions, or directly updates the user record for in-place changes
|
|
1451
|
+
and free plan downgrades.
|
|
1452
|
+
|
|
1453
|
+
```typescript
|
|
1454
|
+
function updatePlan({ name, tableName, schema: _schema }: types.Resource<unknown>): (
|
|
1455
|
+
req: UpdatePlanRequest,
|
|
1456
|
+
) => Promise<
|
|
1457
|
+
| { statusCode: number; body: { error: string; errorKey: string; props?: undefined } }
|
|
1458
|
+
| {
|
|
1459
|
+
statusCode: number
|
|
1460
|
+
body: {
|
|
1461
|
+
props: Pick<Props, 'updatedAt'> & Record<string, unknown>
|
|
1462
|
+
error?: undefined
|
|
1463
|
+
errorKey?: undefined
|
|
1464
|
+
}
|
|
1465
|
+
}
|
|
1466
|
+
| { statusCode: number; body: { checkoutUrl: string } }
|
|
1467
|
+
>
|
|
1468
|
+
```
|
|
1469
|
+
|
|
1470
|
+
- `resource` — The user resource configuration (name, tableName, schema).
|
|
1471
|
+
- `resource.name` — The resource name.
|
|
1472
|
+
- `resource.tableName` — The database table name for users.
|
|
1473
|
+
- `resource.schema` — The validation schema for user properties.
|
|
1474
|
+
|
|
1475
|
+
**Returns:** A request handler for plan updates.
|
|
1476
|
+
|
|
1477
|
+
##### `handlers.VerifyTwoFactorRequest`
|
|
1478
|
+
|
|
1479
|
+
Request body for two-factor authentication operations (setup, enable, or disable).
|
|
1480
|
+
|
|
1481
|
+
```typescript
|
|
1482
|
+
interface VerifyTwoFactorRequest extends MoleculeRequest {
|
|
1483
|
+
body: {
|
|
1484
|
+
token?: string
|
|
1485
|
+
action?: 'setup' | 'enable' | 'disable'
|
|
1486
|
+
}
|
|
1487
|
+
}
|
|
1488
|
+
```
|
|
1489
|
+
|
|
1490
|
+
##### `handlers.verifyTwoFactor(resource)`
|
|
1491
|
+
|
|
1492
|
+
Handles two-factor authentication lifecycle via `@molecule/api-two-factor`:
|
|
1493
|
+
|
|
1494
|
+
- `setup`: Generates a TOTP secret and returns QR code URLs for authenticator apps.
|
|
1495
|
+
- `enable`: Verifies a TOTP token against the pending secret, then activates 2FA.
|
|
1496
|
+
- `disable`: Verifies a TOTP token against the active secret, then deactivates 2FA.
|
|
1497
|
+
|
|
1498
|
+
```typescript
|
|
1499
|
+
function verifyTwoFactor({ name: _name, tableName, schema: _schema }: types.Resource<unknown>): (
|
|
1500
|
+
req: VerifyTwoFactorRequest,
|
|
1501
|
+
) => Promise<
|
|
1502
|
+
| { statusCode: number; body: { error: string; errorKey: string; twoFactorEnabled?: undefined } }
|
|
1503
|
+
| {
|
|
1504
|
+
statusCode: number
|
|
1505
|
+
body: {
|
|
1506
|
+
secret: string
|
|
1507
|
+
keyUrl: string
|
|
1508
|
+
QRImageUrl: string
|
|
1509
|
+
error?: undefined
|
|
1510
|
+
errorKey?: undefined
|
|
1511
|
+
twoFactorEnabled?: undefined
|
|
1512
|
+
}
|
|
1513
|
+
}
|
|
1514
|
+
| {
|
|
1515
|
+
statusCode: number
|
|
1516
|
+
body: { twoFactorEnabled: boolean; error?: undefined; errorKey?: undefined }
|
|
1517
|
+
}
|
|
1518
|
+
>
|
|
1519
|
+
```
|
|
1520
|
+
|
|
1521
|
+
- `resource` — The user resource configuration (name, tableName, schema).
|
|
1522
|
+
- `resource.name` — The resource name.
|
|
1523
|
+
- `resource.tableName` — The database table name for users.
|
|
1524
|
+
- `resource.schema` — The validation schema for user properties.
|
|
1525
|
+
|
|
1526
|
+
**Returns:** A request handler for two-factor authentication operations.
|
|
1527
|
+
|
|
1528
|
+
#### `types`
|
|
1529
|
+
|
|
1530
|
+
Re-exported as a namespace — reach every member below through it (e.g. `types.<name>`).
|
|
1531
|
+
|
|
1532
|
+
##### `types.Props`
|
|
1533
|
+
|
|
1534
|
+
User props type inferred from schema.
|
|
1535
|
+
|
|
1536
|
+
```typescript
|
|
1537
|
+
type Props = z.infer<typeof propsSchema>
|
|
1538
|
+
```
|
|
1539
|
+
|
|
1540
|
+
##### `types.SecretProps`
|
|
1541
|
+
|
|
1542
|
+
Secret Props type.
|
|
1543
|
+
|
|
1544
|
+
```typescript
|
|
1545
|
+
type SecretProps = z.infer<typeof secretPropsSchema>
|
|
1546
|
+
```
|
|
1547
|
+
|
|
1548
|
+
##### `types.CreateProps`
|
|
1549
|
+
|
|
1550
|
+
Create Props type.
|
|
1551
|
+
|
|
1552
|
+
```typescript
|
|
1553
|
+
type CreateProps = z.infer<typeof createPropsSchema>
|
|
1554
|
+
```
|
|
1555
|
+
|
|
1556
|
+
##### `types.CreateSecretProps`
|
|
1557
|
+
|
|
1558
|
+
Create Secret Props type.
|
|
1559
|
+
|
|
1560
|
+
```typescript
|
|
1561
|
+
type CreateSecretProps = z.infer<typeof createSecretPropsSchema>
|
|
1562
|
+
```
|
|
1563
|
+
|
|
1564
|
+
##### `types.CreateOAuthProps`
|
|
1565
|
+
|
|
1566
|
+
Create O Auth Props type.
|
|
1567
|
+
|
|
1568
|
+
```typescript
|
|
1569
|
+
type CreateOAuthProps = z.infer<typeof createOAuthPropsSchema>
|
|
1570
|
+
```
|
|
1571
|
+
|
|
1572
|
+
##### `types.UpdateProps`
|
|
1573
|
+
|
|
1574
|
+
Update Props type.
|
|
1575
|
+
|
|
1576
|
+
```typescript
|
|
1577
|
+
type UpdateProps = z.infer<typeof updatePropsSchema>
|
|
1578
|
+
```
|
|
1579
|
+
|
|
1580
|
+
##### `types.UpdatePasswordSecretProps`
|
|
1581
|
+
|
|
1582
|
+
Update Password Secret Props type.
|
|
1583
|
+
|
|
1584
|
+
```typescript
|
|
1585
|
+
type UpdatePasswordSecretProps = z.infer<typeof updatePasswordSecretPropsSchema>
|
|
1586
|
+
```
|
|
1587
|
+
|
|
1588
|
+
##### `types.UpdatePlanProps`
|
|
1589
|
+
|
|
1590
|
+
Update Plan Props type.
|
|
1591
|
+
|
|
1592
|
+
```typescript
|
|
1593
|
+
type UpdatePlanProps = z.infer<typeof updatePlanPropsSchema>
|
|
1594
|
+
```
|
|
1595
|
+
|
|
1596
|
+
##### `types.VerifyTwoFactorProps`
|
|
1597
|
+
|
|
1598
|
+
Verify Two Factor Props type.
|
|
1599
|
+
|
|
1600
|
+
```typescript
|
|
1601
|
+
type VerifyTwoFactorProps = z.infer<typeof verifyTwoFactorPropsSchema>
|
|
1602
|
+
```
|
|
1603
|
+
|
|
1604
|
+
##### `types.VerifyTwoFactorSecretProps`
|
|
1605
|
+
|
|
1606
|
+
Verify Two Factor Secret Props type.
|
|
1607
|
+
|
|
1608
|
+
```typescript
|
|
1609
|
+
type VerifyTwoFactorSecretProps = z.infer<typeof verifyTwoFactorSecretPropsSchema>
|
|
1610
|
+
```
|
|
1611
|
+
|
|
1612
|
+
##### `types.Session`
|
|
1613
|
+
|
|
1614
|
+
User session data (userId, email, role, permissions, metadata) inferred from sessionSchema.
|
|
1615
|
+
|
|
1616
|
+
```typescript
|
|
1617
|
+
type Session = z.infer<typeof sessionSchema>
|
|
1618
|
+
```
|
|
1619
|
+
|
|
1620
|
+
##### `types.Resource`
|
|
1621
|
+
|
|
1622
|
+
An object describing the `user` resource.
|
|
1623
|
+
|
|
1624
|
+
```typescript
|
|
1625
|
+
type Resource<T = unknown> = resourceTypes.Resource<T>
|
|
1626
|
+
```
|
|
1627
|
+
|
|
1628
|
+
#### `utilities`
|
|
1629
|
+
|
|
1630
|
+
Re-exported as a namespace — reach every member below through it (e.g. `utilities.<name>`).
|
|
1631
|
+
|
|
1632
|
+
##### `utilities.fetchAvatarDataUri(url)`
|
|
1633
|
+
|
|
1634
|
+
Downloads an OAuth provider's profile image and re-hosts it as an inline
|
|
1635
|
+
`data:` URI so the stored avatar never references a third-party domain
|
|
1636
|
+
(provider URLs expire, and hot-linking leaks every viewer's request to the
|
|
1637
|
+
provider's CDN).
|
|
1638
|
+
|
|
1639
|
+
Best-effort by design: any refusal or failure — non-https URL, private or
|
|
1640
|
+
unresolvable host, timeout, non-image content type, oversized body — returns
|
|
1641
|
+
`undefined` (logged at debug) so OAuth login/creation never fails over an
|
|
1642
|
+
avatar.
|
|
1643
|
+
|
|
1644
|
+
```typescript
|
|
1645
|
+
function fetchAvatarDataUri(url: string): Promise<string | undefined>
|
|
1646
|
+
```
|
|
1647
|
+
|
|
1648
|
+
- `url` — The provider profile-image URL (`OAuthUserProps.avatar`).
|
|
1649
|
+
|
|
1650
|
+
**Returns:** The image as a `data:` URI within {@link MAX_AVATAR_LENGTH}, or `undefined`.
|
|
1651
|
+
|
|
1652
|
+
##### `utilities.getPlan(user)`
|
|
1653
|
+
|
|
1654
|
+
Get a user's current plan info.
|
|
1655
|
+
Uses the bonded PlanService if available.
|
|
1656
|
+
|
|
1657
|
+
```typescript
|
|
1658
|
+
function getPlan(user: {
|
|
1659
|
+
id: string
|
|
1660
|
+
createdAt: string
|
|
1661
|
+
updatedAt: string
|
|
1662
|
+
username?: string | undefined
|
|
1663
|
+
name?: string | undefined
|
|
1664
|
+
email?: string | null | undefined
|
|
1665
|
+
emailVerified?: boolean | undefined
|
|
1666
|
+
avatar?: string | null | undefined
|
|
1667
|
+
bio?: string | null | undefined
|
|
1668
|
+
twoFactorEnabled?: boolean | undefined
|
|
1669
|
+
oauthServer?: string | undefined
|
|
1670
|
+
oauthId?: string | undefined
|
|
1671
|
+
oauthData?: Record<string, unknown> | undefined
|
|
1672
|
+
planKey?: string | undefined
|
|
1673
|
+
planExpiresAt?: string | undefined
|
|
1674
|
+
planAutoRenews?: boolean | undefined
|
|
1675
|
+
}): Promise<Plan | PlanInfo | null>
|
|
1676
|
+
```
|
|
1677
|
+
|
|
1678
|
+
- `user` — The user props (needs `planKey`, `planExpiresAt`, `planAutoRenews`).
|
|
1679
|
+
|
|
1680
|
+
**Returns:** The user's plan info with expiration status, or `null` if no `PlanService` is bonded.
|
|
1681
|
+
|
|
1682
|
+
##### `utilities.invalidateEntitlementsCache(userId)`
|
|
1683
|
+
|
|
1684
|
+
Invalidates the entitlements plan-key cache for a user after their plan
|
|
1685
|
+
changed (paid upgrade via `verifyPayment`, webhook grant/cancellation via
|
|
1686
|
+
`handlePaymentNotification`, admin `updatePlan`).
|
|
1687
|
+
|
|
1688
|
+
`@molecule/api-entitlements` caches each user's effective plan key for its
|
|
1689
|
+
TTL (default 5 minutes) on the `getEffectiveTier` hot path. Without this
|
|
1690
|
+
call, a user who just PAID keeps seeing their old (free) tier — and a
|
|
1691
|
+
canceled user keeps premium — until the TTL lapses. The dependency is an
|
|
1692
|
+
OPTIONAL peer: apps that don't use entitlements simply have nothing to
|
|
1693
|
+
invalidate, so the import failure is a documented no-op.
|
|
1694
|
+
|
|
1695
|
+
```typescript
|
|
1696
|
+
function invalidateEntitlementsCache(userId: string): Promise<void>
|
|
1697
|
+
```
|
|
1698
|
+
|
|
1699
|
+
- `userId` — The user whose cached plan key must be dropped.
|
|
1700
|
+
|
|
1701
|
+
##### `utilities.invalidateEntitlementsCacheSafe(userId)`
|
|
1702
|
+
|
|
1703
|
+
Fire-and-forget variant of {@link invalidateEntitlementsCache} for call
|
|
1704
|
+
sites that must not await (or fail) the surrounding response on cache
|
|
1705
|
+
maintenance.
|
|
1706
|
+
|
|
1707
|
+
```typescript
|
|
1708
|
+
function invalidateEntitlementsCacheSafe(userId: string): void
|
|
1709
|
+
```
|
|
1710
|
+
|
|
1711
|
+
- `userId` — The user whose cached plan key must be dropped.
|
|
1712
|
+
|
|
1713
|
+
##### `utilities.normalizeEmail(email)`
|
|
1714
|
+
|
|
1715
|
+
Normalizes an email address for storage and lookup so case/whitespace
|
|
1716
|
+
variants of the same mailbox cannot bypass the `email` UNIQUE constraint
|
|
1717
|
+
(e.g. `Foo@Example.com` vs `foo@example.com`).
|
|
1718
|
+
|
|
1719
|
+
Trims surrounding whitespace and lowercases the whole address. While the
|
|
1720
|
+
local-part is technically case-sensitive per RFC 5321, every mainstream
|
|
1721
|
+
provider treats it case-insensitively, and the account-takeover risk of
|
|
1722
|
+
allowing variants far outweighs that pedantry. Both the local signup
|
|
1723
|
+
(`create`) and OAuth (`logInOAuth`) write paths MUST use this so the stored
|
|
1724
|
+
column and every collision lookup compare apples to apples.
|
|
1725
|
+
|
|
1726
|
+
```typescript
|
|
1727
|
+
function normalizeEmail(email: string | null | undefined): string | undefined
|
|
1728
|
+
```
|
|
1729
|
+
|
|
1730
|
+
- `email` — The raw email address (or null/undefined).
|
|
1731
|
+
|
|
1732
|
+
**Returns:** The normalized email, or `undefined` when the input is empty/blank.
|
|
1733
|
+
|
|
1734
|
+
##### `utilities.notify(options)`
|
|
1735
|
+
|
|
1736
|
+
Sends push notifications to all of a user's devices except the current one. Retrieves devices
|
|
1737
|
+
with active push subscriptions via the bonded DeviceService, then sends each notification via
|
|
1738
|
+
`@molecule/api-push-notifications`. Silently no-ops if either dependency is unavailable.
|
|
1739
|
+
|
|
1740
|
+
```typescript
|
|
1741
|
+
function notify({
|
|
1742
|
+
userId,
|
|
1743
|
+
deviceId,
|
|
1744
|
+
title,
|
|
1745
|
+
titleKey,
|
|
1746
|
+
body,
|
|
1747
|
+
bodyKey,
|
|
1748
|
+
}: {
|
|
1749
|
+
userId: string
|
|
1750
|
+
deviceId?: string
|
|
1751
|
+
title: string
|
|
1752
|
+
titleKey?: string
|
|
1753
|
+
body: string
|
|
1754
|
+
bodyKey?: string
|
|
1755
|
+
}): Promise<void>
|
|
1756
|
+
```
|
|
591
1757
|
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
- `
|
|
595
|
-
- `
|
|
596
|
-
- `
|
|
597
|
-
- `
|
|
598
|
-
- `
|
|
599
|
-
- `z.globalRegistry` — const
|
|
600
|
-
- `z.GlobalMeta` — interface
|
|
601
|
-
- `z.registry` — function
|
|
602
|
-
- `z.config` — function
|
|
603
|
-
- `z.$output` — const
|
|
604
|
-
- `z.$input` — const
|
|
605
|
-
- `z.$brand` — const
|
|
606
|
-
- `z.clone` — function
|
|
607
|
-
- `z.regexes` — namespace
|
|
608
|
-
- `z.treeifyError` — function
|
|
609
|
-
- `z.prettifyError` — function
|
|
610
|
-
- `z.formatError` — function
|
|
611
|
-
- `z.flattenError` — function
|
|
612
|
-
- `z.TimePrecision` — const
|
|
613
|
-
- `z.util` — namespace
|
|
614
|
-
- `z.NEVER` — const: A special constant with type `never`
|
|
615
|
-
- `z.toJSONSchema` — function
|
|
616
|
-
- `z.fromJSONSchema` — function: Converts a JSON Schema to a Zod schema. This function should be considered semi-experimental. It's behavior is liable to change.
|
|
617
|
-
- `z.locales` — namespace
|
|
618
|
-
- `z.ZodISODateTime` — interface
|
|
619
|
-
- `z.ZodISODate` — interface
|
|
620
|
-
- `z.ZodISOTime` — interface
|
|
621
|
-
- `z.ZodISODuration` — interface
|
|
622
|
-
- `z.iso` — namespace
|
|
623
|
-
- `z.ZodCoercedString` — interface
|
|
624
|
-
- `z.ZodCoercedNumber` — interface
|
|
625
|
-
- `z.ZodCoercedBigInt` — interface
|
|
626
|
-
- `z.ZodCoercedBoolean` — interface
|
|
627
|
-
- `z.ZodCoercedDate` — interface
|
|
628
|
-
- `z.coerce` — namespace
|
|
629
|
-
- `z.string` — function
|
|
630
|
-
- `z.email` — function
|
|
631
|
-
- `z.guid` — function
|
|
632
|
-
- `z.uuid` — function
|
|
633
|
-
- `z.uuidv4` — function
|
|
634
|
-
- `z.uuidv6` — function
|
|
635
|
-
- `z.uuidv7` — function
|
|
636
|
-
- `z.url` — function
|
|
637
|
-
- `z.httpUrl` — function
|
|
638
|
-
- `z.emoji` — function
|
|
639
|
-
- `z.nanoid` — function
|
|
640
|
-
- `z.cuid` — function: Validates a CUID v1 string.
|
|
641
|
-
- `z.cuid2` — function
|
|
642
|
-
- `z.ulid` — function
|
|
643
|
-
- `z.xid` — function
|
|
644
|
-
- `z.ksuid` — function
|
|
645
|
-
- `z.ipv4` — function
|
|
646
|
-
- `z.mac` — function
|
|
647
|
-
- `z.ipv6` — function
|
|
648
|
-
- `z.cidrv4` — function
|
|
649
|
-
- `z.cidrv6` — function
|
|
650
|
-
- `z.base64` — function
|
|
651
|
-
- `z.base64url` — function
|
|
652
|
-
- `z.e164` — function
|
|
653
|
-
- `z.jwt` — function
|
|
654
|
-
- `z.stringFormat` — function
|
|
655
|
-
- `z.hostname` — function
|
|
656
|
-
- `z.hex` — function
|
|
657
|
-
- `z.hash` — function
|
|
658
|
-
- `z.number` — function
|
|
659
|
-
- `z.int` — function
|
|
660
|
-
- `z.float32` — function
|
|
661
|
-
- `z.float64` — function
|
|
662
|
-
- `z.int32` — function
|
|
663
|
-
- `z.uint32` — function
|
|
664
|
-
- `z.boolean` — function
|
|
665
|
-
- `z.bigint` — function
|
|
666
|
-
- `z.int64` — function
|
|
667
|
-
- `z.uint64` — function
|
|
668
|
-
- `z.symbol` — function
|
|
669
|
-
- `z.any` — function
|
|
670
|
-
- `z.unknown` — function
|
|
671
|
-
- `z.never` — function
|
|
672
|
-
- `z.date` — function
|
|
673
|
-
- `z.array` — function
|
|
674
|
-
- `z.keyof` — function
|
|
675
|
-
- `z.object` — function
|
|
676
|
-
- `z.strictObject` — function
|
|
677
|
-
- `z.looseObject` — function
|
|
678
|
-
- `z.union` — function
|
|
679
|
-
- `z.xor` — function: Creates an exclusive union (XOR) where exactly one option must match.
|
|
680
|
-
- `z.discriminatedUnion` — function
|
|
681
|
-
- `z.intersection` — function
|
|
682
|
-
- `z.tuple` — function
|
|
683
|
-
- `z.record` — function
|
|
684
|
-
- `z.partialRecord` — function
|
|
685
|
-
- `z.looseRecord` — function
|
|
686
|
-
- `z.map` — function
|
|
687
|
-
- `z.set` — function
|
|
688
|
-
- `z.nativeEnum` — function
|
|
689
|
-
- `z.literal` — function
|
|
690
|
-
- `z.file` — function
|
|
691
|
-
- `z.transform` — function
|
|
692
|
-
- `z.optional` — function
|
|
693
|
-
- `z.exactOptional` — function
|
|
694
|
-
- `z.nullable` — function
|
|
695
|
-
- `z.nullish` — function
|
|
696
|
-
- `z._default` — function
|
|
697
|
-
- `z.prefault` — function
|
|
698
|
-
- `z.nonoptional` — function
|
|
699
|
-
- `z.success` — function
|
|
700
|
-
- `z.nan` — function
|
|
701
|
-
- `z.pipe` — function
|
|
702
|
-
- `z.codec` — function
|
|
703
|
-
- `z.invertCodec` — function
|
|
704
|
-
- `z.readonly` — function
|
|
705
|
-
- `z.templateLiteral` — function
|
|
706
|
-
- `z.lazy` — function
|
|
707
|
-
- `z.promise` — function
|
|
708
|
-
- `z._function` — function
|
|
709
|
-
- `z.check` — function
|
|
710
|
-
- `z.custom` — function
|
|
711
|
-
- `z.refine` — function
|
|
712
|
-
- `z.superRefine` — function
|
|
713
|
-
- `z.json` — function
|
|
714
|
-
- `z.preprocess` — function
|
|
715
|
-
- `z.ZodStandardSchemaWithJSON` — type
|
|
716
|
-
- `z.ZodType` — interface
|
|
717
|
-
- `z._ZodType` — interface
|
|
718
|
-
- `z._ZodString` — interface
|
|
719
|
-
- `z.ZodString` — interface
|
|
720
|
-
- `z.ZodStringFormat` — interface
|
|
721
|
-
- `z.ZodEmail` — interface
|
|
722
|
-
- `z.ZodGUID` — interface
|
|
723
|
-
- `z.ZodUUID` — interface
|
|
724
|
-
- `z.ZodURL` — interface
|
|
725
|
-
- `z.ZodEmoji` — interface
|
|
726
|
-
- `z.ZodNanoID` — interface
|
|
727
|
-
- `z.ZodCUID` — interface
|
|
728
|
-
- `z.ZodCUID2` — interface
|
|
729
|
-
- `z.ZodULID` — interface
|
|
730
|
-
- `z.ZodXID` — interface
|
|
731
|
-
- `z.ZodKSUID` — interface
|
|
732
|
-
- `z.ZodIPv4` — interface
|
|
733
|
-
- `z.ZodMAC` — interface
|
|
734
|
-
- `z.ZodIPv6` — interface
|
|
735
|
-
- `z.ZodCIDRv4` — interface
|
|
736
|
-
- `z.ZodCIDRv6` — interface
|
|
737
|
-
- `z.ZodBase64` — interface
|
|
738
|
-
- `z.ZodBase64URL` — interface
|
|
739
|
-
- `z.ZodE164` — interface
|
|
740
|
-
- `z.ZodJWT` — interface
|
|
741
|
-
- `z.ZodCustomStringFormat` — interface
|
|
742
|
-
- `z._ZodNumber` — interface
|
|
743
|
-
- `z.ZodNumber` — interface
|
|
744
|
-
- `z.ZodNumberFormat` — interface
|
|
745
|
-
- `z.ZodInt` — interface
|
|
746
|
-
- `z.ZodFloat32` — interface
|
|
747
|
-
- `z.ZodFloat64` — interface
|
|
748
|
-
- `z.ZodInt32` — interface
|
|
749
|
-
- `z.ZodUInt32` — interface
|
|
750
|
-
- `z._ZodBoolean` — interface
|
|
751
|
-
- `z.ZodBoolean` — interface
|
|
752
|
-
- `z._ZodBigInt` — interface
|
|
753
|
-
- `z.ZodBigInt` — interface
|
|
754
|
-
- `z.ZodBigIntFormat` — interface
|
|
755
|
-
- `z.ZodSymbol` — interface
|
|
756
|
-
- `z.ZodUndefined` — interface
|
|
757
|
-
- `z.undefined` — function
|
|
758
|
-
- `z.ZodNull` — interface
|
|
759
|
-
- `z.null` — function
|
|
760
|
-
- `z.ZodAny` — interface
|
|
761
|
-
- `z.ZodUnknown` — interface
|
|
762
|
-
- `z.ZodNever` — interface
|
|
763
|
-
- `z.ZodVoid` — interface
|
|
764
|
-
- `z.void` — function
|
|
765
|
-
- `z._ZodDate` — interface
|
|
766
|
-
- `z.ZodDate` — interface
|
|
767
|
-
- `z.ZodArray` — interface
|
|
768
|
-
- `z.SafeExtendShape` — type
|
|
769
|
-
- `z.ZodObject` — interface
|
|
770
|
-
- `z.ZodUnion` — interface
|
|
771
|
-
- `z.ZodXor` — interface
|
|
772
|
-
- `z.ZodDiscriminatedUnion` — interface
|
|
773
|
-
- `z.ZodIntersection` — interface
|
|
774
|
-
- `z.ZodTuple` — interface
|
|
775
|
-
- `z.ZodRecord` — interface
|
|
776
|
-
- `z.ZodMap` — interface
|
|
777
|
-
- `z.ZodSet` — interface
|
|
778
|
-
- `z.ZodEnum` — interface
|
|
779
|
-
- `z.enum` — function
|
|
780
|
-
- `z.ZodLiteral` — interface
|
|
781
|
-
- `z.ZodFile` — interface
|
|
782
|
-
- `z.ZodTransform` — interface
|
|
783
|
-
- `z.ZodOptional` — interface
|
|
784
|
-
- `z.ZodExactOptional` — interface
|
|
785
|
-
- `z.ZodNullable` — interface
|
|
786
|
-
- `z.ZodDefault` — interface
|
|
787
|
-
- `z.ZodPrefault` — interface
|
|
788
|
-
- `z.ZodNonOptional` — interface
|
|
789
|
-
- `z.ZodSuccess` — interface
|
|
790
|
-
- `z.ZodCatch` — interface
|
|
791
|
-
- `z.catch` — function
|
|
792
|
-
- `z.ZodNaN` — interface
|
|
793
|
-
- `z.ZodPipe` — interface
|
|
794
|
-
- `z.ZodCodec` — interface
|
|
795
|
-
- `z.ZodPreprocess` — interface
|
|
796
|
-
- `z.ZodReadonly` — interface
|
|
797
|
-
- `z.ZodTemplateLiteral` — interface
|
|
798
|
-
- `z.ZodLazy` — interface
|
|
799
|
-
- `z.ZodPromise` — interface
|
|
800
|
-
- `z.ZodFunction` — interface
|
|
801
|
-
- `z.function` — function
|
|
802
|
-
- `z.ZodCustom` — interface
|
|
803
|
-
- `z.describe` — const
|
|
804
|
-
- `z.meta` — const
|
|
805
|
-
- `z.instanceof` — function
|
|
806
|
-
- `z.stringbool` — const
|
|
807
|
-
- `z.ZodJSONSchemaInternals` — interface
|
|
808
|
-
- `z.ZodJSONSchema` — interface
|
|
809
|
-
- `z.lt` — function
|
|
810
|
-
- `z.lte` — function
|
|
811
|
-
- `z.gt` — function
|
|
812
|
-
- `z.gte` — function
|
|
813
|
-
- `z.positive` — function
|
|
814
|
-
- `z.negative` — function
|
|
815
|
-
- `z.nonpositive` — function
|
|
816
|
-
- `z.nonnegative` — function
|
|
817
|
-
- `z.multipleOf` — function
|
|
818
|
-
- `z.maxSize` — function
|
|
819
|
-
- `z.minSize` — function
|
|
820
|
-
- `z.size` — function
|
|
821
|
-
- `z.maxLength` — function
|
|
822
|
-
- `z.minLength` — function
|
|
823
|
-
- `z.length` — function
|
|
824
|
-
- `z.regex` — function
|
|
825
|
-
- `z.lowercase` — function
|
|
826
|
-
- `z.uppercase` — function
|
|
827
|
-
- `z.includes` — function
|
|
828
|
-
- `z.startsWith` — function
|
|
829
|
-
- `z.endsWith` — function
|
|
830
|
-
- `z.property` — function
|
|
831
|
-
- `z.mime` — function
|
|
832
|
-
- `z.overwrite` — function
|
|
833
|
-
- `z.normalize` — function
|
|
834
|
-
- `z.trim` — function
|
|
835
|
-
- `z.toLowerCase` — function
|
|
836
|
-
- `z.toUpperCase` — function
|
|
837
|
-
- `z.slugify` — function
|
|
838
|
-
- `z.RefinementCtx` — interface
|
|
839
|
-
- `z.ZodIssue` — type
|
|
840
|
-
- `z.ZodError` — interface: An Error-like class used to store Zod validation issues.
|
|
841
|
-
- `z.ZodRealError` — const
|
|
842
|
-
- `z.ZodFlattenedError` — type
|
|
843
|
-
- `z.ZodFormattedError` — type
|
|
844
|
-
- `z.ZodErrorMap` — interface
|
|
845
|
-
- `z.IssueData` — type
|
|
846
|
-
- `z.ZodSafeParseResult` — type
|
|
847
|
-
- `z.ZodSafeParseSuccess` — type
|
|
848
|
-
- `z.ZodSafeParseError` — type
|
|
849
|
-
- `z.parse` — const
|
|
850
|
-
- `z.parseAsync` — const
|
|
851
|
-
- `z.safeParse` — const
|
|
852
|
-
- `z.safeParseAsync` — const
|
|
853
|
-
- `z.encode` — const
|
|
854
|
-
- `z.decode` — const
|
|
855
|
-
- `z.encodeAsync` — const
|
|
856
|
-
- `z.decodeAsync` — const
|
|
857
|
-
- `z.safeEncode` — const
|
|
858
|
-
- `z.safeDecode` — const
|
|
859
|
-
- `z.safeEncodeAsync` — const
|
|
860
|
-
- `z.safeDecodeAsync` — const
|
|
861
|
-
- `z.setErrorMap` — function
|
|
862
|
-
- `z.getErrorMap` — function
|
|
863
|
-
- `z.TypeOf` — type
|
|
864
|
-
- `z.Infer` — type
|
|
865
|
-
- `z.ZodFirstPartySchemaTypes` — type
|
|
866
|
-
- `z.ZodIssueCode` — const
|
|
867
|
-
- `z.inferFlattenedErrors` — type
|
|
868
|
-
- `z.inferFormattedError` — type
|
|
869
|
-
- `z.BRAND` — type: Use `z.$brand` instead
|
|
870
|
-
- `z.ZodTypeAny` — interface
|
|
871
|
-
- `z.ZodSchema` — interface
|
|
872
|
-
- `z.Schema` — interface
|
|
873
|
-
- `z.ZodRawShape` — type: Included for Zod 3 compatibility
|
|
874
|
-
- `z.ZodFirstPartyTypeKind` — enum
|
|
1758
|
+
- `options` — Notification options.
|
|
1759
|
+
- `options.userId` — The user whose devices should receive the notification.
|
|
1760
|
+
- `options.deviceId` — The current device ID to exclude from notifications.
|
|
1761
|
+
- `options.title` — The notification title text.
|
|
1762
|
+
- `options.titleKey` — The i18n key for the title (sent as notification data for client-side localization).
|
|
1763
|
+
- `options.body` — The notification body text.
|
|
1764
|
+
- `options.bodyKey` — The i18n key for the body (sent as notification data for client-side localization).
|
|
875
1765
|
|
|
876
1766
|
## Injection Notes
|
|
877
1767
|
|