very-express 0.8.3 → 0.8.5
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/AGENTS.md +21 -3
- package/dist/index.js +890 -396
- package/dist/templates/_middlewares/Authentication.ts +49 -32
- package/dist/templates/_middlewares/UserContext.ts +1 -18
- package/dist/templates/_projectSettings/env +14 -0
- package/dist/templates/_projectSettings/package.json +1 -0
- package/dist/templates/_routes/FirebaseAuthUI.gen.ts +20 -0
- package/dist/templates/_routes/LoginUI.ts +484 -4
- package/dist/templates/_services/MongooseRepositoryAdapter.ts +67 -5
- package/dist/templates/_services/TypeOrmRepositoryAdapter.ts +70 -4
- package/dist/templates/_services/oauth/OAuthProfileMap.ts +27 -8
- package/dist/templates/_services/oauth/OAuthStrategyService.ts +5 -5
- package/dist/templates/_types/auth.ts +27 -0
- package/dist/templates/_types/vex/VexRepository.ts +21 -0
- package/dist/templates/jsonSchema/User.json +8 -1
- package/dist/templates/jsonSchema/UserAuthProfiles.json +13 -5
- package/dist/templates/root/public/js/deleteaccount.js +80 -0
- package/docs/appGenerated/auth.md +83 -22
- package/docs/architecture/authAndRouting.md +9 -2
- package/docs/architecture/databaseTargets.md +1 -1
- package/docs/features/accountDeletion.md +149 -0
- package/docs/features/auditFields.md +2 -32
- package/docs/releaseNote/v0-8-4.md +131 -0
- package/docs/releaseNote/v0-8-5.md +98 -0
- package/docs/superpowers/specs/2026-09-22-usercontext-audit-enforcement-design.md +9 -9
- package/docs/vexJsonSchema.md +11 -1
- package/package.json +1 -1
- package/vex.config.json +4 -1
- /package/docs/{ForeignKey.md → features/ForeignKey.md} +0 -0
- /package/docs/{apiUsage.md → features/apiUsage.md} +0 -0
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
# Account deletion (tombstone)
|
|
2
|
+
|
|
3
|
+
Self-service account deletion for generated apps: `POST /api/auth/delete-account` plus a
|
|
4
|
+
`/delete_account` page. The account is **soft-deleted** — the row survives so business tables can
|
|
5
|
+
keep their required foreign keys — and its credentials and personal data are erased.
|
|
6
|
+
|
|
7
|
+
This covers the identity domain only. Business rows, uploaded objects and third-party identities
|
|
8
|
+
(Firebase, etc.) belong to the application.
|
|
9
|
+
|
|
10
|
+
## Configuration
|
|
11
|
+
|
|
12
|
+
```json
|
|
13
|
+
{
|
|
14
|
+
"auth": { "deleteAccount": true },
|
|
15
|
+
"app": { "showSoftDeleted": false }
|
|
16
|
+
}
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
| Option | Default | Meaning |
|
|
20
|
+
|---|---|---|
|
|
21
|
+
| `auth.deleteAccount` | `true` | Emit the endpoint, the service and the page. Also requires auth to be enabled — a deletion endpoint that cannot authenticate a caller could only delete the wrong account. |
|
|
22
|
+
| `app.showSoftDeleted` | `false` | Lifts the adapter's soft-delete filter app-wide, making tombstones visible (and writable) again. Loud on every generation run. |
|
|
23
|
+
|
|
24
|
+
Both are read with `??`, so an explicit `false` is never swallowed. There is no `deleteAccountPlaceholder`:
|
|
25
|
+
the tombstone label is the constant `"Deleted user"`.
|
|
26
|
+
|
|
27
|
+
## Declaring soft delete
|
|
28
|
+
|
|
29
|
+
Tag one boolean property with `x-vexData: "softDelete"`:
|
|
30
|
+
|
|
31
|
+
```jsonc
|
|
32
|
+
"properties": {
|
|
33
|
+
"deleted": {
|
|
34
|
+
"type": "boolean",
|
|
35
|
+
"default": false,
|
|
36
|
+
"x-vexData": "softDelete",
|
|
37
|
+
"description": "Framework-managed; never client-writable."
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
"required": ["deleted"]
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
The framework then owns that field:
|
|
44
|
+
|
|
45
|
+
- **Not client-writable.** `collectRequestManagedFields()` omits it, so it never appears in a
|
|
46
|
+
generated request body (`PayloadUser`), and the OpenAPI schema stops advertising it. A client
|
|
47
|
+
cannot soft-delete, resurrect or pre-tombstone a row through the CRUD API.
|
|
48
|
+
- **Hidden by default.** The adapters add `marker IS NOT TRUE` (`$ne: true` on Mongo) to every read
|
|
49
|
+
and write filter, so a tombstoned row is unreachable without any call-site change.
|
|
50
|
+
- **Owned writes.** `softDelete(id, data)` writes the marker through the normal update-phase
|
|
51
|
+
machinery, so `updatedAt` / `updatedBy` record who deleted the row and when. No `deletedAt` /
|
|
52
|
+
`deletedBy` columns are added.
|
|
53
|
+
|
|
54
|
+
Validation (generation-time, fails loud):
|
|
55
|
+
|
|
56
|
+
- at most **one** marker per document;
|
|
57
|
+
- it must be `"type": "boolean"`, `"default": false` and **required**.
|
|
58
|
+
|
|
59
|
+
The required/default rules are load-bearing. The hide-filter is `marker IS NOT TRUE`, so a nullable
|
|
60
|
+
column would drop every row written before it existed. `required` + `default: false` produce a
|
|
61
|
+
`boolean NOT NULL DEFAULT false` column.
|
|
62
|
+
|
|
63
|
+
## Query semantics
|
|
64
|
+
|
|
65
|
+
`app.showSoftDeleted` is a **generation-time constant** baked into `VexFieldRegistry.gen.ts`, so no
|
|
66
|
+
request can flip it. There is deliberately no soft-delete key in `Filter<T>`: `Filter` is the shape
|
|
67
|
+
of every HTTP `filter` body, so a filter-level escape hatch would be a client-side bypass.
|
|
68
|
+
|
|
69
|
+
Internal callers use dedicated methods instead:
|
|
70
|
+
|
|
71
|
+
```ts
|
|
72
|
+
softDelete(id, data?) // write the marker + redaction in one update
|
|
73
|
+
findOneWithDeleted(filter, join, select) // skip ONLY the soft-delete term; ownership still applies
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
The hide term is **not** applied to relation loading, which is why the marker is a plain column and
|
|
77
|
+
not TypeORM's `@DeleteDateColumn` — the decorator nulls relation loads, and retained conversations
|
|
78
|
+
must still resolve their author to show "Deleted user".
|
|
79
|
+
|
|
80
|
+
> Enabling `app.showSoftDeleted` is a blunt instrument: it applies to every entity, including ones
|
|
81
|
+
> added later, and it removes the write protection that keeps a tombstone's audit fields frozen.
|
|
82
|
+
|
|
83
|
+
## The token guard
|
|
84
|
+
|
|
85
|
+
`Authentication.middleware` verifies the JWT signature and nothing else, so a deleted user's
|
|
86
|
+
unexpired access token would keep working for the rest of its lifetime. The generated
|
|
87
|
+
`AccountStateGuard` closes that gap.
|
|
88
|
+
|
|
89
|
+
It reads the marker **value** rather than relying on the row being hidden, so the answer holds under
|
|
90
|
+
`app.showSoftDeleted` — a visibility-based check would call every tombstone alive. It uses
|
|
91
|
+
`findOneWithDeleted` so the ownership filter still applies, and treats a missing marker as live
|
|
92
|
+
(`!== true`), matching the adapters.
|
|
93
|
+
|
|
94
|
+
Every other auth path is already fail-closed: refresh and the OAuth exchange look the user up
|
|
95
|
+
through a soft-filtered read, local login compares an email that has been wiped to NULL, and the
|
|
96
|
+
OAuth callback cannot re-bind because the profile rows are gone.
|
|
97
|
+
|
|
98
|
+
The guard is emitted as a no-op when the identity document declares no marker, so the middleware's
|
|
99
|
+
import always resolves and projects without soft delete pay nothing.
|
|
100
|
+
|
|
101
|
+
The exported middleware is **synchronous** on purpose. Both call sites are express chains, and
|
|
102
|
+
Express 4 does not catch a rejected promise — an `async` handler would turn every failure into an
|
|
103
|
+
unhandled rejection. The async work lives in `handle()` and every outcome is routed to `next()`.
|
|
104
|
+
|
|
105
|
+
## Deletion flow
|
|
106
|
+
|
|
107
|
+
`AccountDeletionService.deleteSelf()` takes the identity from `UserContext` only; no id is ever
|
|
108
|
+
accepted from a path or body, so one account cannot delete another. It is idempotent: a repeat call
|
|
109
|
+
returns `alreadyDeleted` without re-stamping the row.
|
|
110
|
+
|
|
111
|
+
Order is load-bearing:
|
|
112
|
+
|
|
113
|
+
1. **Credentials first** — auth profiles, roles, sessions. Tombstoning first would leave the OAuth
|
|
114
|
+
path able to resolve the account through `provider` / `oauthId`, i.e. a deleted user could log
|
|
115
|
+
back into their own tombstone.
|
|
116
|
+
2. **Tombstone last** — `softDelete(userId, TOMBSTONE)`. A mid-flight failure therefore leaves a
|
|
117
|
+
live account that can simply be retried.
|
|
118
|
+
|
|
119
|
+
The tombstone redacts the standard personal fields: `email` → NULL, `name` → `"Deleted user"`,
|
|
120
|
+
`locale` / `profileErrors` → NULL, `active` → false. `email` is NULL rather than `""` because `User`
|
|
121
|
+
declares a unique index on it — a second deletion would collide on the empty string, and the same
|
|
122
|
+
address must be able to register a fresh account immediately.
|
|
123
|
+
|
|
124
|
+
The payload is derived from the project's own schema, so absent or renamed fields are never emitted
|
|
125
|
+
and audit / identity fields are skipped.
|
|
126
|
+
|
|
127
|
+
## The page
|
|
128
|
+
|
|
129
|
+
`GET /delete_account`, served from `LoginUI` rather than as a static file — `express.static("public")`
|
|
130
|
+
would expose `public/delete_account.html` at `/delete_account.html`, not at the route the store
|
|
131
|
+
listing points people at.
|
|
132
|
+
|
|
133
|
+
`LoginUIConfig.deleteAccount` is **optional and defaults to true**, because an app's `server.ts` is
|
|
134
|
+
generated once and is not overwritten on later runs; a required field would stop such a project from
|
|
135
|
+
compiling.
|
|
136
|
+
|
|
137
|
+
The copy is explicit that this is a tombstone, not an erasure. The client clears its stored tokens on
|
|
138
|
+
success: the account is gone the moment the tombstone is written, so keeping a token the server now
|
|
139
|
+
rejects would only produce confusing 401s.
|
|
140
|
+
|
|
141
|
+
## Integration notes
|
|
142
|
+
|
|
143
|
+
- **Migration.** Adding the marker adds a column. `SQL_SYNCHRONIZE=true` covers development;
|
|
144
|
+
production needs `ALTER TABLE "user" ADD COLUMN "deleted" boolean NOT NULL DEFAULT false`.
|
|
145
|
+
- **`allowOverwrite: false`.** A project that pinned `UserModel.gen.ts` / `User.gen.ts` must add the
|
|
146
|
+
column by hand, or clear the `.vex/meta.json` entry for those files.
|
|
147
|
+
- **Per-request cost.** The guard costs one narrow indexed read per authenticated request.
|
|
148
|
+
- **Third-party identity.** vex cannot delete a Firebase account. Do it from the app, and capture the
|
|
149
|
+
provider uid *before* calling the service.
|
|
@@ -9,7 +9,7 @@ Generated apps declare `createdBy` / `createdAt` / `updatedBy` / `updatedAt` in
|
|
|
9
9
|
but nothing ever writes them — `grep -rn "createdBy\|updatedBy" src/` in the generator is empty.
|
|
10
10
|
The columns exist in the entities and stay NULL forever.
|
|
11
11
|
|
|
12
|
-
Consequences seen in
|
|
12
|
+
Consequences seen in a production deployment:
|
|
13
13
|
|
|
14
14
|
- A custom controller checking `findOne({ _id, createdBy: request.user._id })` never matches → `404` on `DELETE /api/job/{id}`.
|
|
15
15
|
- Timeline ordering bugs, because `createdAt` is also never written.
|
|
@@ -237,36 +237,6 @@ Validation runs once, after every schema is loaded (`validateAuditFields()` in
|
|
|
237
237
|
`src/preprocess/auditFields.ts`, called from the pipeline), because R1/R2 are cross-document. All problems
|
|
238
238
|
are collected and reported together; `log.error` exits the generator, so a contradiction never reaches runtime.
|
|
239
239
|
|
|
240
|
-
## 7. Backward compatibility
|
|
241
|
-
|
|
242
|
-
- Schema with no reserved keyword and no tag → nothing changes; generation output is identical.
|
|
243
|
-
- Schema with a literal `default` → unchanged.
|
|
244
|
-
- Existing generated apps: re-run `vex`; entities gain `nullable` audit columns if they were declared.
|
|
245
|
-
- Existing rows keep `createdBy = NULL`. This feature cannot backfill; a downstream app that needs
|
|
246
|
-
ownership on legacy rows must migrate data itself.
|
|
247
|
-
- Tokens issued before the upgrade still work (the `?? _id` fallback on refresh).
|
|
248
|
-
- The prototype branch's lowercase `x-format` values (`primary`, `uuid`, …) are **rejected** — this design
|
|
249
|
-
keeps the current capitalized values (`Primary`, `PrimaryUUID`, `UUID`, `UnixTimestamp`) and only adds
|
|
250
|
-
`Timestamp`.
|
|
251
|
-
|
|
252
|
-
### Breaking since v0.8.2
|
|
253
|
-
|
|
254
|
-
Three changes downstream apps must absorb when they re-run `vex`:
|
|
255
|
-
|
|
256
|
-
1. **The context module was renamed.** `DataIsolationContext.gen.ts` is gone; `UserContext.gen.ts` replaces
|
|
257
|
-
it. `cleanupStaleFiles()` removes the old file on the next run, so anything importing it by name breaks.
|
|
258
|
-
2. **A create without an identity now throws** where it previously wrote NULL. This affects
|
|
259
|
-
`onCreateUserId` only, and only a path that reaches `create()` with no authenticated request context
|
|
260
|
-
(seed scripts, background jobs, or a controller that does not mount `Authentication.middleware`).
|
|
261
|
-
3. **Data isolation becomes effective for the first time.** It used to be mounted only on entities declaring
|
|
262
|
-
`dataIsolation`, which in practice meant it rarely ran at all. It now has a context on every
|
|
263
|
-
authenticating controller. Ownership still wins over the caller's filter, so an endpoint that queries
|
|
264
|
-
*another* user's row by id will silently act on the caller's own row instead. Audit those endpoints per
|
|
265
|
-
app.
|
|
266
|
-
|
|
267
|
-
Downstream apps no longer need the `dataIsolation` config merely to get the ALS context mounted — that
|
|
268
|
-
coupling is gone. Use `dataIsolation` only when ownership filtering is actually wanted.
|
|
269
|
-
|
|
270
240
|
## 8. Mongoose
|
|
271
241
|
|
|
272
242
|
Implemented in parallel with TypeORM — same registry, same strip/inject phases, same values
|
|
@@ -281,7 +251,7 @@ schema — a keyword would otherwise become a literal string default.
|
|
|
281
251
|
|
|
282
252
|
- Soft delete.
|
|
283
253
|
- Backfilling existing NULL rows.
|
|
284
|
-
-
|
|
254
|
+
- An app whose ownership column points at a profile row rather than at `User`.
|
|
285
255
|
- Extending `useUserSchema: false` into a fully custom account model. Today `User.json` is copied from the
|
|
286
256
|
templates unconditionally and `localAuth` requires `useUserSchema: true`, so a custom identity schema is
|
|
287
257
|
not reachable yet; `x-vexData: "userId"` is the forward-compatible hook for it.
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
# v0.8.4
|
|
2
|
+
|
|
3
|
+
## Account deletion (tombstone)
|
|
4
|
+
|
|
5
|
+
Generated apps now get self-service account deletion: `POST /api/auth/delete-account` and a
|
|
6
|
+
`/delete_account` page, on by default.
|
|
7
|
+
|
|
8
|
+
The account is **soft-deleted**, not erased. The row survives so business tables can keep their
|
|
9
|
+
required foreign keys — a hard delete would orphan every `Job`, `ChatMessage` and `UserReport`
|
|
10
|
+
pointing at it — while credentials and personal data are removed.
|
|
11
|
+
|
|
12
|
+
| Step | Effect |
|
|
13
|
+
|---|---|
|
|
14
|
+
| Clear credentials | `UserAuthProfiles`, `UserRole`, `Session` rows for the user are hard-deleted |
|
|
15
|
+
| Tombstone last | `deleted: true`, `email` → NULL, `name` → `"Deleted user"`, `locale` / `profileErrors` → NULL, `active` → false |
|
|
16
|
+
|
|
17
|
+
Credentials are removed **before** the tombstone on purpose. Tombstoning first would leave the OAuth
|
|
18
|
+
path able to resolve the account through `provider` / `oauthId`, i.e. a deleted user could log back
|
|
19
|
+
into their own tombstone. Marker last also means a mid-flight failure leaves a live account that can
|
|
20
|
+
simply be retried.
|
|
21
|
+
|
|
22
|
+
`email` becomes `NULL`, never `""`: `User` declares a unique index on it, so a second deletion would
|
|
23
|
+
collide on the empty string, and the same address must be able to register a fresh account
|
|
24
|
+
immediately.
|
|
25
|
+
|
|
26
|
+
Deletion is idempotent — a repeat call changes nothing.
|
|
27
|
+
|
|
28
|
+
## Soft delete: `x-vexData: "softDelete"`
|
|
29
|
+
|
|
30
|
+
Soft delete is a general, opt-in schema feature, not a hard-coded user concept. Tag one boolean
|
|
31
|
+
property:
|
|
32
|
+
|
|
33
|
+
```jsonc
|
|
34
|
+
"deleted": { "type": "boolean", "default": false, "x-vexData": "softDelete" }
|
|
35
|
+
// and list it in the root "required" array
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
- **Not client-writable.** The marker is omitted from the generated request body (`PayloadUser`), so
|
|
39
|
+
a client cannot soft-delete, resurrect or pre-tombstone a row through the CRUD API.
|
|
40
|
+
- **Hidden by default.** The adapters add `marker IS NOT TRUE` (`$ne: true` on Mongo) to every read
|
|
41
|
+
and write filter. No call-site changes.
|
|
42
|
+
- **No new columns.** `softDelete(id, data?)` writes the marker through the normal update-phase
|
|
43
|
+
machinery, so `updatedAt` / `updatedBy` record who deleted the row and when. There is no
|
|
44
|
+
`deletedAt` / `deletedBy`.
|
|
45
|
+
- **Required + defaulted.** The marker must be `boolean`, `default: false` and `required`, producing
|
|
46
|
+
`boolean NOT NULL DEFAULT false`. A nullable marker would make the hide-filter drop every row
|
|
47
|
+
written before the column existed.
|
|
48
|
+
|
|
49
|
+
Internal callers use dedicated repository methods, never a filter key:
|
|
50
|
+
|
|
51
|
+
```ts
|
|
52
|
+
softDelete(id, data?) // write the marker + redaction in one update
|
|
53
|
+
findOneWithDeleted(filter, join, select) // skip ONLY the soft-delete term; ownership still applies
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
A soft-delete key inside `Filter<T>` was rejected deliberately: `Filter` is the shape of every HTTP
|
|
57
|
+
`filter` body, so it would have been a client-side bypass.
|
|
58
|
+
|
|
59
|
+
The hide term is **not** applied to relation loading — which is why the marker is a plain column and
|
|
60
|
+
not TypeORM's `@DeleteDateColumn`, whose relation-nulling would break retained conversations
|
|
61
|
+
rendering "Deleted user".
|
|
62
|
+
|
|
63
|
+
## Tokens of deleted accounts are rejected
|
|
64
|
+
|
|
65
|
+
`Authentication.middleware` verified the JWT signature and nothing else, so a deleted user's
|
|
66
|
+
unexpired access token kept working for the rest of its lifetime. A generated `AccountStateGuard`
|
|
67
|
+
now closes that gap.
|
|
68
|
+
|
|
69
|
+
It reads the marker **value** rather than relying on the row being hidden, so the answer holds even
|
|
70
|
+
under `app.showSoftDeleted`, where a visibility-based check would call every tombstone alive. It
|
|
71
|
+
treats a missing marker as live (`!== true`), matching the adapters.
|
|
72
|
+
|
|
73
|
+
Every other auth path was already fail-closed: refresh and the OAuth exchange look the user up
|
|
74
|
+
through a soft-filtered read, local login compares an email that is now NULL, and the OAuth callback
|
|
75
|
+
cannot re-bind because the profile rows are gone.
|
|
76
|
+
|
|
77
|
+
## Config
|
|
78
|
+
|
|
79
|
+
```jsonc
|
|
80
|
+
{
|
|
81
|
+
"app": { "showSoftDeleted": false },
|
|
82
|
+
"auth": { "deleteAccount": true }
|
|
83
|
+
}
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
- `auth.deleteAccount` — **default true** (opt-out). Also requires auth to be enabled: a deletion
|
|
87
|
+
endpoint that cannot authenticate a caller could only delete the wrong account. When false, no
|
|
88
|
+
endpoint, no service, no page.
|
|
89
|
+
- `app.showSoftDeleted` — **default false**. Lifts the adapter filter app-wide, making tombstones
|
|
90
|
+
visible and writable again. It warns on every generation run, because it is a blunt instrument: it
|
|
91
|
+
applies to every entity, including ones added later.
|
|
92
|
+
|
|
93
|
+
Both are read with `??`, so an explicit `false` is never swallowed.
|
|
94
|
+
|
|
95
|
+
## The `/delete_account` page
|
|
96
|
+
|
|
97
|
+
Served from `LoginUI` rather than as a static file — `express.static("public")` would expose
|
|
98
|
+
`public/delete_account.html` at `/delete_account.html`, not at the route a store listing points at.
|
|
99
|
+
Linked from `/`.
|
|
100
|
+
|
|
101
|
+
`LoginUIConfig.deleteAccount` is optional and defaults to true, because an app's `server.ts` is
|
|
102
|
+
generated once and is not overwritten on later runs; a required field would stop such a project from
|
|
103
|
+
compiling.
|
|
104
|
+
|
|
105
|
+
The client clears its stored tokens on success — the account is gone the moment the tombstone is
|
|
106
|
+
written, so keeping a token the server now rejects would only produce confusing 401s.
|
|
107
|
+
|
|
108
|
+
## Behaviour changes
|
|
109
|
+
|
|
110
|
+
- **Every authenticated request costs one extra indexed read** of the identity row when the identity
|
|
111
|
+
document is soft-deletable. Projects with no soft-delete marker are unaffected — the guard is
|
|
112
|
+
emitted as a no-op there.
|
|
113
|
+
- **A request carrying a soft-delete marker is rejected with `400 Invalid Request Body`**, matching
|
|
114
|
+
the existing `throw-on-extras` contract for other server-managed fields.
|
|
115
|
+
|
|
116
|
+
## After upgrading
|
|
117
|
+
|
|
118
|
+
Re-run the generator. Then:
|
|
119
|
+
|
|
120
|
+
1. **Add the column.** `useUserSchema` apps get `User.deleted` merged in automatically.
|
|
121
|
+
`SQL_SYNCHRONIZE=true` covers development; production needs
|
|
122
|
+
`ALTER TABLE "user" ADD COLUMN "deleted" boolean NOT NULL DEFAULT false`.
|
|
123
|
+
2. **Check `allowOverwrite: false`.** An app that pinned `UserModel.gen.ts` / `User.gen.ts` must add
|
|
124
|
+
the column by hand, or clear those `.vex/meta.json` entries, or the SQL column will not exist.
|
|
125
|
+
3. **Business rows and uploaded objects are the app's job.** This release covers the identity domain
|
|
126
|
+
only. Third-party identities too: vex cannot delete a Firebase account — capture the provider uid
|
|
127
|
+
*before* calling the service.
|
|
128
|
+
4. **To disable it entirely:** `"auth": { "deleteAccount": false }`.
|
|
129
|
+
|
|
130
|
+
Guides: [`docs/features/accountDeletion.md`](../features/accountDeletion.md),
|
|
131
|
+
[`docs/vexJsonSchema.md`](../vexJsonSchema.md).
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
# v0.8.5
|
|
2
|
+
|
|
3
|
+
## Breaking: the identity key is renamed, and now enforced
|
|
4
|
+
|
|
5
|
+
`UserAuthProfiles.oauthId` is **`providerUserId`**, and `(provider, providerUserId)` is a **composite
|
|
6
|
+
unique index**. The value never changed — it is the subject within `provider`'s namespace — but the old
|
|
7
|
+
name claimed a protocol the field does not require, and nothing stopped two rows claiming one identity.
|
|
8
|
+
|
|
9
|
+
Re-run the generator (migration `v0.8.5` rewrites the schema, and any `uniqueIndex` entry naming the old
|
|
10
|
+
field), then migrate the database:
|
|
11
|
+
|
|
12
|
+
```sql
|
|
13
|
+
ALTER TABLE "userauthprofiles" RENAME COLUMN "oauthId" TO "providerUserId";
|
|
14
|
+
ALTER TABLE "userauthprofiles" ADD CONSTRAINT "unique_provider_providerUserId"
|
|
15
|
+
UNIQUE ("provider", "providerUserId");
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
Look for existing duplicates before adding the constraint:
|
|
19
|
+
|
|
20
|
+
```sql
|
|
21
|
+
SELECT provider, "providerUserId", COUNT(*) FROM "userauthprofiles"
|
|
22
|
+
GROUP BY 1,2 HAVING COUNT(*) > 1;
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
`SQL_SYNCHRONIZE=true` covers development. App code that reads `oauthId` must be updated.
|
|
26
|
+
|
|
27
|
+
## New: Firebase as a sign-in option
|
|
28
|
+
|
|
29
|
+
`auth.firebase: true` generates `POST /api/auth/firebase` and a button on the generated `/login` page.
|
|
30
|
+
The endpoint takes a Firebase ID token and answers with the vex token pair:
|
|
31
|
+
|
|
32
|
+
```
|
|
33
|
+
POST /api/auth/firebase { "idToken": "<Firebase ID token>" }
|
|
34
|
+
200 { "result": { accessToken, accessTokenIndex, refreshToken, refreshTokenIndex, isNewUser } }
|
|
35
|
+
503 no usable service account 401 invalid or expired token
|
|
36
|
+
400 the sign-in has no upstream IdP 409 the sign-in carries no email
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
The identity written is the **upstream** IdP's — `("google", <Google sub>)` for a Google sign-in, the
|
|
40
|
+
same pair `passport-google-oauth20` writes, so both doors resolve to one `User` row.
|
|
41
|
+
|
|
42
|
+
```jsonc
|
|
43
|
+
"auth": { "firebase": true }
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
That boolean is the only switch. Everything else is environment. Four required values, under the names a
|
|
47
|
+
deployment already has; two variables earlier drafts carried are gone because nothing reads them
|
|
48
|
+
(`FIREBASE_APP_ID` — Firebase Auth takes no appId — and a file-path alternative to the service account).
|
|
49
|
+
|
|
50
|
+
| Variable | Used by |
|
|
51
|
+
|---|---|
|
|
52
|
+
| `FIREBASE_SERVICE_ACCOUNT_JSON` | server — `firebase-admin` |
|
|
53
|
+
| `FIREBASE_WEB_API_KEY`, `FIREBASE_WEB_AUTH_DOMAIN`, `FIREBASE_WEB_PROJECT_ID` | login page — public, by design |
|
|
54
|
+
| `FIREBASE_PROVIDERS` | optional; which buttons `/login` renders; default `google` |
|
|
55
|
+
|
|
56
|
+
Design record, including the identity rules and the reasons behind them:
|
|
57
|
+
[`docs/plan/spec-firebase-auth.md`](../plan/spec-firebase-auth.md).
|
|
58
|
+
|
|
59
|
+
## Also fixed
|
|
60
|
+
|
|
61
|
+
- **A project with `localAuth: false` and any other sign-in path generated a controller that did not
|
|
62
|
+
compile** — `exchangeToken` / `refreshToken` call `this.userRepo`, which was only emitted when
|
|
63
|
+
`localAuth` was on. The getter is now emitted whenever auth is enabled. Reachable before this release
|
|
64
|
+
with `oauthProviders` alone; the Firebase-only configuration is the same shape.
|
|
65
|
+
|
|
66
|
+
## After upgrading
|
|
67
|
+
|
|
68
|
+
1. **`npm install`** — `firebase-admin` is a dependency of the generated app (the browser SDK is loaded
|
|
69
|
+
from gstatic by the login page, so the `firebase` package is not needed).
|
|
70
|
+
2. **Re-run `vex`.**
|
|
71
|
+
3. **Run the SQL above** if your identity table is older than this release.
|
|
72
|
+
4. **Set the environment** — the service account, plus the three public values from one *Web* app
|
|
73
|
+
registration in the same Firebase project. Paste the downloaded key file into
|
|
74
|
+
`FIREBASE_SERVICE_ACCOUNT_JSON` as-is: its fields are snake_case (`project_id`, `private_key`,
|
|
75
|
+
`client_email`) and that is what the loader expects.
|
|
76
|
+
5. **Turn it on** with `"auth": { "firebase": true }`, then re-run `vex`.
|
|
77
|
+
6. **Open `/login`.** The button posts to `/api/auth/firebase`; a failed sign-in names the server-side
|
|
78
|
+
configuration problem it can see (missing variables, an unstartable provider name).
|
|
79
|
+
|
|
80
|
+
Notes:
|
|
81
|
+
|
|
82
|
+
- The login page loads the **modular** Firebase SDK as ES modules from gstatic (pinned version), the
|
|
83
|
+
same way the app's own web sign-in does, and preloads it on page load. That needs `www.gstatic.com` in
|
|
84
|
+
`script-src`, plus `connect-src` for the token endpoints and `frame-src` for the project's auth domain
|
|
85
|
+
— the generated page sets all three itself, so an app-wide `helmet` CSP does not have to change.
|
|
86
|
+
- The glue is **inlined into the generated page**, not served from `public/js/`. `public/**` is a
|
|
87
|
+
copy-once template, and an earlier draft of this feature put `public/js/firebaseauth.js` there — so a
|
|
88
|
+
project that ran that draft keeps a compat-era file while its page moved on, and the page fails with
|
|
89
|
+
`Firebase sign-in is not configured: window.undefined is missing`. **Delete
|
|
90
|
+
`public/js/firebaseauth.js`** from any project generated from that draft; nothing references it any
|
|
91
|
+
more. (Same for a stale `/js/firebase/*` route, if you carried one.)
|
|
92
|
+
- The login page overrides `Cross-Origin-Opener-Policy` to `same-origin-allow-popups`. helmet's default
|
|
93
|
+
(`same-origin`) nulls `window.opener` inside a cross-origin popup, which is the channel the Firebase
|
|
94
|
+
handler reports on, and the SDK then reports **`auth/popup-closed-by-user`** — a message that points at
|
|
95
|
+
the user and the network, never at the header that actually caused it. If you proxy or re-render the
|
|
96
|
+
login page yourself, keep this header.
|
|
97
|
+
- `LoginUI.gen.ts` and `FirebaseAuthUI.gen.ts` are regenerated even when `auth.firebase` is false; the
|
|
98
|
+
wiring module exports `undefined` and the page renders no Firebase button.
|
|
@@ -8,7 +8,7 @@ Branch: TBD
|
|
|
8
8
|
|
|
9
9
|
`createdBy` / `createdAt` 这类审计字段在生成的应用里**静默留下 NULL**,且没有任何信号。
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
生产项目的实测现场:
|
|
12
12
|
|
|
13
13
|
- 23/24 个 schema 声明了 `createdBy` / `updatedBy`(`onCreateUserId` / `onUpdateUserId`)
|
|
14
14
|
- 只有 `User` 一个声明了 `dataIsolation`
|
|
@@ -67,7 +67,7 @@ if (useAuth) {
|
|
|
67
67
|
|
|
68
68
|
改为 fail-loud 即推翻此契约,文档必须同步。
|
|
69
69
|
|
|
70
|
-
### 2.4
|
|
70
|
+
### 2.4 触发路径证明(生产项目实测)
|
|
71
71
|
|
|
72
72
|
```
|
|
73
73
|
$ grep -rln "DataIsolationContext.middleware" src/system/_controllers/ | wc -l
|
|
@@ -211,7 +211,7 @@ UserContext.run(tokenData, () => next());
|
|
|
211
211
|
- `:55` 的 `DataIsolationContext` 可选 import 删除
|
|
212
212
|
- 不再生成独立的上下文中间件 —— `UserContext` 从 middleware 降级为 helper 模块(只剩 `run()` / `getStore()` / `userId`)
|
|
213
213
|
|
|
214
|
-
**为什么这是从构造上消除挂载门**:`Authentication.middleware` 本来就挂在每个 `useAuth` 的 controller 上(生成 18 个 +
|
|
214
|
+
**为什么这是从构造上消除挂载门**:`Authentication.middleware` 本来就挂在每个 `useAuth` 的 controller 上(生成 18 个 + 应用手写 17 个里的 14 个)。没有第二处需要记得挂的装饰器,就没有「忘了挂」这一态。
|
|
215
215
|
|
|
216
216
|
### 4.4 Adapter 强制规则
|
|
217
217
|
|
|
@@ -338,9 +338,9 @@ Mongoose `replace` 与其余三者行为不一致。本 spec 接受,但要求
|
|
|
338
338
|
|
|
339
339
|
**代价(明确接受)**:**没有逃生口**。任何处于请求链之外的写(seed、后台 job、tsoa 注册路径)都无法声明身份,只能硬失败或静默 NULL。
|
|
340
340
|
|
|
341
|
-
|
|
341
|
+
**影响面(生产项目实测)**:`VexDb.getRepository(...)` 构造点 79 处,其中写路径 19 处(4 处直连写 + 15 处 `this.repo`)。选 ALS 路线意味着这 79 处**零改动**。
|
|
342
342
|
|
|
343
|
-
|
|
343
|
+
**已识别的硬冲突**:某应用的注册路径在**无 auth** 的 controller 里 create `User`:
|
|
344
344
|
|
|
345
345
|
| 位置 | 装饰器 |
|
|
346
346
|
|---|---|
|
|
@@ -391,11 +391,11 @@ const user = await this.userRepo.create({ name: email.split("@")[0], email, acti
|
|
|
391
391
|
|
|
392
392
|
### 6.1 注册路径(高)
|
|
393
393
|
|
|
394
|
-
见 D2
|
|
394
|
+
见 D2。当前该应用安全,但依赖「`User` 没有 `createdBy`」这一巧合。必须在 D2 的 A/B/C 中做出选择后才能认为风险闭环。
|
|
395
395
|
|
|
396
396
|
### 6.2 请求链外的写入(中)
|
|
397
397
|
|
|
398
|
-
`src/services/*`
|
|
398
|
+
`src/services/*` 中的写路径无请求上下文。实测:
|
|
399
399
|
|
|
400
400
|
| 位置 | 写入 | 该实体是否有 `onCreateUserId` |
|
|
401
401
|
|---|---|---|
|
|
@@ -445,9 +445,9 @@ const user = await this.userRepo.create({ name: email.split("@")[0], email, acti
|
|
|
445
445
|
| 删除生成的 `@Middlewares(DataIsolationContext.middleware)` | 破坏性(生成物) | 下游需重新 `vex`;旧的 `DataIsolationContext.gen.ts` 会被 `cleanupStaleFiles()` 清理 |
|
|
446
446
|
| `pipeline` 生成 `UserContext.gen.ts` | 新增 | — |
|
|
447
447
|
| 数据隔离功能 | 无变化 | 覆盖面顺带扩大(所有 authed controller) |
|
|
448
|
-
| **数据隔离第一次真正生效** | **破坏性(下游可见)** | `entityIsolation` 里的实体,此前若其 controller 没挂中间件就不过滤;改后任何 authed controller 访问它都会过滤。`mergeFilter` 是 `{ ...mapped, ...ownership }`,**ownership 覆盖调用方 filter 的 `_id`** → 按 id 查/改的接口会静默变成「操作自己那一行」。**这是各 app 自己的接口问题,修复不在本卡范围**
|
|
448
|
+
| **数据隔离第一次真正生效** | **破坏性(下游可见)** | `entityIsolation` 里的实体,此前若其 controller 没挂中间件就不过滤;改后任何 authed controller 访问它都会过滤。`mergeFilter` 是 `{ ...mapped, ...ownership }`,**ownership 覆盖调用方 filter 的 `_id`** → 按 id 查/改的接口会静默变成「操作自己那一行」。**这是各 app 自己的接口问题,修复不在本卡范围** |
|
|
449
449
|
|
|
450
|
-
**迁移**:下游跑一次 `vex`。历史 NULL 行**不会**被回填 —— 需要各应用自行决定值(`createdBy`
|
|
450
|
+
**迁移**:下游跑一次 `vex`。历史 NULL 行**不会**被回填 —— 需要各应用自行决定值(`createdBy` 有些可由关系推导,有些应用的 owner 是 profile 行而 `createdBy` 语义是 `User`,推不出来)。
|
|
451
451
|
|
|
452
452
|
## 8. 测试计划
|
|
453
453
|
|
package/docs/vexJsonSchema.md
CHANGED
|
@@ -77,6 +77,16 @@ Each `.json` file defines one REST resource. Filename must match `x-documentConf
|
|
|
77
77
|
| --- | --- |
|
|
78
78
|
| `role` | Marks this field as the RBAC role field. Required for role-based access control to work. |
|
|
79
79
|
| `userId` | Marks the field that holds the identity value. Declare it **exactly once**, on the account schema (`User._id`) — every `onXXUserId` audit field is filled from it. |
|
|
80
|
+
| `softDelete` | Marks the boolean field carrying this document's soft-delete state. **At most one per document.** The repository then hides rows whose marker is set, and the field is removed from generated request bodies. Requires `{ "type": "boolean", "default": false }` and must be `required`, which yields a `boolean NOT NULL DEFAULT false` column. Full reference: [`docs/features/accountDeletion.md`](features/accountDeletion.md). |
|
|
81
|
+
|
|
82
|
+
```jsonc
|
|
83
|
+
"deleted": { "type": "boolean", "default": false, "x-vexData": "softDelete" }
|
|
84
|
+
// and list it in the root "required" array
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
A nullable marker would break the hide-filter (`marker IS NOT TRUE` drops NULL rows), so generation
|
|
88
|
+
rejects it. There is no separate `deletedAt` / `deletedBy`: the existing `updatedAt` / `updatedBy`
|
|
89
|
+
audit fields record who deleted the row and when.
|
|
80
90
|
|
|
81
91
|
---
|
|
82
92
|
|
|
@@ -105,7 +115,7 @@ Works for both `sql` and `mongo` targets. Full reference:
|
|
|
105
115
|
|
|
106
116
|
Generation fails early on a contradiction: an unknown `x-vexData` value, an `onXXUserId` field whose
|
|
107
117
|
column type differs from the tagged identity field, a mismatched timestamp keyword, a missing identity
|
|
108
|
-
field,
|
|
118
|
+
field, an audit field marked `required`, or a malformed soft-delete marker.
|
|
109
119
|
|
|
110
120
|
Declaring a keyword also removes the field from the **request body**: the generator emits
|
|
111
121
|
`export type Payload{Doc} = Omit<{Doc}, "createdAt" | … >;` into `src/system/_types/{Doc}.gen.ts` and the
|
package/package.json
CHANGED
package/vex.config.json
CHANGED
|
@@ -9,11 +9,14 @@
|
|
|
9
9
|
"useUserSchema": true,
|
|
10
10
|
"useObjectID": true,
|
|
11
11
|
"allowApiCreateUpdate_id": false,
|
|
12
|
-
"useStatefulRedisAuth": false
|
|
12
|
+
"useStatefulRedisAuth": false,
|
|
13
|
+
"showSoftDeleted": false
|
|
13
14
|
},
|
|
14
15
|
"auth": {
|
|
15
16
|
"localAuth": true,
|
|
16
17
|
"useHttpOnlyCookieToken": true,
|
|
18
|
+
"deleteAccount": true,
|
|
19
|
+
"firebase": true,
|
|
17
20
|
"oauthProviders": {
|
|
18
21
|
"google": false,
|
|
19
22
|
"github": false
|
|
File without changes
|
|
File without changes
|