@aooth/user 0.2.6 → 0.2.7

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.
@@ -1,10 +1,26 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
- const require_federated_identity_store = require("./federated-identity-store-8G0e79Zc.cjs");
2
+ const require_federated_identity_store = require("./federated-identity-store-BjGt6Kny.cjs");
3
3
  let node_crypto = require("node:crypto");
4
4
  // src/atscript-db/federated-identity-store.ts
5
5
  function isConflict$1(err) {
6
6
  return typeof err === "object" && err !== null && err.code === "CONFLICT";
7
7
  }
8
+ /** Optional row fields (see {@link fromRow}). */
9
+ const OPTIONAL_FIELDS = [
10
+ "email",
11
+ "emailVerified",
12
+ "displayName",
13
+ "avatarUrl",
14
+ "lastLoginAt"
15
+ ];
16
+ /**
17
+ * The DB returns an unset optional column as `null` on SQL engines; drop it so
18
+ * a row matches the memory store + the `field?` contract.
19
+ */
20
+ function fromRow$1(row) {
21
+ require_federated_identity_store.dropNullFields(row, OPTIONAL_FIELDS);
22
+ return row;
23
+ }
8
24
  /**
9
25
  * `@atscript/db`-backed {@link FederatedIdentityStore}. Pass the resolved table
10
26
  * for the `AoothFederatedIdentity` model shipped at
@@ -23,13 +39,14 @@ var FederatedIdentityStoreAtscriptDb = class extends require_federated_identity_
23
39
  this.clock = opts.clock ?? Date.now;
24
40
  }
25
41
  async find(provider, subject) {
26
- return this.table.findOne({ filter: {
42
+ const row = await this.table.findOne({ filter: {
27
43
  provider,
28
44
  subject
29
45
  } });
46
+ return row && fromRow$1(row);
30
47
  }
31
48
  async listForUser(userId) {
32
- return (await this.table.findMany({ filter: { userId } })).toSorted((a, b) => a.linkedAt - b.linkedAt);
49
+ return (await this.table.findMany({ filter: { userId } })).map(fromRow$1).toSorted((a, b) => a.linkedAt - b.linkedAt);
33
50
  }
34
51
  async link(rec) {
35
52
  const row = {
@@ -75,6 +92,26 @@ var FederatedIdentityStoreAtscriptDb = class extends require_federated_identity_
75
92
  function isConflict(err) {
76
93
  return typeof err === "object" && err !== null && err.code === "CONFLICT";
77
94
  }
95
+ /** Optional aooth-owned `account.*` fields (see {@link fromRow}). */
96
+ const OPTIONAL_ACCOUNT_FIELDS = [
97
+ "pendingInvitation",
98
+ "invitedBy",
99
+ "inviteNonce",
100
+ "verifiedEmail"
101
+ ];
102
+ /** Optional aooth-owned root fields (see {@link fromRow}). */
103
+ const OPTIONAL_ROOT_FIELDS = ["trustedDevices", "seenDevices"];
104
+ /**
105
+ * The DB returns an unset optional column as `null` on SQL engines (Mongo
106
+ * omits the key); drop `null` from the optional aooth-owned paths so a row
107
+ * matches the memory store + the `field?` contract — a NULL `inviteNonce` on a
108
+ * pre-0.2 invite must read as absent, not as a different nonce.
109
+ */
110
+ function fromRow(row) {
111
+ if (row.account) require_federated_identity_store.dropNullFields(row.account, OPTIONAL_ACCOUNT_FIELDS);
112
+ require_federated_identity_store.dropNullFields(row, OPTIONAL_ROOT_FIELDS);
113
+ return row;
114
+ }
78
115
  /**
79
116
  * `@atscript/db`-backed `UserStore`. Pass the resolved table for the
80
117
  * `AoothUserCredentials` (or a `.as` interface extending it) shipped at the
@@ -94,22 +131,25 @@ var UsersStoreAtscriptDb = class extends require_federated_identity_store.UserSt
94
131
  async exists(handle) {
95
132
  return await this.table.count({ filter: { username: handle } }) > 0;
96
133
  }
134
+ /** The one row read every finder goes through — normalized by {@link fromRow}. */
135
+ async findRow(filter) {
136
+ const row = await this.table.findOne({ filter });
137
+ return row && fromRow(row);
138
+ }
97
139
  async findById(id) {
98
- return await this.table.findOne({ filter: { id } });
140
+ return this.findRow({ id });
99
141
  }
100
142
  async findByHandle(handle) {
101
- const byUsername = await this.table.findOne({ filter: { username: handle } });
143
+ const byUsername = await this.findRow({ username: handle });
102
144
  if (byUsername) return byUsername;
103
145
  for (const field of this.handleFields) {
104
- const byHandle = await this.table.findOne({ filter: { [field]: handle } });
146
+ const byHandle = await this.findRow({ [field]: handle });
105
147
  if (byHandle) return byHandle;
106
148
  }
107
149
  return null;
108
150
  }
109
151
  async findByIdentifier(value) {
110
- const byId = await this.table.findOne({ filter: { id: value } });
111
- if (byId) return byId;
112
- return this.findByHandle(value);
152
+ return await this.findRow({ id: value }) ?? this.findByHandle(value);
113
153
  }
114
154
  async create(data) {
115
155
  try {
@@ -108,6 +108,8 @@ export declare class UsersStoreAtscriptDb<TUserCustom extends object = object> e
108
108
  protected readonly handleFields: string[];
109
109
  constructor(opts: UsersStoreAtscriptDbOptions<TUserCustom>);
110
110
  exists(handle: string): Promise<boolean>;
111
+ /** The one row read every finder goes through — normalized by {@link fromRow}. */
112
+ private findRow;
111
113
  findById(id: string): Promise<(UserCredentials & TUserCustom) | null>;
112
114
  findByHandle(handle: string): Promise<(UserCredentials & TUserCustom) | null>;
113
115
  findByIdentifier(value: string): Promise<(UserCredentials & TUserCustom) | null>;
@@ -108,6 +108,8 @@ export declare class UsersStoreAtscriptDb<TUserCustom extends object = object> e
108
108
  protected readonly handleFields: string[];
109
109
  constructor(opts: UsersStoreAtscriptDbOptions<TUserCustom>);
110
110
  exists(handle: string): Promise<boolean>;
111
+ /** The one row read every finder goes through — normalized by {@link fromRow}. */
112
+ private findRow;
111
113
  findById(id: string): Promise<(UserCredentials & TUserCustom) | null>;
112
114
  findByHandle(handle: string): Promise<(UserCredentials & TUserCustom) | null>;
113
115
  findByIdentifier(value: string): Promise<(UserCredentials & TUserCustom) | null>;
@@ -1,9 +1,25 @@
1
- import { c as setAtPath, f as UserAuthError, l as UserStore, n as pickDefinedProfile, t as FederatedIdentityStore } from "./federated-identity-store-CUjGv4O7.mjs";
1
+ import { i as dropNullFields, l as setAtPath, n as pickDefinedProfile, p as UserAuthError, t as FederatedIdentityStore, u as UserStore } from "./federated-identity-store-fEO9MIyH.mjs";
2
2
  import { randomUUID } from "node:crypto";
3
3
  // src/atscript-db/federated-identity-store.ts
4
4
  function isConflict$1(err) {
5
5
  return typeof err === "object" && err !== null && err.code === "CONFLICT";
6
6
  }
7
+ /** Optional row fields (see {@link fromRow}). */
8
+ const OPTIONAL_FIELDS = [
9
+ "email",
10
+ "emailVerified",
11
+ "displayName",
12
+ "avatarUrl",
13
+ "lastLoginAt"
14
+ ];
15
+ /**
16
+ * The DB returns an unset optional column as `null` on SQL engines; drop it so
17
+ * a row matches the memory store + the `field?` contract.
18
+ */
19
+ function fromRow$1(row) {
20
+ dropNullFields(row, OPTIONAL_FIELDS);
21
+ return row;
22
+ }
7
23
  /**
8
24
  * `@atscript/db`-backed {@link FederatedIdentityStore}. Pass the resolved table
9
25
  * for the `AoothFederatedIdentity` model shipped at
@@ -22,13 +38,14 @@ var FederatedIdentityStoreAtscriptDb = class extends FederatedIdentityStore {
22
38
  this.clock = opts.clock ?? Date.now;
23
39
  }
24
40
  async find(provider, subject) {
25
- return this.table.findOne({ filter: {
41
+ const row = await this.table.findOne({ filter: {
26
42
  provider,
27
43
  subject
28
44
  } });
45
+ return row && fromRow$1(row);
29
46
  }
30
47
  async listForUser(userId) {
31
- return (await this.table.findMany({ filter: { userId } })).toSorted((a, b) => a.linkedAt - b.linkedAt);
48
+ return (await this.table.findMany({ filter: { userId } })).map(fromRow$1).toSorted((a, b) => a.linkedAt - b.linkedAt);
32
49
  }
33
50
  async link(rec) {
34
51
  const row = {
@@ -74,6 +91,26 @@ var FederatedIdentityStoreAtscriptDb = class extends FederatedIdentityStore {
74
91
  function isConflict(err) {
75
92
  return typeof err === "object" && err !== null && err.code === "CONFLICT";
76
93
  }
94
+ /** Optional aooth-owned `account.*` fields (see {@link fromRow}). */
95
+ const OPTIONAL_ACCOUNT_FIELDS = [
96
+ "pendingInvitation",
97
+ "invitedBy",
98
+ "inviteNonce",
99
+ "verifiedEmail"
100
+ ];
101
+ /** Optional aooth-owned root fields (see {@link fromRow}). */
102
+ const OPTIONAL_ROOT_FIELDS = ["trustedDevices", "seenDevices"];
103
+ /**
104
+ * The DB returns an unset optional column as `null` on SQL engines (Mongo
105
+ * omits the key); drop `null` from the optional aooth-owned paths so a row
106
+ * matches the memory store + the `field?` contract — a NULL `inviteNonce` on a
107
+ * pre-0.2 invite must read as absent, not as a different nonce.
108
+ */
109
+ function fromRow(row) {
110
+ if (row.account) dropNullFields(row.account, OPTIONAL_ACCOUNT_FIELDS);
111
+ dropNullFields(row, OPTIONAL_ROOT_FIELDS);
112
+ return row;
113
+ }
77
114
  /**
78
115
  * `@atscript/db`-backed `UserStore`. Pass the resolved table for the
79
116
  * `AoothUserCredentials` (or a `.as` interface extending it) shipped at the
@@ -93,22 +130,25 @@ var UsersStoreAtscriptDb = class extends UserStore {
93
130
  async exists(handle) {
94
131
  return await this.table.count({ filter: { username: handle } }) > 0;
95
132
  }
133
+ /** The one row read every finder goes through — normalized by {@link fromRow}. */
134
+ async findRow(filter) {
135
+ const row = await this.table.findOne({ filter });
136
+ return row && fromRow(row);
137
+ }
96
138
  async findById(id) {
97
- return await this.table.findOne({ filter: { id } });
139
+ return this.findRow({ id });
98
140
  }
99
141
  async findByHandle(handle) {
100
- const byUsername = await this.table.findOne({ filter: { username: handle } });
142
+ const byUsername = await this.findRow({ username: handle });
101
143
  if (byUsername) return byUsername;
102
144
  for (const field of this.handleFields) {
103
- const byHandle = await this.table.findOne({ filter: { [field]: handle } });
145
+ const byHandle = await this.findRow({ [field]: handle });
104
146
  if (byHandle) return byHandle;
105
147
  }
106
148
  return null;
107
149
  }
108
150
  async findByIdentifier(value) {
109
- const byId = await this.table.findOne({ filter: { id: value } });
110
- if (byId) return byId;
111
- return this.findByHandle(value);
151
+ return await this.findRow({ id: value }) ?? this.findByHandle(value);
112
152
  }
113
153
  async create(data) {
114
154
  try {
@@ -106,6 +106,11 @@ function deepMerge(target, source) {
106
106
  else t[key] = sv;
107
107
  }
108
108
  }
109
+ /** Delete each of `fields` whose value on `obj` is `null` (in place). */
110
+ function dropNullFields(obj, fields) {
111
+ const o = obj;
112
+ for (const field of fields) if (o[field] === null) delete o[field];
113
+ }
109
114
  function setAtPath(obj, path, value) {
110
115
  const parts = path.split(".");
111
116
  let current = obj;
@@ -194,6 +199,12 @@ Object.defineProperty(exports, "deepMerge", {
194
199
  return deepMerge;
195
200
  }
196
201
  });
202
+ Object.defineProperty(exports, "dropNullFields", {
203
+ enumerable: true,
204
+ get: function() {
205
+ return dropNullFields;
206
+ }
207
+ });
197
208
  Object.defineProperty(exports, "incrementAtPath", {
198
209
  enumerable: true,
199
210
  get: function() {
@@ -106,6 +106,11 @@ function deepMerge(target, source) {
106
106
  else t[key] = sv;
107
107
  }
108
108
  }
109
+ /** Delete each of `fields` whose value on `obj` is `null` (in place). */
110
+ function dropNullFields(obj, fields) {
111
+ const o = obj;
112
+ for (const field of fields) if (o[field] === null) delete o[field];
113
+ }
109
114
  function setAtPath(obj, path, value) {
110
115
  const parts = path.split(".");
111
116
  let current = obj;
@@ -158,4 +163,4 @@ function pickDefinedProfile(src) {
158
163
  * keeps the federated-login core (`@aooth/idp`, phase 2) storage-agnostic.
159
164
  */
160
165
  var FederatedIdentityStore = class {};
161
- export { maskEmail as a, setAtPath as c, USER_AUTH_ERROR_MESSAGES as d, UserAuthError as f, incrementAtPath as i, UserStore as l, pickDefinedProfile as n, maskMfaValue as o, deepMerge as r, maskPhone as s, FederatedIdentityStore as t, USER_AUTH_ERROR_CODES as u };
166
+ export { incrementAtPath as a, maskPhone as c, USER_AUTH_ERROR_CODES as d, USER_AUTH_ERROR_MESSAGES as f, dropNullFields as i, setAtPath as l, pickDefinedProfile as n, maskEmail as o, UserAuthError as p, deepMerge as r, maskMfaValue as s, FederatedIdentityStore as t, UserStore as u };
package/dist/index.cjs CHANGED
@@ -1,5 +1,5 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
- const require_federated_identity_store = require("./federated-identity-store-8G0e79Zc.cjs");
2
+ const require_federated_identity_store = require("./federated-identity-store-BjGt6Kny.cjs");
3
3
  let node_crypto = require("node:crypto");
4
4
  // src/base-x/base32.ts
5
5
  /**
@@ -588,7 +588,7 @@ var UserService = class {
588
588
  accepted = false;
589
589
  const account = user.account;
590
590
  if (!account.pendingInvitation) return null;
591
- if (account.inviteNonce !== void 0 && account.inviteNonce !== nonce) return null;
591
+ if (account.inviteNonce != null && account.inviteNonce !== nonce) return null;
592
592
  accepted = true;
593
593
  return { set: { account: { pendingInvitation: false } } };
594
594
  });
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { a as maskEmail, c as setAtPath, d as USER_AUTH_ERROR_MESSAGES, f as UserAuthError, i as incrementAtPath, l as UserStore, n as pickDefinedProfile, o as maskMfaValue, r as deepMerge, s as maskPhone, t as FederatedIdentityStore, u as USER_AUTH_ERROR_CODES } from "./federated-identity-store-CUjGv4O7.mjs";
1
+ import { a as incrementAtPath, c as maskPhone, d as USER_AUTH_ERROR_CODES, f as USER_AUTH_ERROR_MESSAGES, l as setAtPath, n as pickDefinedProfile, o as maskEmail, p as UserAuthError, r as deepMerge, s as maskMfaValue, t as FederatedIdentityStore, u as UserStore } from "./federated-identity-store-fEO9MIyH.mjs";
2
2
  import { createHash, createHmac, randomBytes, randomInt, randomUUID, scrypt, timingSafeEqual } from "node:crypto";
3
3
  // src/base-x/base32.ts
4
4
  /**
@@ -587,7 +587,7 @@ var UserService = class {
587
587
  accepted = false;
588
588
  const account = user.account;
589
589
  if (!account.pendingInvitation) return null;
590
- if (account.inviteNonce !== void 0 && account.inviteNonce !== nonce) return null;
590
+ if (account.inviteNonce != null && account.inviteNonce !== nonce) return null;
591
591
  accepted = true;
592
592
  return { set: { account: { pendingInvitation: false } } };
593
593
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aooth/user",
3
- "version": "0.2.6",
3
+ "version": "0.2.7",
4
4
  "description": "User credential primitives for aoothjs",
5
5
  "keywords": [
6
6
  "aoothjs",
@@ -42,6 +42,10 @@
42
42
  "import": "./dist/atscript-db.mjs",
43
43
  "require": "./dist/atscript-db.cjs"
44
44
  },
45
+ "./atscript-db/model": {
46
+ "types": "./src/atscript-db/user-credentials.as.d.ts",
47
+ "default": "./src/atscript-db/user-credentials.as"
48
+ },
45
49
  "./atscript-db/model.as": {
46
50
  "types": "./src/atscript-db/user-credentials.as.d.ts",
47
51
  "default": "./src/atscript-db/user-credentials.as"
@@ -60,18 +64,18 @@
60
64
  "access": "public"
61
65
  },
62
66
  "devDependencies": {
63
- "@atscript/core": "^0.2.9",
64
- "@atscript/db": "^0.1.174",
65
- "@atscript/db-sql-tools": "^0.1.174",
66
- "@atscript/db-sqlite": "^0.1.174",
67
- "@atscript/typescript": "^0.2.9",
67
+ "@atscript/core": "^0.2.11",
68
+ "@atscript/db": "^0.1.175",
69
+ "@atscript/db-sql-tools": "^0.1.175",
70
+ "@atscript/db-sqlite": "^0.1.175",
71
+ "@atscript/typescript": "^0.2.11",
68
72
  "@types/better-sqlite3": "^7.6.13",
69
- "@uniqu/core": "^0.2.1",
73
+ "@uniqu/core": "^0.2.2",
70
74
  "better-sqlite3": "^12.6.2",
71
- "unplugin-atscript": "^0.2.9"
75
+ "unplugin-atscript": "^0.2.11"
72
76
  },
73
77
  "peerDependencies": {
74
- "@atscript/db": "^0.1.174"
78
+ "@atscript/db": "^0.1.175"
75
79
  },
76
80
  "peerDependenciesMeta": {
77
81
  "@atscript/db": {