@fonderie/auth 1.0.0

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.
Files changed (43) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +51 -0
  3. package/dist/dtos/user.cjs +66 -0
  4. package/dist/dtos/user.cjs.map +1 -0
  5. package/dist/dtos/user.d.cts +24 -0
  6. package/dist/dtos/user.d.ts +24 -0
  7. package/dist/dtos/user.js +41 -0
  8. package/dist/dtos/user.js.map +1 -0
  9. package/dist/index.cjs +1952 -0
  10. package/dist/index.cjs.map +1 -0
  11. package/dist/index.d.cts +22 -0
  12. package/dist/index.d.ts +22 -0
  13. package/dist/index.js +1915 -0
  14. package/dist/index.js.map +1 -0
  15. package/dist/middlewares/index.cjs +303 -0
  16. package/dist/middlewares/index.cjs.map +1 -0
  17. package/dist/middlewares/index.d.cts +10 -0
  18. package/dist/middlewares/index.d.ts +10 -0
  19. package/dist/middlewares/index.js +270 -0
  20. package/dist/middlewares/index.js.map +1 -0
  21. package/dist/migrations/index.js +7 -0
  22. package/dist/migrations/index.js.map +1 -0
  23. package/dist/migrations/sql/001_auth.sql +73 -0
  24. package/dist/migrations/sql/002_phone_auth.sql +20 -0
  25. package/dist/migrations/sql/003_phone_registration_name.sql +4 -0
  26. package/dist/migrations/sql/004_drop_phone_verif_name_cols.sql +5 -0
  27. package/dist/migrations/sql/005_phone_verification_user_id.sql +6 -0
  28. package/dist/migrations/sql/006_drop_phone_verified_at.sql +1 -0
  29. package/dist/migrations/sql/007_email_verif_user_id_pk.sql +14 -0
  30. package/dist/migrations/sql/008_password_reset_pin.sql +4 -0
  31. package/dist/migrations/sql/009_password_reset_created_at.sql +1 -0
  32. package/dist/migrations/sql/010_mfa_pending_secret.sql +3 -0
  33. package/dist/migrations/sql/011_mfa_backup_codes.sql +10 -0
  34. package/dist/migrations/sql/012_drop_skills.sql +1 -0
  35. package/dist/session-CLD1WPJs.d.cts +41 -0
  36. package/dist/session-CLD1WPJs.d.ts +41 -0
  37. package/dist/types.cjs +19 -0
  38. package/dist/types.cjs.map +1 -0
  39. package/dist/types.d.cts +52 -0
  40. package/dist/types.d.ts +52 -0
  41. package/dist/types.js +1 -0
  42. package/dist/types.js.map +1 -0
  43. package/package.json +100 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Fonderie, Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,51 @@
1
+ # @fonderie/auth
2
+
3
+ Drop-in auth for SaaS: email/password, phone OTP, Google OAuth, and
4
+ stateless JWT sessions — shipped as a brick that registers its routes,
5
+ migrations, and events in one line.
6
+
7
+ ## Install
8
+
9
+ ```sh
10
+ npm install @fonderie/auth
11
+ ```
12
+
13
+ ## Use
14
+
15
+ ```ts
16
+ import { FonderieApp, defineConfig } from '@fonderie/core';
17
+ import { AuthModule } from '@fonderie/auth';
18
+
19
+ const app = await new FonderieApp(defineConfig({}))
20
+ .register(new AuthModule())
21
+ .boot();
22
+ ```
23
+
24
+ Guard your own routes with the exported middlewares:
25
+
26
+ ```ts
27
+ import { withSession, requireAuth } from '@fonderie/auth';
28
+ ```
29
+
30
+ Also exports `toUserDTO`, `normalizeEmail`, and the full type surface
31
+ (`IUser`, `ISession`, `IMfaChallenge`, …).
32
+
33
+ ## Why this exists
34
+
35
+ You've shipped this plumbing before — auth, teams, billing, messaging —
36
+ and the next project will ask for it again. Fonderie packages it once:
37
+ plain TypeScript modules for
38
+ [`@fonderie/core`](https://github.com/fonderie-js/sdk/tree/main/packages/core),
39
+ PostgreSQL-backed, self-hosted, MIT. No external control plane, no
40
+ per-seat anything. Register the modules you need; skip the ones you don't.
41
+
42
+ **This package owns** who the caller is. Identity, credentials, sessions, and MFA — every
43
+ other brick trusts the `ctx.user` this one establishes.
44
+
45
+ Browse the whole set at
46
+ [fonderie-js/sdk](https://github.com/fonderie-js/sdk) · follow
47
+ [@fonderiejs](https://x.com/fonderiejs)
48
+
49
+ ## License
50
+
51
+ MIT © Fonderie, Inc.
@@ -0,0 +1,66 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/dtos/user.ts
21
+ var user_exports = {};
22
+ __export(user_exports, {
23
+ toUserDTO: () => toUserDTO
24
+ });
25
+ module.exports = __toCommonJS(user_exports);
26
+ var import_core = require("@fonderie/core");
27
+ var DEFAULT_PREFERENCES = {
28
+ locale: "en-US",
29
+ timezone: "UTC",
30
+ notifications: { email: true, inApp: true, sms: false, push: false },
31
+ emailDigest: "immediate",
32
+ dateFormat: "MM/DD/YYYY",
33
+ timeFormat: "hh:mm A"
34
+ };
35
+ function toUserDTO(user, phoneVerified = false) {
36
+ const prefs = user.preferences ?? {};
37
+ return {
38
+ id: (0, import_core.stringOrEmpty)(user.id),
39
+ email: (0, import_core.stringOrEmpty)(user.email),
40
+ firstName: (0, import_core.stringOrEmpty)(user.firstName),
41
+ lastName: (0, import_core.stringOrEmpty)(user.lastName),
42
+ phone: (0, import_core.stringOrEmpty)(user.phone),
43
+ profileImageUrl: (0, import_core.stringOrEmpty)(user.profileImageUrl),
44
+ isActive: typeof user.isActive === "boolean" ? user.isActive : true,
45
+ lastLogin: user.lastLogin instanceof Date ? user.lastLogin.toISOString() : "",
46
+ preferences: {
47
+ ...DEFAULT_PREFERENCES,
48
+ ...prefs,
49
+ locale: user.locale || prefs.locale || "en-US",
50
+ timezone: user.timezone || prefs.timezone || "UTC"
51
+ },
52
+ isEmailVerified: user.emailVerifiedAt !== null,
53
+ isPhoneVerified: phoneVerified,
54
+ mfaEnabled: (0, import_core.booleanOrFalse)(user.mfaEnabled),
55
+ suspended: (0, import_core.booleanOrFalse)(user.suspended),
56
+ whitelist: (0, import_core.booleanOrFalse)(user.whitelist),
57
+ ipWhitelist: Array.isArray(user.ipWhitelist) ? user.ipWhitelist : [],
58
+ createdAt: user.createdAt instanceof Date ? user.createdAt.toISOString() : "",
59
+ updatedAt: user.updatedAt instanceof Date ? user.updatedAt.toISOString() : ""
60
+ };
61
+ }
62
+ // Annotate the CommonJS export names for ESM import in node:
63
+ 0 && (module.exports = {
64
+ toUserDTO
65
+ });
66
+ //# sourceMappingURL=user.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/dtos/user.ts"],"sourcesContent":["import { stringOrEmpty, booleanOrFalse } from '@fonderie/core';\n\nimport type { IUser, IUserPreferences } from '../types';\n\nexport interface IUserDTO {\n\tid: string;\n\temail: string;\n\tfirstName: string;\n\tlastName: string;\n\tphone: string;\n\tprofileImageUrl: string;\n\tisActive: boolean;\n\tlastLogin: string;\n\tpreferences: IUserPreferences;\n\tisEmailVerified: boolean;\n\tisPhoneVerified: boolean;\n\tmfaEnabled: boolean;\n\tsuspended: boolean;\n\twhitelist: boolean;\n\tipWhitelist: string[];\n\tcreatedAt: string;\n\tupdatedAt: string;\n}\n\nconst DEFAULT_PREFERENCES: IUserPreferences = {\n\tlocale: 'en-US',\n\ttimezone: 'UTC',\n\tnotifications: { email: true, inApp: true, sms: false, push: false },\n\temailDigest: 'immediate',\n\tdateFormat: 'MM/DD/YYYY',\n\ttimeFormat: 'hh:mm A',\n};\n\nexport function toUserDTO(user: IUser, phoneVerified = false): IUserDTO {\n\tconst prefs = user.preferences ?? ({} as IUserPreferences);\n\treturn {\n\t\tid: stringOrEmpty(user.id),\n\t\temail: stringOrEmpty(user.email),\n\t\tfirstName: stringOrEmpty(user.firstName),\n\t\tlastName: stringOrEmpty(user.lastName),\n\t\tphone: stringOrEmpty(user.phone),\n\t\tprofileImageUrl: stringOrEmpty(user.profileImageUrl),\n\t\tisActive: typeof user.isActive === 'boolean' ? user.isActive : true,\n\t\tlastLogin: user.lastLogin instanceof Date ? user.lastLogin.toISOString() : '',\n\t\tpreferences: {\n\t\t\t...DEFAULT_PREFERENCES,\n\t\t\t...prefs,\n\t\t\tlocale: user.locale || prefs.locale || 'en-US',\n\t\t\ttimezone: user.timezone || prefs.timezone || 'UTC',\n\t\t},\n\t\tisEmailVerified: user.emailVerifiedAt !== null,\n\t\tisPhoneVerified: phoneVerified,\n\t\tmfaEnabled: booleanOrFalse(user.mfaEnabled),\n\t\tsuspended: booleanOrFalse(user.suspended),\n\t\twhitelist: booleanOrFalse(user.whitelist),\n\t\tipWhitelist: Array.isArray(user.ipWhitelist) ? user.ipWhitelist : [],\n\t\tcreatedAt: user.createdAt instanceof Date ? user.createdAt.toISOString() : '',\n\t\tupdatedAt: user.updatedAt instanceof Date ? user.updatedAt.toISOString() : '',\n\t};\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,kBAA8C;AAwB9C,IAAM,sBAAwC;AAAA,EAC7C,QAAQ;AAAA,EACR,UAAU;AAAA,EACV,eAAe,EAAE,OAAO,MAAM,OAAO,MAAM,KAAK,OAAO,MAAM,MAAM;AAAA,EACnE,aAAa;AAAA,EACb,YAAY;AAAA,EACZ,YAAY;AACb;AAEO,SAAS,UAAU,MAAa,gBAAgB,OAAiB;AACvE,QAAM,QAAQ,KAAK,eAAgB,CAAC;AACpC,SAAO;AAAA,IACN,QAAI,2BAAc,KAAK,EAAE;AAAA,IACzB,WAAO,2BAAc,KAAK,KAAK;AAAA,IAC/B,eAAW,2BAAc,KAAK,SAAS;AAAA,IACvC,cAAU,2BAAc,KAAK,QAAQ;AAAA,IACrC,WAAO,2BAAc,KAAK,KAAK;AAAA,IAC/B,qBAAiB,2BAAc,KAAK,eAAe;AAAA,IACnD,UAAU,OAAO,KAAK,aAAa,YAAY,KAAK,WAAW;AAAA,IAC/D,WAAW,KAAK,qBAAqB,OAAO,KAAK,UAAU,YAAY,IAAI;AAAA,IAC3E,aAAa;AAAA,MACZ,GAAG;AAAA,MACH,GAAG;AAAA,MACH,QAAQ,KAAK,UAAU,MAAM,UAAU;AAAA,MACvC,UAAU,KAAK,YAAY,MAAM,YAAY;AAAA,IAC9C;AAAA,IACA,iBAAiB,KAAK,oBAAoB;AAAA,IAC1C,iBAAiB;AAAA,IACjB,gBAAY,4BAAe,KAAK,UAAU;AAAA,IAC1C,eAAW,4BAAe,KAAK,SAAS;AAAA,IACxC,eAAW,4BAAe,KAAK,SAAS;AAAA,IACxC,aAAa,MAAM,QAAQ,KAAK,WAAW,IAAI,KAAK,cAAc,CAAC;AAAA,IACnE,WAAW,KAAK,qBAAqB,OAAO,KAAK,UAAU,YAAY,IAAI;AAAA,IAC3E,WAAW,KAAK,qBAAqB,OAAO,KAAK,UAAU,YAAY,IAAI;AAAA,EAC5E;AACD;","names":[]}
@@ -0,0 +1,24 @@
1
+ import { IUserPreferences, IUser } from '../types.cjs';
2
+
3
+ interface IUserDTO {
4
+ id: string;
5
+ email: string;
6
+ firstName: string;
7
+ lastName: string;
8
+ phone: string;
9
+ profileImageUrl: string;
10
+ isActive: boolean;
11
+ lastLogin: string;
12
+ preferences: IUserPreferences;
13
+ isEmailVerified: boolean;
14
+ isPhoneVerified: boolean;
15
+ mfaEnabled: boolean;
16
+ suspended: boolean;
17
+ whitelist: boolean;
18
+ ipWhitelist: string[];
19
+ createdAt: string;
20
+ updatedAt: string;
21
+ }
22
+ declare function toUserDTO(user: IUser, phoneVerified?: boolean): IUserDTO;
23
+
24
+ export { type IUserDTO, toUserDTO };
@@ -0,0 +1,24 @@
1
+ import { IUserPreferences, IUser } from '../types.js';
2
+
3
+ interface IUserDTO {
4
+ id: string;
5
+ email: string;
6
+ firstName: string;
7
+ lastName: string;
8
+ phone: string;
9
+ profileImageUrl: string;
10
+ isActive: boolean;
11
+ lastLogin: string;
12
+ preferences: IUserPreferences;
13
+ isEmailVerified: boolean;
14
+ isPhoneVerified: boolean;
15
+ mfaEnabled: boolean;
16
+ suspended: boolean;
17
+ whitelist: boolean;
18
+ ipWhitelist: string[];
19
+ createdAt: string;
20
+ updatedAt: string;
21
+ }
22
+ declare function toUserDTO(user: IUser, phoneVerified?: boolean): IUserDTO;
23
+
24
+ export { type IUserDTO, toUserDTO };
@@ -0,0 +1,41 @@
1
+ // src/dtos/user.ts
2
+ import { stringOrEmpty, booleanOrFalse } from "@fonderie/core";
3
+ var DEFAULT_PREFERENCES = {
4
+ locale: "en-US",
5
+ timezone: "UTC",
6
+ notifications: { email: true, inApp: true, sms: false, push: false },
7
+ emailDigest: "immediate",
8
+ dateFormat: "MM/DD/YYYY",
9
+ timeFormat: "hh:mm A"
10
+ };
11
+ function toUserDTO(user, phoneVerified = false) {
12
+ const prefs = user.preferences ?? {};
13
+ return {
14
+ id: stringOrEmpty(user.id),
15
+ email: stringOrEmpty(user.email),
16
+ firstName: stringOrEmpty(user.firstName),
17
+ lastName: stringOrEmpty(user.lastName),
18
+ phone: stringOrEmpty(user.phone),
19
+ profileImageUrl: stringOrEmpty(user.profileImageUrl),
20
+ isActive: typeof user.isActive === "boolean" ? user.isActive : true,
21
+ lastLogin: user.lastLogin instanceof Date ? user.lastLogin.toISOString() : "",
22
+ preferences: {
23
+ ...DEFAULT_PREFERENCES,
24
+ ...prefs,
25
+ locale: user.locale || prefs.locale || "en-US",
26
+ timezone: user.timezone || prefs.timezone || "UTC"
27
+ },
28
+ isEmailVerified: user.emailVerifiedAt !== null,
29
+ isPhoneVerified: phoneVerified,
30
+ mfaEnabled: booleanOrFalse(user.mfaEnabled),
31
+ suspended: booleanOrFalse(user.suspended),
32
+ whitelist: booleanOrFalse(user.whitelist),
33
+ ipWhitelist: Array.isArray(user.ipWhitelist) ? user.ipWhitelist : [],
34
+ createdAt: user.createdAt instanceof Date ? user.createdAt.toISOString() : "",
35
+ updatedAt: user.updatedAt instanceof Date ? user.updatedAt.toISOString() : ""
36
+ };
37
+ }
38
+ export {
39
+ toUserDTO
40
+ };
41
+ //# sourceMappingURL=user.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/dtos/user.ts"],"sourcesContent":["import { stringOrEmpty, booleanOrFalse } from '@fonderie/core';\n\nimport type { IUser, IUserPreferences } from '../types';\n\nexport interface IUserDTO {\n\tid: string;\n\temail: string;\n\tfirstName: string;\n\tlastName: string;\n\tphone: string;\n\tprofileImageUrl: string;\n\tisActive: boolean;\n\tlastLogin: string;\n\tpreferences: IUserPreferences;\n\tisEmailVerified: boolean;\n\tisPhoneVerified: boolean;\n\tmfaEnabled: boolean;\n\tsuspended: boolean;\n\twhitelist: boolean;\n\tipWhitelist: string[];\n\tcreatedAt: string;\n\tupdatedAt: string;\n}\n\nconst DEFAULT_PREFERENCES: IUserPreferences = {\n\tlocale: 'en-US',\n\ttimezone: 'UTC',\n\tnotifications: { email: true, inApp: true, sms: false, push: false },\n\temailDigest: 'immediate',\n\tdateFormat: 'MM/DD/YYYY',\n\ttimeFormat: 'hh:mm A',\n};\n\nexport function toUserDTO(user: IUser, phoneVerified = false): IUserDTO {\n\tconst prefs = user.preferences ?? ({} as IUserPreferences);\n\treturn {\n\t\tid: stringOrEmpty(user.id),\n\t\temail: stringOrEmpty(user.email),\n\t\tfirstName: stringOrEmpty(user.firstName),\n\t\tlastName: stringOrEmpty(user.lastName),\n\t\tphone: stringOrEmpty(user.phone),\n\t\tprofileImageUrl: stringOrEmpty(user.profileImageUrl),\n\t\tisActive: typeof user.isActive === 'boolean' ? user.isActive : true,\n\t\tlastLogin: user.lastLogin instanceof Date ? user.lastLogin.toISOString() : '',\n\t\tpreferences: {\n\t\t\t...DEFAULT_PREFERENCES,\n\t\t\t...prefs,\n\t\t\tlocale: user.locale || prefs.locale || 'en-US',\n\t\t\ttimezone: user.timezone || prefs.timezone || 'UTC',\n\t\t},\n\t\tisEmailVerified: user.emailVerifiedAt !== null,\n\t\tisPhoneVerified: phoneVerified,\n\t\tmfaEnabled: booleanOrFalse(user.mfaEnabled),\n\t\tsuspended: booleanOrFalse(user.suspended),\n\t\twhitelist: booleanOrFalse(user.whitelist),\n\t\tipWhitelist: Array.isArray(user.ipWhitelist) ? user.ipWhitelist : [],\n\t\tcreatedAt: user.createdAt instanceof Date ? user.createdAt.toISOString() : '',\n\t\tupdatedAt: user.updatedAt instanceof Date ? user.updatedAt.toISOString() : '',\n\t};\n}\n"],"mappings":";AAAA,SAAS,eAAe,sBAAsB;AAwB9C,IAAM,sBAAwC;AAAA,EAC7C,QAAQ;AAAA,EACR,UAAU;AAAA,EACV,eAAe,EAAE,OAAO,MAAM,OAAO,MAAM,KAAK,OAAO,MAAM,MAAM;AAAA,EACnE,aAAa;AAAA,EACb,YAAY;AAAA,EACZ,YAAY;AACb;AAEO,SAAS,UAAU,MAAa,gBAAgB,OAAiB;AACvE,QAAM,QAAQ,KAAK,eAAgB,CAAC;AACpC,SAAO;AAAA,IACN,IAAI,cAAc,KAAK,EAAE;AAAA,IACzB,OAAO,cAAc,KAAK,KAAK;AAAA,IAC/B,WAAW,cAAc,KAAK,SAAS;AAAA,IACvC,UAAU,cAAc,KAAK,QAAQ;AAAA,IACrC,OAAO,cAAc,KAAK,KAAK;AAAA,IAC/B,iBAAiB,cAAc,KAAK,eAAe;AAAA,IACnD,UAAU,OAAO,KAAK,aAAa,YAAY,KAAK,WAAW;AAAA,IAC/D,WAAW,KAAK,qBAAqB,OAAO,KAAK,UAAU,YAAY,IAAI;AAAA,IAC3E,aAAa;AAAA,MACZ,GAAG;AAAA,MACH,GAAG;AAAA,MACH,QAAQ,KAAK,UAAU,MAAM,UAAU;AAAA,MACvC,UAAU,KAAK,YAAY,MAAM,YAAY;AAAA,IAC9C;AAAA,IACA,iBAAiB,KAAK,oBAAoB;AAAA,IAC1C,iBAAiB;AAAA,IACjB,YAAY,eAAe,KAAK,UAAU;AAAA,IAC1C,WAAW,eAAe,KAAK,SAAS;AAAA,IACxC,WAAW,eAAe,KAAK,SAAS;AAAA,IACxC,aAAa,MAAM,QAAQ,KAAK,WAAW,IAAI,KAAK,cAAc,CAAC;AAAA,IACnE,WAAW,KAAK,qBAAqB,OAAO,KAAK,UAAU,YAAY,IAAI;AAAA,IAC3E,WAAW,KAAK,qBAAqB,OAAO,KAAK,UAAU,YAAY,IAAI;AAAA,EAC5E;AACD;","names":[]}