@fonderie/auth 7.4.0 → 7.5.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.
package/brain/outcomes.md CHANGED
@@ -150,6 +150,7 @@ Raw SQL ships in `node_modules/@fonderie/auth/dist/migrations/sql/` — read it
150
150
  | POST | `/auth/mfa/disable` | `requireAuth → requireEmailLogin → requireVerified → validate(mfaTokenSchema) → mfa.disable` |
151
151
  | POST | `/auth/mfa/setup` | `requireAuth → requireEmailLogin → requireVerified → mfa.setup` |
152
152
  | POST | `/auth/mfa/verify` | `ipLimit('mfaVerify') → requireAnyAuth → requireEmailLogin → requireVerified → validate(mfaTokenSchema) → mfa.verify` |
153
+ | DELETE | `/auth/oauth/:provider` | `requireAuth → user.unlinkOauth` |
153
154
  | GET | `/auth/providers` | `async () => setApiResponse(HTTP.OK, 'AUTH_PROVIDERS', 'Sign-in methods available here', { providers: [...config.providers], })` |
154
155
  | POST | `/auth/refresh` | `validate(refreshSchema) → auth.refresh` |
155
156
  | POST | `/auth/register` | `ipLimit('register') → validate(registerSchema) → auth.register` |
@@ -28,6 +28,7 @@ interface IUser {
28
28
  mfaEnabled: boolean;
29
29
  passwordHash: string | null;
30
30
  emailVerifiedAt: Date | null;
31
+ provider: string | null;
31
32
  }
32
33
 
33
34
  interface ISession {
@@ -99,11 +100,11 @@ interface IDataExportContributor {
99
100
 
100
101
  const AUTH_CONFIG_KEYS: { sessionDuration: string; verificationCooldown: string; mfa: string; requireVerification: string; }
101
102
 
102
- const MESSAGE_KEYS: { readonly emailRegistration: "email-registration"; readonly emailVerification: "email-verification"; readonly passwordReset: "password-reset"; readonly phoneOtp: "phone-otp"; readonly mfaEnabled: "mfa-enabled"; readonly mfaDisabled: "mfa-disabled"; readonly mfaBackupCodesRegenerated: "mfa-backup-codes-regenerated"; readonly emailChanged: "email-changed"; readonly phoneChanged: "phone-changed"; }
103
+ const MESSAGE_KEYS: { readonly emailRegistration: "email-registration"; readonly emailVerification: "email-verification"; readonly passwordReset: "password-reset"; readonly phoneOtp: "phone-otp"; readonly mfaEnabled: "mfa-enabled"; readonly mfaDisabled: "mfa-disabled"; readonly mfaBackupCodesRegenerated: "mfa-backup-codes-regenerated"; readonly emailChanged: "email-changed"; readonly phoneChanged: "phone-changed"; readonly oauthRegistration: "oauth-registration"; readonly oauthLinked: "oauth-linked"; readonly oauthUnlinked: "oauth-unlinked"; }
103
104
 
104
105
  type AuthMessageKey = (typeof MESSAGE_KEYS)[keyof typeof MESSAGE_KEYS];
105
106
 
106
- const DEFAULT_TEMPLATES: { "email-registration": { subject: string; html: string; text: string; }; "email-verification": { subject: string; html: string; text: string; }; "password-reset": { subject: string; html: string; text: string; }; "phone-otp": { text: string; }; "mfa-enabled": { subject: string; html: string; text: string; }; "mfa-disabled": { subject: string; html: string; text: string; }; "mfa-backup-codes-regenerated": { subject: string; html: string; text: string; }; "email-changed": { subject: string; html: string; text: string; }; "phone-changed": { subject: string; html: string; text: string; }; }
107
+ const DEFAULT_TEMPLATES: { "email-registration": { subject: string; html: string; text: string; }; "email-verification": { subject: string; html: string; text: string; }; "password-reset": { subject: string; html: string; text: string; }; "phone-otp": { text: string; }; "mfa-enabled": { subject: string; html: string; text: string; }; "mfa-disabled": { subject: string; html: string; text: string; }; "mfa-backup-codes-regenerated": { subject: string; html: string; text: string; }; "email-changed": { subject: string; html: string; text: string; }; "phone-changed": { subject: string; html: string; text: string; }; "oauth-registration": { subject: string; html: string; text: string; }; "oauth-linked": { subject: string; html: string; text: string; }; "oauth-unlinked": { subject: string; html: string; text: string; }; }
107
108
 
108
109
  interface IUserDTO {
109
110
  id: string;
@@ -118,6 +119,8 @@ interface IUserDTO {
118
119
  isEmailVerified: boolean;
119
120
  isPhoneVerified: boolean;
120
121
  mfaEnabled: boolean;
122
+ provider: string;
123
+ hasPassword: boolean;
121
124
  suspended: boolean;
122
125
  whitelist: boolean;
123
126
  ipWhitelist: string[];
@@ -75,6 +75,8 @@ function toUserDTO(user, phoneVerified = false) {
75
75
  isEmailVerified: user.emailVerifiedAt !== null,
76
76
  isPhoneVerified: phoneVerified,
77
77
  mfaEnabled: (0, import_core.booleanOrFalse)(user.mfaEnabled),
78
+ provider: (0, import_core.stringOrEmpty)(user.provider),
79
+ hasPassword: user.passwordHash !== null && user.passwordHash !== void 0,
78
80
  suspended: (0, import_core.booleanOrFalse)(user.suspended),
79
81
  whitelist: (0, import_core.booleanOrFalse)(user.whitelist),
80
82
  ipWhitelist: Array.isArray(user.ipWhitelist) ? user.ipWhitelist : [],
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/dtos/user.ts"],"sourcesContent":["import { stringOrEmpty, booleanOrFalse, dateOrEmpty } 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\n// Historical rows may hold garbage preferences (the update schema accepted\n// `unknown` for four keys until it was typed). Only well-typed values\n// survive into the DTO, and notifications deep-merge over the defaults so a\n// partial stored object can't shrink the shape the client type promises.\nfunction cleanPreferences(prefs: IUserPreferences): Partial<IUserPreferences> {\n\tconst out: Partial<IUserPreferences> = {};\n\tif (typeof prefs.locale === 'string') out.locale = prefs.locale;\n\tif (typeof prefs.timezone === 'string') out.timezone = prefs.timezone;\n\tif (typeof prefs.emailDigest === 'string') out.emailDigest = prefs.emailDigest;\n\tif (typeof prefs.dateFormat === 'string') out.dateFormat = prefs.dateFormat;\n\tif (typeof prefs.timeFormat === 'string') out.timeFormat = prefs.timeFormat;\n\treturn out;\n}\n\nfunction cleanNotifications(value: unknown): Partial<IUserPreferences['notifications']> {\n\tif (!value || typeof value !== 'object') return {};\n\tconst out: Partial<IUserPreferences['notifications']> = {};\n\tfor (const key of ['email', 'inApp', 'sms', 'push'] as const) {\n\t\tconst v = (value as Record<string, unknown>)[key];\n\t\tif (typeof v === 'boolean') out[key] = v;\n\t}\n\treturn out;\n}\n\nexport function toUserDTO(user: IUser, phoneVerified = false): IUserDTO {\n\tconst prefs = user.preferences ?? ({} as IUserPreferences);\n\tconst cleaned = cleanPreferences(prefs);\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: dateOrEmpty(user.lastLogin),\n\t\tpreferences: {\n\t\t\t...DEFAULT_PREFERENCES,\n\t\t\t...cleaned,\n\t\t\tnotifications: {\n\t\t\t\t...DEFAULT_PREFERENCES.notifications,\n\t\t\t\t...cleanNotifications(prefs.notifications),\n\t\t\t},\n\t\t\tlocale: user.locale || cleaned.locale || 'en-US',\n\t\t\ttimezone: user.timezone || cleaned.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: dateOrEmpty(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Ac,KAAK,KAAK;AAAA,IAC/B,qBAAiB,2BAAc,KAAK,eAAe;AAAA,IACnD,UAAU,OAAO,KAAK,aAAa,YAAY,KAAK,WAAW;AAAA,IAC/D,eAAW,yBAAY,KAAK,SAAS;AAAA,IACrC,aAAa;AAAA,MACZ,GAAG;AAAA,MACH,GAAG;AAAA,MACH,eAAe;AAAA,QACd,GAAG,oBAAoB;AAAA,QACvB,GAAG,mBAAmB,MAAM,aAAa;AAAA,MAC1C;AAAA,MACA,QAAQ,KAAK,UAAU,QAAQ,UAAU;AAAA,MACzC,UAAU,KAAK,YAAY,QAAQ,YAAY;AAAA,IAChD;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,eAAW,yBAAY,KAAK,SAAS;AAAA,IACrC,eAAW,yBAAY,KAAK,SAAS;AAAA,EACtC;AACD;","names":[]}
1
+ {"version":3,"sources":["../../src/dtos/user.ts"],"sourcesContent":["import { stringOrEmpty, booleanOrFalse, dateOrEmpty } 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\t// OAuth provider linked to this account ('google', 'apple'), or '' if none.\n\tprovider: string;\n\t// Whether a password is set. Together with `provider` this is everything a\n\t// settings screen needs to render sign-in methods AND to know that\n\t// disconnecting the provider is possible — an account with no password has\n\t// no other credential, so unlinking is refused (409 PASSWORD_REQUIRED).\n\t// Exposed as a boolean precisely so the hash never can be.\n\thasPassword: 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\n// Historical rows may hold garbage preferences (the update schema accepted\n// `unknown` for four keys until it was typed). Only well-typed values\n// survive into the DTO, and notifications deep-merge over the defaults so a\n// partial stored object can't shrink the shape the client type promises.\nfunction cleanPreferences(prefs: IUserPreferences): Partial<IUserPreferences> {\n\tconst out: Partial<IUserPreferences> = {};\n\tif (typeof prefs.locale === 'string') out.locale = prefs.locale;\n\tif (typeof prefs.timezone === 'string') out.timezone = prefs.timezone;\n\tif (typeof prefs.emailDigest === 'string') out.emailDigest = prefs.emailDigest;\n\tif (typeof prefs.dateFormat === 'string') out.dateFormat = prefs.dateFormat;\n\tif (typeof prefs.timeFormat === 'string') out.timeFormat = prefs.timeFormat;\n\treturn out;\n}\n\nfunction cleanNotifications(value: unknown): Partial<IUserPreferences['notifications']> {\n\tif (!value || typeof value !== 'object') return {};\n\tconst out: Partial<IUserPreferences['notifications']> = {};\n\tfor (const key of ['email', 'inApp', 'sms', 'push'] as const) {\n\t\tconst v = (value as Record<string, unknown>)[key];\n\t\tif (typeof v === 'boolean') out[key] = v;\n\t}\n\treturn out;\n}\n\nexport function toUserDTO(user: IUser, phoneVerified = false): IUserDTO {\n\tconst prefs = user.preferences ?? ({} as IUserPreferences);\n\tconst cleaned = cleanPreferences(prefs);\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: dateOrEmpty(user.lastLogin),\n\t\tpreferences: {\n\t\t\t...DEFAULT_PREFERENCES,\n\t\t\t...cleaned,\n\t\t\tnotifications: {\n\t\t\t\t...DEFAULT_PREFERENCES.notifications,\n\t\t\t\t...cleanNotifications(prefs.notifications),\n\t\t\t},\n\t\t\tlocale: user.locale || cleaned.locale || 'en-US',\n\t\t\ttimezone: user.timezone || cleaned.timezone || 'UTC',\n\t\t},\n\t\tisEmailVerified: user.emailVerifiedAt !== null,\n\t\tisPhoneVerified: phoneVerified,\n\t\tmfaEnabled: booleanOrFalse(user.mfaEnabled),\n\t\tprovider: stringOrEmpty(user.provider),\n\t\thasPassword: user.passwordHash !== null && user.passwordHash !== undefined,\n\t\tsuspended: booleanOrFalse(user.suspended),\n\t\twhitelist: booleanOrFalse(user.whitelist),\n\t\tipWhitelist: Array.isArray(user.ipWhitelist) ? user.ipWhitelist : [],\n\t\tcreatedAt: dateOrEmpty(user.createdAt),\n\t\tupdatedAt: dateOrEmpty(user.updatedAt),\n\t};\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,kBAA2D;AAgC3D,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;AAMA,SAAS,iBAAiB,OAAoD;AAC7E,QAAM,MAAiC,CAAC;AACxC,MAAI,OAAO,MAAM,WAAW,SAAU,KAAI,SAAS,MAAM;AACzD,MAAI,OAAO,MAAM,aAAa,SAAU,KAAI,WAAW,MAAM;AAC7D,MAAI,OAAO,MAAM,gBAAgB,SAAU,KAAI,cAAc,MAAM;AACnE,MAAI,OA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iB,QAAQ,KAAK,iBAAiB;AAAA,IACjE,eAAW,4BAAe,KAAK,SAAS;AAAA,IACxC,eAAW,4BAAe,KAAK,SAAS;AAAA,IACxC,aAAa,MAAM,QAAQ,KAAK,WAAW,IAAI,KAAK,cAAc,CAAC;AAAA,IACnE,eAAW,yBAAY,KAAK,SAAS;AAAA,IACrC,eAAW,yBAAY,KAAK,SAAS;AAAA,EACtC;AACD;","names":[]}
@@ -13,6 +13,8 @@ interface IUserDTO {
13
13
  isEmailVerified: boolean;
14
14
  isPhoneVerified: boolean;
15
15
  mfaEnabled: boolean;
16
+ provider: string;
17
+ hasPassword: boolean;
16
18
  suspended: boolean;
17
19
  whitelist: boolean;
18
20
  ipWhitelist: string[];
@@ -13,6 +13,8 @@ interface IUserDTO {
13
13
  isEmailVerified: boolean;
14
14
  isPhoneVerified: boolean;
15
15
  mfaEnabled: boolean;
16
+ provider: string;
17
+ hasPassword: boolean;
16
18
  suspended: boolean;
17
19
  whitelist: boolean;
18
20
  ipWhitelist: string[];
package/dist/dtos/user.js CHANGED
@@ -51,6 +51,8 @@ function toUserDTO(user, phoneVerified = false) {
51
51
  isEmailVerified: user.emailVerifiedAt !== null,
52
52
  isPhoneVerified: phoneVerified,
53
53
  mfaEnabled: booleanOrFalse(user.mfaEnabled),
54
+ provider: stringOrEmpty(user.provider),
55
+ hasPassword: user.passwordHash !== null && user.passwordHash !== void 0,
54
56
  suspended: booleanOrFalse(user.suspended),
55
57
  whitelist: booleanOrFalse(user.whitelist),
56
58
  ipWhitelist: Array.isArray(user.ipWhitelist) ? user.ipWhitelist : [],
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/dtos/user.ts"],"sourcesContent":["import { stringOrEmpty, booleanOrFalse, dateOrEmpty } 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\n// Historical rows may hold garbage preferences (the update schema accepted\n// `unknown` for four keys until it was typed). Only well-typed values\n// survive into the DTO, and notifications deep-merge over the defaults so a\n// partial stored object can't shrink the shape the client type promises.\nfunction cleanPreferences(prefs: IUserPreferences): Partial<IUserPreferences> {\n\tconst out: Partial<IUserPreferences> = {};\n\tif (typeof prefs.locale === 'string') out.locale = prefs.locale;\n\tif (typeof prefs.timezone === 'string') out.timezone = prefs.timezone;\n\tif (typeof prefs.emailDigest === 'string') out.emailDigest = prefs.emailDigest;\n\tif (typeof prefs.dateFormat === 'string') out.dateFormat = prefs.dateFormat;\n\tif (typeof prefs.timeFormat === 'string') out.timeFormat = prefs.timeFormat;\n\treturn out;\n}\n\nfunction cleanNotifications(value: unknown): Partial<IUserPreferences['notifications']> {\n\tif (!value || typeof value !== 'object') return {};\n\tconst out: Partial<IUserPreferences['notifications']> = {};\n\tfor (const key of ['email', 'inApp', 'sms', 'push'] as const) {\n\t\tconst v = (value as Record<string, unknown>)[key];\n\t\tif (typeof v === 'boolean') out[key] = v;\n\t}\n\treturn out;\n}\n\nexport function toUserDTO(user: IUser, phoneVerified = false): IUserDTO {\n\tconst prefs = user.preferences ?? ({} as IUserPreferences);\n\tconst cleaned = cleanPreferences(prefs);\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: dateOrEmpty(user.lastLogin),\n\t\tpreferences: {\n\t\t\t...DEFAULT_PREFERENCES,\n\t\t\t...cleaned,\n\t\t\tnotifications: {\n\t\t\t\t...DEFAULT_PREFERENCES.notifications,\n\t\t\t\t...cleanNotifications(prefs.notifications),\n\t\t\t},\n\t\t\tlocale: user.locale || cleaned.locale || 'en-US',\n\t\t\ttimezone: user.timezone || cleaned.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: dateOrEmpty(user.createdAt),\n\t\tupdatedAt: dateOrEmpty(user.updatedAt),\n\t};\n}\n"],"mappings":";AAAA,SAAS,eAAe,gBAAgB,mBAAmB;AAwB3D,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;AAMA,SAAS,iBAAiB,OAAoD;AAC7E,QAAM,MAAiC,CAAC;AACxC,MAAI,OAAO,MAAM,WAAW,SAAU,KAAI,SAAS,MAAM;AACzD,MAAI,OAAO,MAAM,aAAa,SAAU,KAAI,WAAW,MAAM;AAC7D,MAAI,OAAO,MAAM,gBAAgB,SAAU,KAAI,cAAc,MAAM;AACnE,MAAI,OAAO,MAAM,eAAe,SAAU,KAAI,aAAa,MAAM;AACjE,MAAI,OAAO,MAA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AAAA,IACnE,WAAW,YAAY,KAAK,SAAS;AAAA,IACrC,WAAW,YAAY,KAAK,SAAS;AAAA,EACtC;AACD;","names":[]}
1
+ {"version":3,"sources":["../../src/dtos/user.ts"],"sourcesContent":["import { stringOrEmpty, booleanOrFalse, dateOrEmpty } 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\t// OAuth provider linked to this account ('google', 'apple'), or '' if none.\n\tprovider: string;\n\t// Whether a password is set. Together with `provider` this is everything a\n\t// settings screen needs to render sign-in methods AND to know that\n\t// disconnecting the provider is possible — an account with no password has\n\t// no other credential, so unlinking is refused (409 PASSWORD_REQUIRED).\n\t// Exposed as a boolean precisely so the hash never can be.\n\thasPassword: 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\n// Historical rows may hold garbage preferences (the update schema accepted\n// `unknown` for four keys until it was typed). Only well-typed values\n// survive into the DTO, and notifications deep-merge over the defaults so a\n// partial stored object can't shrink the shape the client type promises.\nfunction cleanPreferences(prefs: IUserPreferences): Partial<IUserPreferences> {\n\tconst out: Partial<IUserPreferences> = {};\n\tif (typeof prefs.locale === 'string') out.locale = prefs.locale;\n\tif (typeof prefs.timezone === 'string') out.timezone = prefs.timezone;\n\tif (typeof prefs.emailDigest === 'string') out.emailDigest = prefs.emailDigest;\n\tif (typeof prefs.dateFormat === 'string') out.dateFormat = prefs.dateFormat;\n\tif (typeof prefs.timeFormat === 'string') out.timeFormat = prefs.timeFormat;\n\treturn out;\n}\n\nfunction cleanNotifications(value: unknown): Partial<IUserPreferences['notifications']> {\n\tif (!value || typeof value !== 'object') return {};\n\tconst out: Partial<IUserPreferences['notifications']> = {};\n\tfor (const key of ['email', 'inApp', 'sms', 'push'] as const) {\n\t\tconst v = (value as Record<string, unknown>)[key];\n\t\tif (typeof v === 'boolean') out[key] = v;\n\t}\n\treturn out;\n}\n\nexport function toUserDTO(user: IUser, phoneVerified = false): IUserDTO {\n\tconst prefs = user.preferences ?? ({} as IUserPreferences);\n\tconst cleaned = cleanPreferences(prefs);\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: dateOrEmpty(user.lastLogin),\n\t\tpreferences: {\n\t\t\t...DEFAULT_PREFERENCES,\n\t\t\t...cleaned,\n\t\t\tnotifications: {\n\t\t\t\t...DEFAULT_PREFERENCES.notifications,\n\t\t\t\t...cleanNotifications(prefs.notifications),\n\t\t\t},\n\t\t\tlocale: user.locale || cleaned.locale || 'en-US',\n\t\t\ttimezone: user.timezone || cleaned.timezone || 'UTC',\n\t\t},\n\t\tisEmailVerified: user.emailVerifiedAt !== null,\n\t\tisPhoneVerified: phoneVerified,\n\t\tmfaEnabled: booleanOrFalse(user.mfaEnabled),\n\t\tprovider: stringOrEmpty(user.provider),\n\t\thasPassword: user.passwordHash !== null && user.passwordHash !== undefined,\n\t\tsuspended: booleanOrFalse(user.suspended),\n\t\twhitelist: booleanOrFalse(user.whitelist),\n\t\tipWhitelist: Array.isArray(user.ipWhitelist) ?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cAAc,KAAK,KAAK;AAAA,IAC/B,iBAAiB,cAAc,KAAK,eAAe;AAAA,IACnD,UAAU,OAAO,KAAK,aAAa,YAAY,KAAK,WAAW;AAAA,IAC/D,WAAW,YAAY,KAAK,SAAS;AAAA,IACrC,aAAa;AAAA,MACZ,GAAG;AAAA,MACH,GAAG;AAAA,MACH,eAAe;AAAA,QACd,GAAG,oBAAoB;AAAA,QACvB,GAAG,mBAAmB,MAAM,aAAa;AAAA,MAC1C;AAAA,MACA,QAAQ,KAAK,UAAU,QAAQ,UAAU;AAAA,MACzC,UAAU,KAAK,YAAY,QAAQ,YAAY;AAAA,IAChD;AAAA,IACA,iBAAiB,KAAK,oBAAoB;AAAA,IAC1C,iBAAiB;AAAA,IACjB,YAAY,eAAe,KAAK,UAAU;AAAA,IAC1C,UAAU,cAAc,KAAK,QAAQ;AAAA,IACrC,aAAa,KAAK,iBAAiB,QAAQ,KAAK,iBAAiB;AAAA,IACjE,WAAW,eAAe,KAAK,SAAS;AAAA,IACxC,WAAW,eAAe,KAAK,SAAS;AAAA,IACxC,aAAa,MAAM,QAAQ,KAAK,WAAW,IAAI,KAAK,cAAc,CAAC;AAAA,IACnE,WAAW,YAAY,KAAK,SAAS;AAAA,IACrC,WAAW,YAAY,KAAK,SAAS;AAAA,EACtC;AACD;","names":[]}
package/dist/index.cjs CHANGED
@@ -296,7 +296,16 @@ var MESSAGE_KEYS = {
296
296
  mfaDisabled: "mfa-disabled",
297
297
  mfaBackupCodesRegenerated: "mfa-backup-codes-regenerated",
298
298
  emailChanged: "email-changed",
299
- phoneChanged: "phone-changed"
299
+ phoneChanged: "phone-changed",
300
+ // Changing HOW an account can be signed into is a security event for the
301
+ // account's owner, who may not be the person doing it. Both directions are
302
+ // notified for the same reason password changes are.
303
+ // Welcome for an account CREATED by an OAuth sign-in. Distinct from
304
+ // emailRegistration, which carries a verification PIN — an OAuth signup
305
+ // arrives already verified by the provider and has nothing to confirm.
306
+ oauthRegistration: "oauth-registration",
307
+ oauthLinked: "oauth-linked",
308
+ oauthUnlinked: "oauth-unlinked"
300
309
  };
301
310
  var EVENT_KEYS = {
302
311
  userRegistered: "fonderie.user.registered",
@@ -528,6 +537,8 @@ function toUserDTO(user, phoneVerified = false) {
528
537
  isEmailVerified: user.emailVerifiedAt !== null,
529
538
  isPhoneVerified: phoneVerified,
530
539
  mfaEnabled: (0, import_core3.booleanOrFalse)(user.mfaEnabled),
540
+ provider: (0, import_core3.stringOrEmpty)(user.provider),
541
+ hasPassword: user.passwordHash !== null && user.passwordHash !== void 0,
531
542
  suspended: (0, import_core3.booleanOrFalse)(user.suspended),
532
543
  whitelist: (0, import_core3.booleanOrFalse)(user.whitelist),
533
544
  ipWhitelist: Array.isArray(user.ipWhitelist) ? user.ipWhitelist : [],
@@ -555,6 +566,7 @@ var USER_COLUMNS = `
555
566
  ip_whitelist AS "ipWhitelist",
556
567
  mfa_enabled AS "mfaEnabled",
557
568
  email_verified_at AS "emailVerifiedAt",
569
+ provider,
558
570
  deleted_at AS "deletedAt",
559
571
  created_at AS "createdAt",
560
572
  updated_at AS "updatedAt"
@@ -632,6 +644,31 @@ var UserModel = class {
632
644
  );
633
645
  return row ?? null;
634
646
  }
647
+ /**
648
+ * Clear the OAuth provider, but ONLY when the account can still be signed
649
+ * into afterwards.
650
+ *
651
+ * An account created through Google has no password. Unlinking it without
652
+ * checking would not be "removing a sign-in method" — it would be locking
653
+ * the owner out permanently, which is account deletion wearing a friendlier
654
+ * label. The guard lives in the WHERE clause so the check and the write are
655
+ * one atomic statement: a password cannot be removed between them.
656
+ *
657
+ * Returns false when the account has no password (nothing was changed) so
658
+ * the caller can say why rather than reporting a silent success.
659
+ */
660
+ async clearProvider(id) {
661
+ const rows = await this.store.query(
662
+ `UPDATE fonderie_users
663
+ SET provider = NULL, provider_id = NULL
664
+ WHERE id = $1
665
+ AND password_hash IS NOT NULL
666
+ AND provider IS NOT NULL
667
+ RETURNING id`,
668
+ [id]
669
+ );
670
+ return rows.length > 0;
671
+ }
635
672
  async updatePassword(id, passwordHash) {
636
673
  await this.store.query(`UPDATE fonderie_users SET password_hash = $1 WHERE id = $2`, [
637
674
  passwordHash,
@@ -732,14 +769,44 @@ var UserModel = class {
732
769
  );
733
770
  return row?.mfa_secret ?? null;
734
771
  }
772
+ /**
773
+ * Link an OAuth identity to the account owning `email`, creating it if there
774
+ * is none.
775
+ *
776
+ * Returns what HAPPENED, not just who it happened to. Three outcomes hide
777
+ * behind one upsert, and callers must tell them apart:
778
+ *
779
+ * • `inserted` — a brand-new account. Whoever provisions on signup (the
780
+ * personal workspace, a wallet, a welcome email) must run, exactly as
781
+ * it does for a password registration. This is the case that was
782
+ * previously invisible: the upsert returned an id and looked identical
783
+ * to a returning user signing in, so OAuth signups silently skipped
784
+ * every signup side effect.
785
+ * • `previousProvider` differs — an existing account gained, or switched,
786
+ * a way to sign in. A security event for the owner.
787
+ * • `previousProvider` is the same — an ordinary login. Nothing to do, and
788
+ * notifying here would email the user on every single sign-in.
789
+ *
790
+ * The prior state is read in a CTE of the SAME statement, so it sees the
791
+ * snapshot from before the insert. Reading it in a separate query would
792
+ * race two concurrent logins into both seeing "new".
793
+ */
735
794
  async upsertByProvider(email2, provider, providerId) {
736
795
  const [row] = await this.store.query(
737
- `INSERT INTO fonderie_users (email, email_verified_at, provider, provider_id)
738
- VALUES ($1, now(), $2, $3)
739
- ON CONFLICT (email) DO UPDATE
740
- SET provider = $2, provider_id = $3,
741
- email_verified_at = COALESCE(fonderie_users.email_verified_at, now())
742
- RETURNING id`,
796
+ `WITH prior AS (
797
+ SELECT provider FROM fonderie_users WHERE email = $1
798
+ ), upserted AS (
799
+ INSERT INTO fonderie_users (email, email_verified_at, provider, provider_id)
800
+ VALUES ($1, now(), $2, $3)
801
+ ON CONFLICT (email) DO UPDATE
802
+ SET provider = $2, provider_id = $3,
803
+ email_verified_at = COALESCE(fonderie_users.email_verified_at, now())
804
+ RETURNING id
805
+ )
806
+ SELECT upserted.id,
807
+ NOT EXISTS (SELECT 1 FROM prior) AS inserted,
808
+ (SELECT provider FROM prior) AS "previousProvider"
809
+ FROM upserted`,
743
810
  [email2, provider, providerId]
744
811
  );
745
812
  return row ?? null;
@@ -2232,6 +2299,50 @@ function userController(store, config, bus) {
2232
2299
  await sessions.deleteByUser(ctx.user.id);
2233
2300
  return (0, import_core8.setApiResponse)(import_core8.HTTP.OK, "PASSWORD_CHANGED", "Password updated successfully.");
2234
2301
  },
2302
+ /**
2303
+ * Remove an OAuth provider from the account.
2304
+ *
2305
+ * Refuses when the account has no password, because unlinking would
2306
+ * then leave NO way to sign in — that is account deletion, not a
2307
+ * settings toggle, and a user clicking "disconnect Google" is not
2308
+ * asking for it. The check is inside the UPDATE's WHERE clause, so a
2309
+ * password cannot disappear between the check and the write.
2310
+ *
2311
+ * Notifies the account's email either way: changing how an account can
2312
+ * be signed into is a security event for its owner, who is not
2313
+ * necessarily the person doing it.
2314
+ */
2315
+ unlinkOauth: async (ctx) => {
2316
+ const provider = String(ctx.meta["params"]?.["provider"] ?? "");
2317
+ const current = await users.findById(ctx.user.id);
2318
+ if (!current?.provider) {
2319
+ return (0, import_core8.setApiResponse)(import_core8.HTTP.NOT_FOUND, "NOT_LINKED", "No sign-in provider is linked.");
2320
+ }
2321
+ if (provider && provider.toLowerCase() !== current.provider.toLowerCase()) {
2322
+ return (0, import_core8.setApiResponse)(
2323
+ import_core8.HTTP.NOT_FOUND,
2324
+ "NOT_LINKED",
2325
+ `This account is not linked to ${provider}.`
2326
+ );
2327
+ }
2328
+ const removed = await users.clearProvider(ctx.user.id);
2329
+ if (!removed) {
2330
+ return (0, import_core8.setApiResponse)(
2331
+ import_core8.HTTP.CONFLICT,
2332
+ "PASSWORD_REQUIRED",
2333
+ "Set a password before disconnecting this provider \u2014 it is currently the only way to sign in."
2334
+ );
2335
+ }
2336
+ if (ctx.user.email) {
2337
+ await (0, import_core8.background)(bus?.emit(import_events3.NOTIFICATION_EVENT, {
2338
+ type: MESSAGE_KEYS.oauthUnlinked,
2339
+ locale: ctx.user.locale,
2340
+ data: { provider: current.provider },
2341
+ recipient: { email: ctx.user.email, phone: null, deviceToken: null }
2342
+ }));
2343
+ }
2344
+ return (0, import_core8.setApiResponse)(import_core8.HTTP.OK, "OAUTH_UNLINKED", "Sign-in provider removed.");
2345
+ },
2235
2346
  deleteMe: async (ctx) => {
2236
2347
  const userId = ctx.user.id;
2237
2348
  await users.softDelete(userId);
@@ -2292,6 +2403,7 @@ function userController(store, config, bus) {
2292
2403
  var import_node_crypto7 = require("crypto");
2293
2404
  var import_jsonwebtoken2 = __toESM(require("jsonwebtoken"), 1);
2294
2405
  var import_core9 = require("@fonderie/core");
2406
+ var import_events4 = require("@fonderie/events");
2295
2407
 
2296
2408
  // src/models/consumed-token.model.ts
2297
2409
  var ConsumedTokenModel = class {
@@ -2392,11 +2504,56 @@ async function verifyAppleIdToken(idToken, opts) {
2392
2504
  }
2393
2505
  }
2394
2506
  var appleEmailVerified = (c) => c.email_verified === true || c.email_verified === "true";
2395
- function oauthController(store, config) {
2507
+ function oauthController(store, config, bus) {
2396
2508
  const users = new UserModel(store);
2397
2509
  const sessions = new SessionModel(store);
2398
2510
  const loginEvents = new LoginEventModel(store);
2399
2511
  const consumedTokens = new ConsumedTokenModel(store);
2512
+ const announceOAuthUpsert = async (ctx, upserted, provider, user) => {
2513
+ if (!bus) return;
2514
+ const reqId = ctx.meta["requestId"];
2515
+ const reqOpts = reqId !== void 0 ? { requestId: reqId } : void 0;
2516
+ const label = provider.charAt(0).toUpperCase() + provider.slice(1);
2517
+ const recipient = { email: user.email, phone: null, deviceToken: null };
2518
+ if (upserted.inserted) {
2519
+ await (0, import_core9.background)(bus.emit(
2520
+ EVENT_KEYS.userRegistered,
2521
+ {
2522
+ userId: upserted.id,
2523
+ email: user.email,
2524
+ firstName: user.firstName,
2525
+ lastName: user.lastName,
2526
+ loginMethod: provider
2527
+ },
2528
+ reqOpts
2529
+ ));
2530
+ if (user.email) {
2531
+ await (0, import_core9.background)(bus.emit(
2532
+ import_events4.NOTIFICATION_EVENT,
2533
+ {
2534
+ type: MESSAGE_KEYS.oauthRegistration,
2535
+ locale: user.locale,
2536
+ data: { provider: label, appName: config.appName ?? "Fonderie" },
2537
+ recipient
2538
+ },
2539
+ reqOpts
2540
+ ));
2541
+ }
2542
+ return;
2543
+ }
2544
+ if (upserted.previousProvider !== provider && user.email) {
2545
+ await (0, import_core9.background)(bus.emit(
2546
+ import_events4.NOTIFICATION_EVENT,
2547
+ {
2548
+ type: MESSAGE_KEYS.oauthLinked,
2549
+ locale: user.locale,
2550
+ data: { provider: label },
2551
+ recipient
2552
+ },
2553
+ reqOpts
2554
+ ));
2555
+ }
2556
+ };
2400
2557
  const completeAppleLogin = async (ctx, claims) => {
2401
2558
  const meta = requestMeta(ctx);
2402
2559
  if (!claims.email) {
@@ -2425,6 +2582,7 @@ function oauthController(store, config) {
2425
2582
  if (!fullUser) {
2426
2583
  return (0, import_core9.setApiResponse)(import_core9.HTTP.SERVER_ERROR, "SERVER_ERROR", "Apple login failed");
2427
2584
  }
2585
+ await announceOAuthUpsert(ctx, upserted, "apple", fullUser);
2428
2586
  const { accessToken, refreshToken, sid } = issueTokenPair(upserted.id, config, { loginMethod: "apple" });
2429
2587
  await sessions.create(upserted.id, refreshToken, refreshTokenExpiry(refreshToken), sid, meta);
2430
2588
  await loginEvents.recordSafe({
@@ -2575,6 +2733,7 @@ function oauthController(store, config) {
2575
2733
  if (!fullUser) {
2576
2734
  return (0, import_core9.setApiResponse)(import_core9.HTTP.SERVER_ERROR, "SERVER_ERROR", "OAuth login failed");
2577
2735
  }
2736
+ await announceOAuthUpsert(ctx, upserted, "google", fullUser);
2578
2737
  const { accessToken, refreshToken, sid } = issueTokenPair(upserted.id, config, {
2579
2738
  loginMethod: "google"
2580
2739
  });
@@ -2739,7 +2898,7 @@ function oauthController(store, config) {
2739
2898
  function buildAuthRoutes(store, config, bus) {
2740
2899
  const user = userController(store, config, bus);
2741
2900
  const auth = authController(store, config, bus);
2742
- const oauth = oauthController(store, config);
2901
+ const oauth = oauthController(store, config, bus);
2743
2902
  const mfa = mfaController(store, config, config.appName ?? "Fonderie", bus);
2744
2903
  const verifyGate = config.requireVerification ? import_middlewares2.requireVerified : (_ctx, next) => next();
2745
2904
  const passthrough = (_ctx, next) => next();
@@ -2797,6 +2956,9 @@ function buildAuthRoutes(store, config, bus) {
2797
2956
  R("updateEmail", "PUT", "/users/email", import_middlewares2.requireAuth, verifyGate, (0, import_middlewares.validate)(updateEmailSchema), user.updateEmail),
2798
2957
  R("updatePhone", "PUT", "/users/phone", import_middlewares2.requireAuth, verifyGate, (0, import_middlewares.validate)(updatePhoneSchema), user.updatePhone),
2799
2958
  R("changePassword", "PUT", "/users/password", import_middlewares2.requireAuth, (0, import_middlewares.validate)(changePasswordSchema), user.changePassword),
2959
+ // Disconnect an OAuth provider. requireAuth only — the controller
2960
+ // refuses when it would leave the account with no way to sign in.
2961
+ R("unlinkOauth", "DELETE", "/auth/oauth/:provider", import_middlewares2.requireAuth, user.unlinkOauth),
2800
2962
  R("deleteMe", "DELETE", "/users", import_middlewares2.requireAuth, verifyGate, user.deleteMe),
2801
2963
  R("exportMe", "GET", "/users/export", import_middlewares2.requireAuth, user.exportMe),
2802
2964
  // Security surfaces (Protected; the caller's own login history + sessions).
@@ -3109,9 +3271,47 @@ If you made this change, you're all set. If not, contact support right away \u20
3109
3271
  The phone number on your account was just updated.
3110
3272
 
3111
3273
  If you made this change, you're all set. If not, contact support right away \u2014 someone may have access to your account.`
3274
+ },
3275
+ [MESSAGE_KEYS.oauthRegistration]: {
3276
+ subject: "Welcome to {{appName}}",
3277
+ html: `<h1>Welcome to {{appName}}</h1>
3278
+ <p>Your account was created using <strong>{{provider}}</strong>. Sign in any time with the same {{provider}} account &mdash; there is no password to remember.</p>
3279
+ <p class="muted">If you did not create this account, contact support.</p>`,
3280
+ text: `Welcome
3281
+
3282
+ Your account was created using {{provider}}. Sign in any time with the same
3283
+ {{provider}} account \u2014 there is no password to remember.
3284
+
3285
+ If you did not create this account, contact support.`
3286
+ },
3287
+ [MESSAGE_KEYS.oauthLinked]: {
3288
+ subject: "A new sign-in method was added to your account",
3289
+ html: `<h1>{{provider}} sign-in was added</h1>
3290
+ <p>Your account can now also be signed into with <strong>{{provider}}</strong>.</p>
3291
+ <p class="muted">If you did this, nothing more is needed. If not, contact support right away and change your password &mdash; someone else may be able to sign in as you.</p>`,
3292
+ text: `{{provider}} sign-in was added
3293
+
3294
+ Your account can now also be signed into with {{provider}}.
3295
+
3296
+ If you did this, nothing more is needed. If not, contact support right away and
3297
+ change your password \u2014 someone else may be able to sign in as you.`
3298
+ },
3299
+ [MESSAGE_KEYS.oauthUnlinked]: {
3300
+ subject: "A sign-in method was removed from your account",
3301
+ html: `<h1>{{provider}} sign-in was removed</h1>
3302
+ <p><strong>{{provider}}</strong> can no longer be used to sign in. Your email and password still work.</p>
3303
+ <p class="muted">If you did this, you&rsquo;re all set. If not, contact support right away.</p>`,
3304
+ text: `{{provider}} sign-in was removed
3305
+
3306
+ {{provider}} can no longer be used to sign in. Your email and password still work.
3307
+
3308
+ If you did this, you're all set. If not, contact support right away.`
3112
3309
  }
3113
3310
  };
3114
3311
  var SAMPLE_PAYLOADS = {
3312
+ [MESSAGE_KEYS.oauthRegistration]: { provider: "Google", appName: "Fonderie" },
3313
+ [MESSAGE_KEYS.oauthLinked]: { provider: "Google" },
3314
+ [MESSAGE_KEYS.oauthUnlinked]: { provider: "Google" },
3115
3315
  [MESSAGE_KEYS.emailRegistration]: { firstName: "", pin: "123456" },
3116
3316
  [MESSAGE_KEYS.emailVerification]: { firstName: "Ada", pin: "123456" },
3117
3317
  [MESSAGE_KEYS.passwordReset]: {