@jskit-ai/auth-provider-local-db-core 0.1.156 → 0.1.158

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/CHANGELOG.md ADDED
@@ -0,0 +1,16 @@
1
+ # Changelog
2
+
3
+ ## Unreleased
4
+
5
+ - The owner APIs (`auth.local.accounts`, `auth.local.sessions`, `auth.local.recovery`) hand their records only the caller's transaction, with the feature's system context: a caller's other settings no longer ride into every read and write the records make.
6
+ - `@jskit-ai/database-runtime` and `@jskit-ai/resource-runtime` are peer dependencies, required and at the same versions, where they were dependencies: its accounts, sessions and sign-in records are table resources, and the app holds one copy of the table runtime that every package shares, as it does the kernel. npm installs a required peer as it installed the dependency, so an app installs the same packages and versions; a release whose versions disagree now fails the install with ERESOLVE instead of nesting a second copy.
7
+ - `auth.local.accounts.create` (and the database backend's `users.create`) refuses a duplicate account with a `ConflictError` (409) coded `auth.local.accounts.exists`, carrying the database error as its `cause`, where it threw a plain `Error("Local auth user already exists.")` with no code.
8
+ - `AuthLocalDatabaseBackendProvider` declares a system scope ("local sign-in keeps its own accounts, sessions and tokens, which no user owns"), and `auth.local.backend` and the owner APIs run every read and write in its system context, whatever context a caller passes: local sign-in writes its own records, timestamps included. A handle call without a context is now a request's, whose timestamps the handle replaces, so this keeps the times local sign-in writes (a session revoked at its own time, a recovery token created with its account) exactly as before.
9
+ - `./server/lib/index` exports `LOCAL_AUTH_INFRASTRUCTURE_TABLES` (`auth_local_identities`), the one table local sign-in keeps outside table resources, for an app test's `expectEveryTableOwned({ infrastructureTables })`.
10
+ - New owner capabilities, provided by `AuthLocalDatabaseBackendProvider` beside `auth.local.backend`, for application code that works with local auth's records: `auth.local.accounts` (`create`, `findByEmail`, `updatePassword`, `updateProfile`, `markEmailVerified`), `auth.local.sessions` (`create`, `revoke`, `revokeForUser`, `deleteExpired`) and `auth.local.recovery` (`create`, `findByTokenHash`, `consume`, `consumeForUser`). Each method takes `options` last and runs in `options.trx` when there is one. Their types are `AuthLocalAccounts`, `AuthLocalSessions` and `AuthLocalRecovery` in the new `./server/types`; `./server/lib/index` also exports `createLocalDbOwners`.
11
+ - `markEmailVerified(userId, options)` records that the account proved it owns its email address, keeping the first proof, and answers the account; account records answer `emailVerifiedAt` (empty until then). The new migration `auth_local_db_users_email_verified` adds the nullable `email_verified_at` column to `auth_local_users` when it is missing.
12
+ - `deleteExpired(now, options)` deletes the sessions that expired by `now` and answers how many.
13
+
14
+ ### Upgrading
15
+
16
+ - Run `npm run db:migrate` before the new version serves requests: its account reads select `email_verified_at`, so until the migration has run every sign-in and account read fails with an unknown column. An application that added `email_verified_at` itself keeps its column (the migration leaves an existing one alone) and replaces its own writes with `markEmailVerified`.
@@ -0,0 +1,20 @@
1
+ // When an account proved it owns its email address, for example by using an
2
+ // emailed sign-in link: set once by auth.local.accounts' markEmailVerified.
3
+ const USERS_TABLE = "auth_local_users";
4
+ const COLUMN_NAME = "email_verified_at";
5
+
6
+ exports.up = async function up(knex) {
7
+ if (!(await knex.schema.hasColumn(USERS_TABLE, COLUMN_NAME))) {
8
+ await knex.schema.alterTable(USERS_TABLE, (table) => {
9
+ table.timestamp(COLUMN_NAME).nullable();
10
+ });
11
+ }
12
+ };
13
+
14
+ exports.down = async function down(knex) {
15
+ if (await knex.schema.hasColumn(USERS_TABLE, COLUMN_NAME)) {
16
+ await knex.schema.alterTable(USERS_TABLE, (table) => {
17
+ table.dropColumn(COLUMN_NAME);
18
+ });
19
+ }
20
+ };
package/package.json CHANGED
@@ -1,30 +1,39 @@
1
1
  {
2
2
  "name": "@jskit-ai/auth-provider-local-db-core",
3
- "version": "0.1.156",
3
+ "version": "0.1.158",
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "test": "jskit test"
7
7
  },
8
8
  "exports": {
9
9
  "./server/providers/AuthLocalDatabaseBackendProvider": "./src/server/providers/AuthLocalDatabaseBackendProvider.js",
10
- "./server/lib/index": "./src/server/lib/index.js"
10
+ "./server/lib/index": "./src/server/lib/index.js",
11
+ "./server/types": "./src/server/types.js"
11
12
  },
12
13
  "dependencies": {
13
- "@jskit-ai/auth-provider-local-core": "0.2.15",
14
- "@jskit-ai/database-runtime": "0.1.261"
14
+ "@jskit-ai/auth-provider-local-core": "0.2.17",
15
+ "@jskit-ai/resource-core": "0.1.205"
15
16
  },
16
17
  "peerDependencies": {
17
- "@jskit-ai/kernel": "0.2.12"
18
+ "@jskit-ai/auth-core": "0.2.14",
19
+ "@jskit-ai/database-runtime": "0.1.263",
20
+ "@jskit-ai/kernel": "0.2.14",
21
+ "@jskit-ai/resource-runtime": "0.2.12"
18
22
  },
19
23
  "description": "Database-backed local auth storage backend for JSKIT local auth.",
20
24
  "jskit": {
21
25
  "kind": "runtime",
22
26
  "capabilities": {
23
27
  "provides": [
24
- "auth.local.backend"
28
+ "auth.local.backend",
29
+ "auth.local.accounts",
30
+ "auth.local.sessions",
31
+ "auth.local.recovery"
25
32
  ],
26
33
  "requires": [
27
- "runtime.database"
34
+ "runtime.actions",
35
+ "runtime.database",
36
+ "runtime.resources"
28
37
  ]
29
38
  },
30
39
  "runtime": {
@@ -72,14 +81,23 @@
72
81
  "surfaces": [
73
82
  {
74
83
  "subpath": "./server/providers/AuthLocalDatabaseBackendProvider",
75
- "summary": "Provides the database-backed local authentication repository contract."
84
+ "summary": "Provides the database-backed local authentication repository contract (auth.local.backend) and the owner APIs of its accounts, sessions and recovery tokens (auth.local.accounts, auth.local.sessions, auth.local.recovery)."
76
85
  },
77
86
  {
78
87
  "subpath": "./server/lib/index",
79
- "summary": "Exports the DB local auth backend factory."
88
+ "summary": "Exports the DB local auth backend and owner API factories."
89
+ },
90
+ {
91
+ "subpath": "./server/types",
92
+ "summary": "Types of the auth.local.accounts, auth.local.sessions and auth.local.recovery capabilities, for features that require them."
80
93
  }
81
94
  ],
82
- "capability": "auth.local.backend"
95
+ "capabilities": [
96
+ "auth.local.backend",
97
+ "auth.local.accounts",
98
+ "auth.local.sessions",
99
+ "auth.local.recovery"
100
+ ]
83
101
  }
84
102
  },
85
103
  "migrations": {
@@ -0,0 +1,19 @@
1
+ // Local sign-in's timestamps: read from any stored or given form, and refused when they are not dates.
2
+
3
+ function nowIso() {
4
+ return new Date().toISOString();
5
+ }
6
+
7
+ /**
8
+ * @param {unknown} value
9
+ * @returns {Date}
10
+ */
11
+ function toDate(value) {
12
+ const date = value instanceof Date ? value : new Date(/** @type {string | number} */ (value));
13
+ if (Number.isNaN(date.getTime())) {
14
+ throw new TypeError("Invalid local auth DB timestamp.");
15
+ }
16
+ return date;
17
+ }
18
+
19
+ export { nowIso, toDate };