@techelusive/nest-auth 0.0.0-stage → 0.2.1

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 (340) hide show
  1. package/CHANGELOG.md +71 -0
  2. package/LICENSE +21 -0
  3. package/README.md +204 -2
  4. package/api/openapi.json +4399 -0
  5. package/api/techelusive-authentication.postman_collection.json +3178 -0
  6. package/dist/auth-options.d.ts +156 -0
  7. package/dist/auth-options.js +3 -0
  8. package/dist/auth-options.js.map +1 -0
  9. package/dist/auth-options.validation.d.ts +9 -0
  10. package/dist/auth-options.validation.js +74 -0
  11. package/dist/auth-options.validation.js.map +1 -0
  12. package/dist/auth.module.d.ts +38 -0
  13. package/dist/auth.module.js +173 -0
  14. package/dist/auth.module.js.map +1 -0
  15. package/dist/common/auth-error.d.ts +16 -0
  16. package/dist/common/auth-error.js +23 -0
  17. package/dist/common/auth-error.js.map +1 -0
  18. package/dist/common/auth-exception.filter.d.ts +24 -0
  19. package/dist/common/auth-exception.filter.js +66 -0
  20. package/dist/common/auth-exception.filter.js.map +1 -0
  21. package/dist/common/crypto/jwt.d.ts +89 -0
  22. package/dist/common/crypto/jwt.js +144 -0
  23. package/dist/common/crypto/jwt.js.map +1 -0
  24. package/dist/common/crypto/random-secrets.d.ts +78 -0
  25. package/dist/common/crypto/random-secrets.js +113 -0
  26. package/dist/common/crypto/random-secrets.js.map +1 -0
  27. package/dist/common/crypto/remote-public-keys.d.ts +49 -0
  28. package/dist/common/crypto/remote-public-keys.js +105 -0
  29. package/dist/common/crypto/remote-public-keys.js.map +1 -0
  30. package/dist/common/crypto/totp.d.ts +68 -0
  31. package/dist/common/crypto/totp.js +148 -0
  32. package/dist/common/crypto/totp.js.map +1 -0
  33. package/dist/common/encryption.service.d.ts +36 -0
  34. package/dist/common/encryption.service.js +111 -0
  35. package/dist/common/encryption.service.js.map +1 -0
  36. package/dist/common/identifiers.d.ts +48 -0
  37. package/dist/common/identifiers.js +89 -0
  38. package/dist/common/identifiers.js.map +1 -0
  39. package/dist/common/network-safety.d.ts +19 -0
  40. package/dist/common/network-safety.js +72 -0
  41. package/dist/common/network-safety.js.map +1 -0
  42. package/dist/common/rate-limiter.service.d.ts +63 -0
  43. package/dist/common/rate-limiter.service.js +158 -0
  44. package/dist/common/rate-limiter.service.js.map +1 -0
  45. package/dist/common/refresh-token-cookie.d.ts +78 -0
  46. package/dist/common/refresh-token-cookie.js +185 -0
  47. package/dist/common/refresh-token-cookie.js.map +1 -0
  48. package/dist/common/request-context.d.ts +14 -0
  49. package/dist/common/request-context.js +22 -0
  50. package/dist/common/request-context.js.map +1 -0
  51. package/dist/common/strict-validation.pipe.d.ts +8 -0
  52. package/dist/common/strict-validation.pipe.js +16 -0
  53. package/dist/common/strict-validation.pipe.js.map +1 -0
  54. package/dist/common/time.d.ts +15 -0
  55. package/dist/common/time.js +34 -0
  56. package/dist/common/time.js.map +1 -0
  57. package/dist/constants.d.ts +1326 -0
  58. package/dist/constants.js +1142 -0
  59. package/dist/constants.js.map +1 -0
  60. package/dist/database/create-auth-tables.migration.d.ts +26 -0
  61. package/dist/database/create-auth-tables.migration.js +291 -0
  62. package/dist/database/create-auth-tables.migration.js.map +1 -0
  63. package/dist/database/email-change-and-retention.migration.d.ts +25 -0
  64. package/dist/database/email-change-and-retention.migration.js +54 -0
  65. package/dist/database/email-change-and-retention.migration.js.map +1 -0
  66. package/dist/database/entities/api-key.entity.d.ts +17 -0
  67. package/dist/database/entities/api-key.entity.js +80 -0
  68. package/dist/database/entities/api-key.entity.js.map +1 -0
  69. package/dist/database/entities/audit-log.entity.d.ts +13 -0
  70. package/dist/database/entities/audit-log.entity.js +64 -0
  71. package/dist/database/entities/audit-log.entity.js.map +1 -0
  72. package/dist/database/entities/backup-code.entity.d.ts +9 -0
  73. package/dist/database/entities/backup-code.entity.js +52 -0
  74. package/dist/database/entities/backup-code.entity.js.map +1 -0
  75. package/dist/database/entities/index.d.ts +43 -0
  76. package/dist/database/entities/index.js +82 -0
  77. package/dist/database/entities/index.js.map +1 -0
  78. package/dist/database/entities/json-object.d.ts +12 -0
  79. package/dist/database/entities/json-object.js +3 -0
  80. package/dist/database/entities/json-object.js.map +1 -0
  81. package/dist/database/entities/link-token.entity.d.ts +15 -0
  82. package/dist/database/entities/link-token.entity.js +64 -0
  83. package/dist/database/entities/link-token.entity.js.map +1 -0
  84. package/dist/database/entities/one-time-code.entity.d.ts +16 -0
  85. package/dist/database/entities/one-time-code.entity.js +69 -0
  86. package/dist/database/entities/one-time-code.entity.js.map +1 -0
  87. package/dist/database/entities/organization-invitation.entity.d.ts +14 -0
  88. package/dist/database/entities/organization-invitation.entity.js +73 -0
  89. package/dist/database/entities/organization-invitation.entity.js.map +1 -0
  90. package/dist/database/entities/organization-member.entity.d.ts +9 -0
  91. package/dist/database/entities/organization-member.entity.js +48 -0
  92. package/dist/database/entities/organization-member.entity.js.map +1 -0
  93. package/dist/database/entities/organization.entity.d.ts +12 -0
  94. package/dist/database/entities/organization.entity.js +58 -0
  95. package/dist/database/entities/organization.entity.js.map +1 -0
  96. package/dist/database/entities/passkey.entity.d.ts +19 -0
  97. package/dist/database/entities/passkey.entity.js +78 -0
  98. package/dist/database/entities/passkey.entity.js.map +1 -0
  99. package/dist/database/entities/password.entity.d.ts +6 -0
  100. package/dist/database/entities/password.entity.js +36 -0
  101. package/dist/database/entities/password.entity.js.map +1 -0
  102. package/dist/database/entities/refresh-token.entity.d.ts +9 -0
  103. package/dist/database/entities/refresh-token.entity.js +53 -0
  104. package/dist/database/entities/refresh-token.entity.js.map +1 -0
  105. package/dist/database/entities/session.entity.d.ts +20 -0
  106. package/dist/database/entities/session.entity.js +87 -0
  107. package/dist/database/entities/session.entity.js.map +1 -0
  108. package/dist/database/entities/settings.entity.d.ts +7 -0
  109. package/dist/database/entities/settings.entity.js +41 -0
  110. package/dist/database/entities/settings.entity.js.map +1 -0
  111. package/dist/database/entities/sign-in-challenge.entity.d.ts +23 -0
  112. package/dist/database/entities/sign-in-challenge.entity.js +80 -0
  113. package/dist/database/entities/sign-in-challenge.entity.js.map +1 -0
  114. package/dist/database/entities/signing-key.entity.d.ts +11 -0
  115. package/dist/database/entities/signing-key.entity.js +54 -0
  116. package/dist/database/entities/signing-key.entity.js.map +1 -0
  117. package/dist/database/entities/social-account.entity.d.ts +13 -0
  118. package/dist/database/entities/social-account.entity.js +54 -0
  119. package/dist/database/entities/social-account.entity.js.map +1 -0
  120. package/dist/database/entities/two-factor-secret.entity.d.ts +10 -0
  121. package/dist/database/entities/two-factor-secret.entity.js +48 -0
  122. package/dist/database/entities/two-factor-secret.entity.js.map +1 -0
  123. package/dist/database/entities/user.entity.d.ts +34 -0
  124. package/dist/database/entities/user.entity.js +129 -0
  125. package/dist/database/entities/user.entity.js.map +1 -0
  126. package/dist/database/entities/webhook-delivery.entity.d.ts +17 -0
  127. package/dist/database/entities/webhook-delivery.entity.js +79 -0
  128. package/dist/database/entities/webhook-delivery.entity.js.map +1 -0
  129. package/dist/database/entities/webhook.entity.d.ts +12 -0
  130. package/dist/database/entities/webhook.entity.js +58 -0
  131. package/dist/database/entities/webhook.entity.js.map +1 -0
  132. package/dist/database/typeorm-row.d.ts +21 -0
  133. package/dist/database/typeorm-row.js +29 -0
  134. package/dist/database/typeorm-row.js.map +1 -0
  135. package/dist/features/account/account.controller.d.ts +259 -0
  136. package/dist/features/account/account.controller.js +372 -0
  137. package/dist/features/account/account.controller.js.map +1 -0
  138. package/dist/features/account/account.dto.d.ts +16 -0
  139. package/dist/features/account/account.dto.js +61 -0
  140. package/dist/features/account/account.dto.js.map +1 -0
  141. package/dist/features/account/data-export.service.d.ts +85 -0
  142. package/dist/features/account/data-export.service.js +125 -0
  143. package/dist/features/account/data-export.service.js.map +1 -0
  144. package/dist/features/account/health.service.d.ts +30 -0
  145. package/dist/features/account/health.service.js +59 -0
  146. package/dist/features/account/health.service.js.map +1 -0
  147. package/dist/features/admin/admin.controller.d.ts +382 -0
  148. package/dist/features/admin/admin.controller.js +623 -0
  149. package/dist/features/admin/admin.controller.js.map +1 -0
  150. package/dist/features/admin/admin.dto.d.ts +61 -0
  151. package/dist/features/admin/admin.dto.js +253 -0
  152. package/dist/features/admin/admin.dto.js.map +1 -0
  153. package/dist/features/admin/admin.service.d.ts +142 -0
  154. package/dist/features/admin/admin.service.js +227 -0
  155. package/dist/features/admin/admin.service.js.map +1 -0
  156. package/dist/features/api-keys/api-keys.service.d.ts +67 -0
  157. package/dist/features/api-keys/api-keys.service.js +144 -0
  158. package/dist/features/api-keys/api-keys.service.js.map +1 -0
  159. package/dist/features/email-change/email-change.controller.d.ts +38 -0
  160. package/dist/features/email-change/email-change.controller.js +85 -0
  161. package/dist/features/email-change/email-change.controller.js.map +1 -0
  162. package/dist/features/email-change/email-change.dto.d.ts +10 -0
  163. package/dist/features/email-change/email-change.dto.js +43 -0
  164. package/dist/features/email-change/email-change.dto.js.map +1 -0
  165. package/dist/features/email-change/email-change.service.d.ts +74 -0
  166. package/dist/features/email-change/email-change.service.js +179 -0
  167. package/dist/features/email-change/email-change.service.js.map +1 -0
  168. package/dist/features/email-links/email-verification.service.d.ts +46 -0
  169. package/dist/features/email-links/email-verification.service.js +94 -0
  170. package/dist/features/email-links/email-verification.service.js.map +1 -0
  171. package/dist/features/email-links/link-tokens.service.d.ts +44 -0
  172. package/dist/features/email-links/link-tokens.service.js +122 -0
  173. package/dist/features/email-links/link-tokens.service.js.map +1 -0
  174. package/dist/features/events/auth-events.service.d.ts +52 -0
  175. package/dist/features/events/auth-events.service.js +117 -0
  176. package/dist/features/events/auth-events.service.js.map +1 -0
  177. package/dist/features/maintenance/data-retention.service.d.ts +60 -0
  178. package/dist/features/maintenance/data-retention.service.js +182 -0
  179. package/dist/features/maintenance/data-retention.service.js.map +1 -0
  180. package/dist/features/notifications/notifications.service.d.ts +40 -0
  181. package/dist/features/notifications/notifications.service.js +94 -0
  182. package/dist/features/notifications/notifications.service.js.map +1 -0
  183. package/dist/features/one-time-codes/one-time-codes.service.d.ts +40 -0
  184. package/dist/features/one-time-codes/one-time-codes.service.js +102 -0
  185. package/dist/features/one-time-codes/one-time-codes.service.js.map +1 -0
  186. package/dist/features/organizations/organizations.controller.d.ts +220 -0
  187. package/dist/features/organizations/organizations.controller.js +337 -0
  188. package/dist/features/organizations/organizations.controller.js.map +1 -0
  189. package/dist/features/organizations/organizations.dto.d.ts +31 -0
  190. package/dist/features/organizations/organizations.dto.js +100 -0
  191. package/dist/features/organizations/organizations.dto.js.map +1 -0
  192. package/dist/features/organizations/organizations.service.d.ts +274 -0
  193. package/dist/features/organizations/organizations.service.js +481 -0
  194. package/dist/features/organizations/organizations.service.js.map +1 -0
  195. package/dist/features/passkeys/passkeys.controller.d.ts +92 -0
  196. package/dist/features/passkeys/passkeys.controller.js +183 -0
  197. package/dist/features/passkeys/passkeys.controller.js.map +1 -0
  198. package/dist/features/passkeys/passkeys.dto.d.ts +13 -0
  199. package/dist/features/passkeys/passkeys.dto.js +50 -0
  200. package/dist/features/passkeys/passkeys.dto.js.map +1 -0
  201. package/dist/features/passkeys/passkeys.service.d.ts +159 -0
  202. package/dist/features/passkeys/passkeys.service.js +350 -0
  203. package/dist/features/passkeys/passkeys.service.js.map +1 -0
  204. package/dist/features/passwordless/passwordless-auth.service.d.ts +152 -0
  205. package/dist/features/passwordless/passwordless-auth.service.js +304 -0
  206. package/dist/features/passwordless/passwordless-auth.service.js.map +1 -0
  207. package/dist/features/passwordless/passwordless.controller.d.ts +75 -0
  208. package/dist/features/passwordless/passwordless.controller.js +163 -0
  209. package/dist/features/passwordless/passwordless.controller.js.map +1 -0
  210. package/dist/features/passwordless/passwordless.dto.d.ts +29 -0
  211. package/dist/features/passwordless/passwordless.dto.js +101 -0
  212. package/dist/features/passwordless/passwordless.dto.js.map +1 -0
  213. package/dist/features/passwords/password-auth.service.d.ts +154 -0
  214. package/dist/features/passwords/password-auth.service.js +317 -0
  215. package/dist/features/passwords/password-auth.service.js.map +1 -0
  216. package/dist/features/passwords/password-policy.service.d.ts +63 -0
  217. package/dist/features/passwords/password-policy.service.js +175 -0
  218. package/dist/features/passwords/password-policy.service.js.map +1 -0
  219. package/dist/features/passwords/password.controller.d.ts +95 -0
  220. package/dist/features/passwords/password.controller.js +193 -0
  221. package/dist/features/passwords/password.controller.js.map +1 -0
  222. package/dist/features/passwords/password.dto.d.ts +36 -0
  223. package/dist/features/passwords/password.dto.js +140 -0
  224. package/dist/features/passwords/password.dto.js.map +1 -0
  225. package/dist/features/sessions/session.service.d.ts +132 -0
  226. package/dist/features/sessions/session.service.js +308 -0
  227. package/dist/features/sessions/session.service.js.map +1 -0
  228. package/dist/features/sessions/session.types.d.ts +34 -0
  229. package/dist/features/sessions/session.types.js +3 -0
  230. package/dist/features/sessions/session.types.js.map +1 -0
  231. package/dist/features/settings/settings.defaults.d.ts +2 -0
  232. package/dist/features/settings/settings.defaults.js +7 -0
  233. package/dist/features/settings/settings.defaults.js.map +1 -0
  234. package/dist/features/settings/settings.service.d.ts +44 -0
  235. package/dist/features/settings/settings.service.js +112 -0
  236. package/dist/features/settings/settings.service.js.map +1 -0
  237. package/dist/features/settings/settings.types.d.ts +133 -0
  238. package/dist/features/settings/settings.types.js +3 -0
  239. package/dist/features/settings/settings.types.js.map +1 -0
  240. package/dist/features/settings/settings.validation.d.ts +33 -0
  241. package/dist/features/settings/settings.validation.js +614 -0
  242. package/dist/features/settings/settings.validation.js.map +1 -0
  243. package/dist/features/sign-in/sign-in-challenges.service.d.ts +54 -0
  244. package/dist/features/sign-in/sign-in-challenges.service.js +104 -0
  245. package/dist/features/sign-in/sign-in-challenges.service.js.map +1 -0
  246. package/dist/features/sign-in/sign-in.service.d.ts +127 -0
  247. package/dist/features/sign-in/sign-in.service.js +248 -0
  248. package/dist/features/sign-in/sign-in.service.js.map +1 -0
  249. package/dist/features/sign-in/sign-in.types.d.ts +22 -0
  250. package/dist/features/sign-in/sign-in.types.js +3 -0
  251. package/dist/features/sign-in/sign-in.types.js.map +1 -0
  252. package/dist/features/signing-keys/jwks.controller.d.ts +17 -0
  253. package/dist/features/signing-keys/jwks.controller.js +51 -0
  254. package/dist/features/signing-keys/jwks.controller.js.map +1 -0
  255. package/dist/features/signing-keys/signing-keys.service.d.ts +67 -0
  256. package/dist/features/signing-keys/signing-keys.service.js +142 -0
  257. package/dist/features/signing-keys/signing-keys.service.js.map +1 -0
  258. package/dist/features/social/social-auth.service.d.ts +100 -0
  259. package/dist/features/social/social-auth.service.js +232 -0
  260. package/dist/features/social/social-auth.service.js.map +1 -0
  261. package/dist/features/social/social-providers.service.d.ts +97 -0
  262. package/dist/features/social/social-providers.service.js +214 -0
  263. package/dist/features/social/social-providers.service.js.map +1 -0
  264. package/dist/features/social/social.controller.d.ts +54 -0
  265. package/dist/features/social/social.controller.js +124 -0
  266. package/dist/features/social/social.controller.js.map +1 -0
  267. package/dist/features/social/social.dto.d.ts +11 -0
  268. package/dist/features/social/social.dto.js +38 -0
  269. package/dist/features/social/social.dto.js.map +1 -0
  270. package/dist/features/two-factor/two-factor.controller.d.ts +84 -0
  271. package/dist/features/two-factor/two-factor.controller.js +174 -0
  272. package/dist/features/two-factor/two-factor.controller.js.map +1 -0
  273. package/dist/features/two-factor/two-factor.dto.d.ts +10 -0
  274. package/dist/features/two-factor/two-factor.dto.js +37 -0
  275. package/dist/features/two-factor/two-factor.dto.js.map +1 -0
  276. package/dist/features/two-factor/two-factor.service.d.ts +171 -0
  277. package/dist/features/two-factor/two-factor.service.js +348 -0
  278. package/dist/features/two-factor/two-factor.service.js.map +1 -0
  279. package/dist/features/users/sign-in-methods.service.d.ts +40 -0
  280. package/dist/features/users/sign-in-methods.service.js +81 -0
  281. package/dist/features/users/sign-in-methods.service.js.map +1 -0
  282. package/dist/features/users/sign-up-rules.d.ts +12 -0
  283. package/dist/features/users/sign-up-rules.js +29 -0
  284. package/dist/features/users/sign-up-rules.js.map +1 -0
  285. package/dist/features/users/users.service.d.ts +246 -0
  286. package/dist/features/users/users.service.js +509 -0
  287. package/dist/features/users/users.service.js.map +1 -0
  288. package/dist/features/users/users.types.d.ts +24 -0
  289. package/dist/features/users/users.types.js +3 -0
  290. package/dist/features/users/users.types.js.map +1 -0
  291. package/dist/features/webhooks/webhook-delivery.worker.d.ts +55 -0
  292. package/dist/features/webhooks/webhook-delivery.worker.js +175 -0
  293. package/dist/features/webhooks/webhook-delivery.worker.js.map +1 -0
  294. package/dist/features/webhooks/webhook-signature.d.ts +28 -0
  295. package/dist/features/webhooks/webhook-signature.js +85 -0
  296. package/dist/features/webhooks/webhook-signature.js.map +1 -0
  297. package/dist/features/webhooks/webhooks.service.d.ts +144 -0
  298. package/dist/features/webhooks/webhooks.service.js +238 -0
  299. package/dist/features/webhooks/webhooks.service.js.map +1 -0
  300. package/dist/guards/auth.decorators.d.ts +65 -0
  301. package/dist/guards/auth.decorators.js +68 -0
  302. package/dist/guards/auth.decorators.js.map +1 -0
  303. package/dist/guards/auth.guard.d.ts +47 -0
  304. package/dist/guards/auth.guard.js +132 -0
  305. package/dist/guards/auth.guard.js.map +1 -0
  306. package/dist/index.d.ts +38 -0
  307. package/dist/index.js +95 -0
  308. package/dist/index.js.map +1 -0
  309. package/dist/integrations/console-email-sender.d.ts +15 -0
  310. package/dist/integrations/console-email-sender.js +26 -0
  311. package/dist/integrations/console-email-sender.js.map +1 -0
  312. package/dist/integrations/msg91-sms-sender.d.ts +28 -0
  313. package/dist/integrations/msg91-sms-sender.js +46 -0
  314. package/dist/integrations/msg91-sms-sender.js.map +1 -0
  315. package/docs/README.md +39 -0
  316. package/docs/admin-api-keys-webhooks.md +147 -0
  317. package/docs/api-reference.md +175 -0
  318. package/docs/architecture.md +119 -0
  319. package/docs/client-integration.md +192 -0
  320. package/docs/code-walkthrough.md +273 -0
  321. package/docs/configuration.md +229 -0
  322. package/docs/endpoints/README.md +151 -0
  323. package/docs/endpoints/account.md +363 -0
  324. package/docs/endpoints/admin.md +422 -0
  325. package/docs/endpoints/email-password.md +261 -0
  326. package/docs/endpoints/organizations.md +192 -0
  327. package/docs/endpoints/passkeys.md +195 -0
  328. package/docs/endpoints/passwordless.md +214 -0
  329. package/docs/endpoints/social.md +116 -0
  330. package/docs/endpoints/two-factor.md +146 -0
  331. package/docs/error-codes.md +100 -0
  332. package/docs/events.md +83 -0
  333. package/docs/getting-started.md +139 -0
  334. package/docs/manual-testing.md +215 -0
  335. package/docs/organizations.md +86 -0
  336. package/docs/production-checklist.md +60 -0
  337. package/docs/protecting-routes.md +147 -0
  338. package/docs/security.md +61 -0
  339. package/docs/sign-in-flows.md +294 -0
  340. package/package.json +92 -4
@@ -0,0 +1,156 @@
1
+ import type { DataSource } from 'typeorm';
2
+ import type { AuthEventName, CookieSameSite, EMAIL_TYPE } from './constants';
3
+ import type { SettingsChanges } from './features/settings/settings.types';
4
+ import type { JsonObject } from './database/entities/json-object';
5
+ /**
6
+ * Everything AuthModule needs from your app.
7
+ *
8
+ * Secrets (API keys, app secrets) belong here, loaded from environment variables.
9
+ * Behaviour (which sign-in methods are on, lifetimes, roles) belongs in `settings`,
10
+ * which admins can also change at runtime.
11
+ */
12
+ export interface AuthOptions {
13
+ /** Your app's TypeORM DataSource. Add AUTH_ENTITIES and AUTH_MIGRATIONS to it. */
14
+ dataSource: DataSource;
15
+ /** Your API's public URL, e.g. https://api.myapp.com. Every token carries it as "iss" and "aud". */
16
+ issuer: string;
17
+ /** Protects the private signing key and other secrets stored in the database. */
18
+ encryption: EncryptionOptions;
19
+ /** Sends the emails the library needs (you write the email; we give you the token or code). */
20
+ emailSender: EmailSender;
21
+ /** Sends SMS codes. Required only when phone sign-in is turned on. */
22
+ smsSender?: SmsSender;
23
+ /** Facebook app secret, needed to verify Facebook sign-ins. Google and Apple need no secret. */
24
+ facebookAppSecret?: string;
25
+ /** Passkeys: your website's domain and origins. Required only when passkeys are turned on. */
26
+ passkeys?: PasskeyOptions;
27
+ /** Name shown in authenticator apps next to the 2FA code, e.g. "MyApp". */
28
+ appName?: string;
29
+ /** Your defaults for behaviour. Admins can change them later with PATCH /auth/admin/settings. */
30
+ settings?: SettingsChanges;
31
+ /**
32
+ * Redis connection URL, so every server shares the same rate-limit counters. Required in
33
+ * production unless `singleServer` is true: without it, each server counts on its own and an
34
+ * attacker gets one full set of attempts per server.
35
+ */
36
+ redisUrl?: string;
37
+ /**
38
+ * Set to true only when production runs exactly one server process, to run without Redis
39
+ * (counters then live in that process's memory and reset on restart).
40
+ */
41
+ singleServer?: boolean;
42
+ /**
43
+ * For web apps: put the refresh token in an HttpOnly cookie instead of the JSON response, so
44
+ * scripts on the page (including injected ones) can never read it. Applies only to requests
45
+ * from `allowedOrigins`; mobile apps and servers on the same API still get it in the body.
46
+ */
47
+ refreshTokenCookie?: RefreshTokenCookieOptions;
48
+ /** Called for every auth event (also sent to webhooks). Handy to create a profile row on "user.created". */
49
+ onEvent?: (event: AuthEvent) => void | Promise<void>;
50
+ }
51
+ export interface RefreshTokenCookieOptions {
52
+ /**
53
+ * Exact origins of your web app, e.g. ["https://app.myapp.com"]. A refresh that uses the cookie
54
+ * must come from one of these (checked with the Origin header), which blocks cross-site requests.
55
+ */
56
+ allowedOrigins: string[];
57
+ /** Default "__Host-refresh_token" (the browser then enforces Secure, path "/" and no domain). */
58
+ name?: string;
59
+ /** Share the cookie with subdomains, e.g. "myapp.com". Not allowed with a "__Host-" name. */
60
+ domain?: string;
61
+ /** Default "/". */
62
+ path?: string;
63
+ /** Default "Strict". Use "Lax" if your app and API are on different sites; "None" only if you must. */
64
+ sameSite?: CookieSameSite;
65
+ }
66
+ export type EncryptionOptions =
67
+ /**
68
+ * Production: an AWS KMS symmetric key. The server needs kms:Encrypt and kms:Decrypt on it.
69
+ * `legacyLocalKey`: set it to your old development localKey while moving an existing database to
70
+ * KMS. Values encrypted earlier still decrypt; everything new is encrypted with KMS.
71
+ */
72
+ {
73
+ awsKmsKeyId: string;
74
+ awsRegion: string;
75
+ legacyLocalKey?: string;
76
+ }
77
+ /** Development only: 32 random bytes in base64. Refused when NODE_ENV=production. */
78
+ | {
79
+ localKey: string;
80
+ };
81
+ /** One message the library asks you to send. Use `type` to pick your template. */
82
+ export type AuthEmail = {
83
+ type: typeof EMAIL_TYPE.verifyEmail;
84
+ to: string;
85
+ token: string;
86
+ } | {
87
+ type: typeof EMAIL_TYPE.resetPassword;
88
+ to: string;
89
+ token: string;
90
+ } | {
91
+ type: typeof EMAIL_TYPE.magicSignIn;
92
+ to: string;
93
+ token: string;
94
+ } | {
95
+ type: typeof EMAIL_TYPE.signInCode;
96
+ to: string;
97
+ code: string;
98
+ } | {
99
+ type: typeof EMAIL_TYPE.organizationInvitation;
100
+ to: string;
101
+ token: string;
102
+ organizationName: string;
103
+ invitedByName: string | null;
104
+ } | {
105
+ type: typeof EMAIL_TYPE.passwordChanged;
106
+ to: string;
107
+ } | {
108
+ type: typeof EMAIL_TYPE.newSignIn;
109
+ to: string;
110
+ ipAddress: string | null;
111
+ userAgent: string | null;
112
+ }
113
+ /** Sent to the NEW address; the link confirms the change (POST /auth/email/change/confirm). */
114
+ | {
115
+ type: typeof EMAIL_TYPE.confirmEmailChange;
116
+ to: string;
117
+ token: string;
118
+ }
119
+ /** Sent to the OLD address once the change is done. `newEmail` is masked, e.g. "r***@mail.com". */
120
+ | {
121
+ type: typeof EMAIL_TYPE.emailChanged;
122
+ to: string;
123
+ newEmail: string;
124
+ };
125
+ export interface EmailSender {
126
+ send(email: AuthEmail): Promise<void>;
127
+ }
128
+ export interface SmsSender {
129
+ /** Send `code` to `phoneNumber` (E.164, e.g. +919876543210). */
130
+ sendCode(phoneNumber: string, code: string): Promise<void>;
131
+ }
132
+ export interface PasskeyOptions {
133
+ /** Your domain without protocol, e.g. "myapp.com". Passkeys only work on this domain and its subdomains. */
134
+ relyingPartyId: string;
135
+ /** Name shown in the passkey prompt, e.g. "MyApp". */
136
+ relyingPartyName: string;
137
+ /** Exact origins allowed, e.g. ["https://myapp.com", "android:apk-key-hash:..."]. */
138
+ allowedOrigins: string[];
139
+ }
140
+ /** What onEvent and webhooks receive. */
141
+ export interface AuthEvent {
142
+ name: AuthEventName;
143
+ /** The user the event is about, when there is one. */
144
+ userId: string | null;
145
+ /** Event details. Never contains passwords, tokens or codes. */
146
+ data: JsonObject;
147
+ occurredAt: string;
148
+ }
149
+ /** Shape of AuthModule.forRoot({ ... }). */
150
+ export interface AuthModuleConfig {
151
+ imports?: any[];
152
+ inject?: any[];
153
+ useFactory: (...dependencies: any[]) => AuthOptions | Promise<AuthOptions>;
154
+ /** Protect every route in the app by default (open routes with @Public()). Default: true. */
155
+ protectAllRoutes?: boolean;
156
+ }
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=auth-options.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"auth-options.js","sourceRoot":"","sources":["../src/auth-options.ts"],"names":[],"mappings":""}
@@ -0,0 +1,9 @@
1
+ import { AuthOptions } from './auth-options';
2
+ /**
3
+ * Checks AuthOptions when the app starts, so a mistake stops the app with a clear message
4
+ * instead of failing later on a user's request.
5
+ *
6
+ * @param options - What your `useFactory` returned.
7
+ * @throws Error listing every problem found (missing data source, bad encryption key, etc.).
8
+ */
9
+ export declare function assertValidAuthOptions(options: AuthOptions): void;
@@ -0,0 +1,74 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.assertValidAuthOptions = assertValidAuthOptions;
4
+ const constants_1 = require("./constants");
5
+ /**
6
+ * Checks AuthOptions when the app starts, so a mistake stops the app with a clear message
7
+ * instead of failing later on a user's request.
8
+ *
9
+ * @param options - What your `useFactory` returned.
10
+ * @throws Error listing every problem found (missing data source, bad encryption key, etc.).
11
+ */
12
+ function assertValidAuthOptions(options) {
13
+ const problems = [];
14
+ const isProduction = process.env.NODE_ENV === constants_1.PRODUCTION_ENVIRONMENT;
15
+ if (!options || typeof options !== 'object')
16
+ throw new Error('AuthModule: useFactory must return the AuthOptions object');
17
+ if (!options.dataSource)
18
+ problems.push(constants_1.OPTIONS_PROBLEM.dataSourceRequired);
19
+ if (!options.emailSender || typeof options.emailSender.send !== 'function')
20
+ problems.push(constants_1.OPTIONS_PROBLEM.emailSenderRequired);
21
+ try {
22
+ const issuerUrl = new URL(options.issuer);
23
+ if (isProduction && issuerUrl.protocol !== constants_1.URL_PROTOCOL.https)
24
+ problems.push(constants_1.OPTIONS_PROBLEM.issuerMustBeHttps);
25
+ }
26
+ catch {
27
+ problems.push(constants_1.OPTIONS_PROBLEM.issuerMustBeUrl);
28
+ }
29
+ const encryption = options.encryption;
30
+ if (!encryption) {
31
+ problems.push(constants_1.OPTIONS_PROBLEM.encryptionRequired);
32
+ }
33
+ else if ('localKey' in encryption) {
34
+ if (isProduction)
35
+ problems.push(constants_1.OPTIONS_PROBLEM.localKeyNotForProduction);
36
+ const keyBytes = typeof encryption.localKey === 'string' ? Buffer.from(encryption.localKey, constants_1.TEXT_ENCODING.base64) : Buffer.alloc(0);
37
+ if (keyBytes.length !== constants_1.LOCAL_ENCRYPTION_KEY_BYTES) {
38
+ problems.push(constants_1.OPTIONS_PROBLEM.localKeyWrongSize);
39
+ }
40
+ }
41
+ else {
42
+ if (!encryption.awsKmsKeyId || !encryption.awsRegion)
43
+ problems.push(constants_1.OPTIONS_PROBLEM.kmsDetailsRequired);
44
+ const legacyKey = encryption.legacyLocalKey;
45
+ if (legacyKey !== undefined && (typeof legacyKey !== 'string' || Buffer.from(legacyKey, constants_1.TEXT_ENCODING.base64).length !== constants_1.LOCAL_ENCRYPTION_KEY_BYTES)) {
46
+ problems.push(constants_1.OPTIONS_PROBLEM.legacyLocalKeyWrongSize);
47
+ }
48
+ }
49
+ if (isProduction && !options.redisUrl && options.singleServer !== true)
50
+ problems.push(constants_1.OPTIONS_PROBLEM.redisRequiredInProduction);
51
+ if (options.passkeys) {
52
+ const { relyingPartyId, relyingPartyName, allowedOrigins } = options.passkeys;
53
+ if (!relyingPartyId || constants_1.RELYING_PARTY_ID_FORBIDDEN_CHARACTERS.some((character) => relyingPartyId.includes(character))) {
54
+ problems.push(constants_1.OPTIONS_PROBLEM.relyingPartyIdMustBeDomain);
55
+ }
56
+ if (!relyingPartyName)
57
+ problems.push(constants_1.OPTIONS_PROBLEM.relyingPartyNameRequired);
58
+ if (!Array.isArray(allowedOrigins) || allowedOrigins.length === 0)
59
+ problems.push(constants_1.OPTIONS_PROBLEM.allowedOriginsRequired);
60
+ }
61
+ if (options.refreshTokenCookie) {
62
+ const cookie = options.refreshTokenCookie;
63
+ if (!Array.isArray(cookie.allowedOrigins) || cookie.allowedOrigins.length === 0)
64
+ problems.push(constants_1.OPTIONS_PROBLEM.cookieAllowedOriginsRequired);
65
+ const name = cookie.name ?? constants_1.REFRESH_TOKEN_COOKIE_DEFAULTS.name;
66
+ const path = cookie.path ?? constants_1.REFRESH_TOKEN_COOKIE_DEFAULTS.path;
67
+ if (name.startsWith(constants_1.HOST_COOKIE_PREFIX) && (cookie.domain !== undefined || path !== constants_1.REFRESH_TOKEN_COOKIE_DEFAULTS.path)) {
68
+ problems.push(constants_1.OPTIONS_PROBLEM.hostCookieRules);
69
+ }
70
+ }
71
+ if (problems.length > 0)
72
+ throw new Error(`AuthModule options are invalid:\n - ${problems.join('\n - ')}`);
73
+ }
74
+ //# sourceMappingURL=auth-options.validation.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"auth-options.validation.js","sourceRoot":"","sources":["../src/auth-options.validation.ts"],"names":[],"mappings":";;AAmBA,wDAsDC;AAxED,2CASqB;AAErB;;;;;;GAMG;AACH,SAAgB,sBAAsB,CAAC,OAAoB;IACzD,MAAM,QAAQ,GAAa,EAAE,CAAC;IAC9B,MAAM,YAAY,GAAG,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,kCAAsB,CAAC;IAErE,IAAI,CAAC,OAAO,IAAI,OAAO,OAAO,KAAK,QAAQ;QAAE,MAAM,IAAI,KAAK,CAAC,2DAA2D,CAAC,CAAC;IAC1H,IAAI,CAAC,OAAO,CAAC,UAAU;QAAE,QAAQ,CAAC,IAAI,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G,OAAO,CAAC,kBAAkB,CAAC;QAC1C,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,cAAc,CAAC,IAAI,MAAM,CAAC,cAAc,CAAC,MAAM,KAAK,CAAC;YAAE,QAAQ,CAAC,IAAI,CAAC,2BAAe,CAAC,4BAA4B,CAAC,CAAC;QAC7I,MAAM,IAAI,GAAG,MAAM,CAAC,IAAI,IAAI,yCAA6B,CAAC,IAAI,CAAC;QAC/D,MAAM,IAAI,GAAG,MAAM,CAAC,IAAI,IAAI,yCAA6B,CAAC,IAAI,CAAC;QAC/D,IAAI,IAAI,CAAC,UAAU,CAAC,8BAAkB,CAAC,IAAI,CAAC,MAAM,CAAC,MAAM,KAAK,SAAS,IAAI,IAAI,KAAK,yCAA6B,CAAC,IAAI,CAAC,EAAE,CAAC;YACxH,QAAQ,CAAC,IAAI,CAAC,2BAAe,CAAC,eAAe,CAAC,CAAC;QACjD,CAAC;IACH,CAAC;IAED,IAAI,QAAQ,CAAC,MAAM,GAAG,CAAC;QAAE,MAAM,IAAI,KAAK,CAAC,wCAAwC,QAAQ,CAAC,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC;AAC9G,CAAC"}
@@ -0,0 +1,38 @@
1
+ import { DynamicModule } from '@nestjs/common';
2
+ import { AuthModuleConfig } from './auth-options';
3
+ /**
4
+ * Add authentication to a NestJS app:
5
+ *
6
+ * @Module({
7
+ * imports: [
8
+ * AuthModule.forRoot({
9
+ * inject: [DataSource, ConfigService],
10
+ * useFactory: (dataSource: DataSource, config: ConfigService) => ({
11
+ * dataSource,
12
+ * issuer: 'https://api.myapp.com',
13
+ * encryption: { awsKmsKeyId: config.getOrThrow('KMS_KEY_ID'), awsRegion: 'ap-south-1' },
14
+ * emailSender: myEmailSender,
15
+ * settings: { emailOtp: { enabled: true } },
16
+ * }),
17
+ * }),
18
+ * ],
19
+ * })
20
+ *
21
+ * What you get:
22
+ * - every /auth/* endpoint (see each controller for the list),
23
+ * - every route in your app protected by default (open one with @Public()),
24
+ * - errors in the { success: false, code, message } format,
25
+ * - the background worker that delivers webhooks.
26
+ */
27
+ export declare class AuthModule {
28
+ /**
29
+ * Registers the library in your app.
30
+ *
31
+ * @param config - `useFactory` returns the AuthOptions (data source, issuer, encryption, senders,
32
+ * settings); `inject` and `imports` work as in any Nest async module. Set
33
+ * `protectAllRoutes: false` to opt routes in with @UseGuards(AuthGuard) instead.
34
+ * @returns The configured, global module.
35
+ * @throws At start-up, when the options are invalid (every problem is listed).
36
+ */
37
+ static forRoot(config: AuthModuleConfig): DynamicModule;
38
+ }
@@ -0,0 +1,173 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var AuthModule_1;
9
+ Object.defineProperty(exports, "__esModule", { value: true });
10
+ exports.AuthModule = void 0;
11
+ const common_1 = require("@nestjs/common");
12
+ const core_1 = require("@nestjs/core");
13
+ const auth_options_validation_1 = require("./auth-options.validation");
14
+ const auth_exception_filter_1 = require("./common/auth-exception.filter");
15
+ const encryption_service_1 = require("./common/encryption.service");
16
+ const rate_limiter_service_1 = require("./common/rate-limiter.service");
17
+ const refresh_token_cookie_1 = require("./common/refresh-token-cookie");
18
+ const data_export_service_1 = require("./features/account/data-export.service");
19
+ const health_service_1 = require("./features/account/health.service");
20
+ const email_change_controller_1 = require("./features/email-change/email-change.controller");
21
+ const email_change_service_1 = require("./features/email-change/email-change.service");
22
+ const constants_1 = require("./constants");
23
+ const account_controller_1 = require("./features/account/account.controller");
24
+ const admin_controller_1 = require("./features/admin/admin.controller");
25
+ const admin_service_1 = require("./features/admin/admin.service");
26
+ const api_keys_service_1 = require("./features/api-keys/api-keys.service");
27
+ const email_verification_service_1 = require("./features/email-links/email-verification.service");
28
+ const link_tokens_service_1 = require("./features/email-links/link-tokens.service");
29
+ const data_retention_service_1 = require("./features/maintenance/data-retention.service");
30
+ const auth_events_service_1 = require("./features/events/auth-events.service");
31
+ const notifications_service_1 = require("./features/notifications/notifications.service");
32
+ const one_time_codes_service_1 = require("./features/one-time-codes/one-time-codes.service");
33
+ const organizations_controller_1 = require("./features/organizations/organizations.controller");
34
+ const organizations_service_1 = require("./features/organizations/organizations.service");
35
+ const passkeys_controller_1 = require("./features/passkeys/passkeys.controller");
36
+ const passkeys_service_1 = require("./features/passkeys/passkeys.service");
37
+ const passwordless_controller_1 = require("./features/passwordless/passwordless.controller");
38
+ const passwordless_auth_service_1 = require("./features/passwordless/passwordless-auth.service");
39
+ const password_auth_service_1 = require("./features/passwords/password-auth.service");
40
+ const password_controller_1 = require("./features/passwords/password.controller");
41
+ const password_policy_service_1 = require("./features/passwords/password-policy.service");
42
+ const session_service_1 = require("./features/sessions/session.service");
43
+ const settings_service_1 = require("./features/settings/settings.service");
44
+ const sign_in_challenges_service_1 = require("./features/sign-in/sign-in-challenges.service");
45
+ const sign_in_service_1 = require("./features/sign-in/sign-in.service");
46
+ const jwks_controller_1 = require("./features/signing-keys/jwks.controller");
47
+ const signing_keys_service_1 = require("./features/signing-keys/signing-keys.service");
48
+ const social_auth_service_1 = require("./features/social/social-auth.service");
49
+ const social_controller_1 = require("./features/social/social.controller");
50
+ const social_providers_service_1 = require("./features/social/social-providers.service");
51
+ const two_factor_controller_1 = require("./features/two-factor/two-factor.controller");
52
+ const two_factor_service_1 = require("./features/two-factor/two-factor.service");
53
+ const sign_in_methods_service_1 = require("./features/users/sign-in-methods.service");
54
+ const users_service_1 = require("./features/users/users.service");
55
+ const webhook_delivery_worker_1 = require("./features/webhooks/webhook-delivery.worker");
56
+ const webhooks_service_1 = require("./features/webhooks/webhooks.service");
57
+ const auth_guard_1 = require("./guards/auth.guard");
58
+ /** Services your app may inject (e.g. UsersService in a profile module, OrganizationsService in a billing module). */
59
+ const PUBLIC_SERVICES = [
60
+ settings_service_1.SettingsService,
61
+ users_service_1.UsersService,
62
+ session_service_1.SessionService,
63
+ sign_in_service_1.SignInService,
64
+ auth_events_service_1.AuthEventsService,
65
+ api_keys_service_1.ApiKeysService,
66
+ organizations_service_1.OrganizationsService,
67
+ two_factor_service_1.TwoFactorService,
68
+ signing_keys_service_1.SigningKeysService,
69
+ webhooks_service_1.WebhooksService,
70
+ data_retention_service_1.DataRetentionService,
71
+ auth_guard_1.AuthGuard,
72
+ ];
73
+ const INTERNAL_SERVICES = [
74
+ encryption_service_1.EncryptionService,
75
+ rate_limiter_service_1.RateLimiterService,
76
+ notifications_service_1.NotificationsService,
77
+ link_tokens_service_1.LinkTokensService,
78
+ one_time_codes_service_1.OneTimeCodesService,
79
+ sign_in_challenges_service_1.SignInChallengesService,
80
+ sign_in_methods_service_1.SignInMethodsService,
81
+ password_policy_service_1.PasswordPolicyService,
82
+ password_auth_service_1.PasswordAuthService,
83
+ email_verification_service_1.EmailVerificationService,
84
+ passwordless_auth_service_1.PasswordlessAuthService,
85
+ social_providers_service_1.SocialProvidersService,
86
+ social_auth_service_1.SocialAuthService,
87
+ passkeys_service_1.PasskeysService,
88
+ admin_service_1.AdminService,
89
+ webhook_delivery_worker_1.WebhookDeliveryWorker,
90
+ email_change_service_1.EmailChangeService,
91
+ data_export_service_1.DataExportService,
92
+ health_service_1.HealthService,
93
+ ];
94
+ const CONTROLLERS = [
95
+ password_controller_1.PasswordController,
96
+ passwordless_controller_1.PasswordlessController,
97
+ account_controller_1.AccountController,
98
+ email_change_controller_1.EmailChangeController,
99
+ social_controller_1.SocialController,
100
+ passkeys_controller_1.PasskeysController,
101
+ two_factor_controller_1.TwoFactorController,
102
+ organizations_controller_1.OrganizationsController,
103
+ admin_controller_1.AdminController,
104
+ jwks_controller_1.JwksController,
105
+ ];
106
+ /**
107
+ * Add authentication to a NestJS app:
108
+ *
109
+ * @Module({
110
+ * imports: [
111
+ * AuthModule.forRoot({
112
+ * inject: [DataSource, ConfigService],
113
+ * useFactory: (dataSource: DataSource, config: ConfigService) => ({
114
+ * dataSource,
115
+ * issuer: 'https://api.myapp.com',
116
+ * encryption: { awsKmsKeyId: config.getOrThrow('KMS_KEY_ID'), awsRegion: 'ap-south-1' },
117
+ * emailSender: myEmailSender,
118
+ * settings: { emailOtp: { enabled: true } },
119
+ * }),
120
+ * }),
121
+ * ],
122
+ * })
123
+ *
124
+ * What you get:
125
+ * - every /auth/* endpoint (see each controller for the list),
126
+ * - every route in your app protected by default (open one with @Public()),
127
+ * - errors in the { success: false, code, message } format,
128
+ * - the background worker that delivers webhooks.
129
+ */
130
+ let AuthModule = AuthModule_1 = class AuthModule {
131
+ /**
132
+ * Registers the library in your app.
133
+ *
134
+ * @param config - `useFactory` returns the AuthOptions (data source, issuer, encryption, senders,
135
+ * settings); `inject` and `imports` work as in any Nest async module. Set
136
+ * `protectAllRoutes: false` to opt routes in with @UseGuards(AuthGuard) instead.
137
+ * @returns The configured, global module.
138
+ * @throws At start-up, when the options are invalid (every problem is listed).
139
+ */
140
+ static forRoot(config) {
141
+ const optionsProvider = {
142
+ provide: constants_1.AUTH_OPTIONS_TOKEN,
143
+ inject: config.inject ?? [],
144
+ useFactory: async (...dependencies) => {
145
+ const options = await config.useFactory(...dependencies);
146
+ (0, auth_options_validation_1.assertValidAuthOptions)(options);
147
+ return options;
148
+ },
149
+ };
150
+ const appWideProviders = [
151
+ { provide: core_1.APP_FILTER, useClass: auth_exception_filter_1.AuthExceptionFilter },
152
+ // Inert unless refreshTokenCookie is set, and only ever acts on the library's own routes.
153
+ { provide: core_1.APP_INTERCEPTOR, useClass: refresh_token_cookie_1.RefreshTokenCookieInterceptor },
154
+ ];
155
+ // Protect every route by default; apps that prefer opt-in use @UseGuards(AuthGuard) themselves.
156
+ if (config.protectAllRoutes !== false)
157
+ appWideProviders.push({ provide: core_1.APP_GUARD, useExisting: auth_guard_1.AuthGuard });
158
+ return {
159
+ module: AuthModule_1,
160
+ global: true,
161
+ imports: config.imports ?? [],
162
+ controllers: CONTROLLERS,
163
+ providers: [optionsProvider, ...PUBLIC_SERVICES, ...INTERNAL_SERVICES, ...appWideProviders],
164
+ exports: [constants_1.AUTH_OPTIONS_TOKEN, ...PUBLIC_SERVICES],
165
+ };
166
+ }
167
+ };
168
+ exports.AuthModule = AuthModule;
169
+ exports.AuthModule = AuthModule = AuthModule_1 = __decorate([
170
+ (0, common_1.Global)(),
171
+ (0, common_1.Module)({})
172
+ ], AuthModule);
173
+ //# sourceMappingURL=auth.module.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"auth.module.js","sourceRoot":"","sources":["../src/auth.module.ts"],"names":[],"mappings":";;;;;;;;;;AAAA,2CAAyE;AACzE,uCAAsE;AAEtE,uEAAmE;AACnE,0EAAqE;AACrE,oEAAgE;AAChE,wEAAmE;AACnE,wEAA8E;AAC9E,gFAA2E;AAC3E,sEAAkE;AAClE,6FAAwF;AACxF,uFAAkF;AAClF,2CAAiD;AACjD,8EAA0E;AAC1E,wEAAoE;AACpE,kEAA8D;AAC9D,2EAAsE;AACtE,kGAA6F;AAC7F,oFAA+E;AAC/E,0FAAqF;AACrF,+EAA0E;AAC1E,0FAAsF;AACtF,6FAAuF;AACvF,gGAA4F;AAC5F,0FAAsF;AACtF,iFAA6E;AAC7E,2EAAuE;AACvE,6FAAyF;AACzF,iGAA4F;AAC5F,sFAAiF;AACjF,kFAA8E;AAC9E,0FAAqF;AACrF,yEAAqE;AACrE,2EAAuE;AACvE,8FAAwF;AACxF,wEAAmE;AACnE,6EAAyE;AACzE,uFAAkF;AAClF,+EAA0E;AAC1E,2EAAuE;AACvE,yFAAoF;AACpF,uFAAkF;AAClF,iFAA4E;AAC5E,sFAAgF;AAChF,kEAA8D;AAC9D,yFAAo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,EAAE,iBAAU,EAAE,QAAQ,EAAE,2CAAmB,EAAE;YACtD,0FAA0F;YAC1F,EAAE,OAAO,EAAE,sBAAe,EAAE,QAAQ,EAAE,oDAA6B,EAAE;SACtE,CAAC;QACF,gGAAgG;QAChG,IAAI,MAAM,CAAC,gBAAgB,KAAK,KAAK;YAAE,gBAAgB,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,gBAAS,EAAE,WAAW,EAAE,sBAAS,EAAE,CAAC,CAAC;QAE7G,OAAO;YACL,MAAM,EAAE,YAAU;YAClB,MAAM,EAAE,IAAI;YACZ,OAAO,EAAE,MAAM,CAAC,OAAO,IAAI,EAAE;YAC7B,WAAW,EAAE,WAAW;YACxB,SAAS,EAAE,CAAC,eAAe,EAAE,GAAG,eAAe,EAAE,GAAG,iBAAiB,EAAE,GAAG,gBAAgB,CAAC;YAC3F,OAAO,EAAE,CAAC,8BAAkB,EAAE,GAAG,eAAe,CAAC;SAClD,CAAC;IACJ,CAAC;CACF,CAAA;AAtCY,gCAAU;qBAAV,UAAU;IAFtB,IAAA,eAAM,GAAE;IACR,IAAA,eAAM,EAAC,EAAE,CAAC;GACE,UAAU,CAsCtB"}
@@ -0,0 +1,16 @@
1
+ import { HttpException } from '@nestjs/common';
2
+ import { AuthErrorDefinition } from '../constants.js';
3
+ /**
4
+ * The one error type the library throws for expected problems (wrong password, expired code...).
5
+ * Every error is defined once, in AUTH_ERRORS (constants.ts), with its HTTP status, code and message.
6
+ *
7
+ * Sent to the client as:
8
+ * { "success": false, "code": "INVALID_CREDENTIALS", "message": "Invalid email or password." }
9
+ *
10
+ * Usage: throw new AuthError(AUTH_ERRORS.invalidCredentials)
11
+ * throw new AuthError(AUTH_ERRORS.tooManyRequests, { retryAfterSeconds: 60 })
12
+ */
13
+ export declare class AuthError extends HttpException {
14
+ readonly code: string;
15
+ constructor(error: AuthErrorDefinition, extraDetails?: Record<string, unknown>);
16
+ }
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AuthError = void 0;
4
+ const common_1 = require("@nestjs/common");
5
+ /**
6
+ * The one error type the library throws for expected problems (wrong password, expired code...).
7
+ * Every error is defined once, in AUTH_ERRORS (constants.ts), with its HTTP status, code and message.
8
+ *
9
+ * Sent to the client as:
10
+ * { "success": false, "code": "INVALID_CREDENTIALS", "message": "Invalid email or password." }
11
+ *
12
+ * Usage: throw new AuthError(AUTH_ERRORS.invalidCredentials)
13
+ * throw new AuthError(AUTH_ERRORS.tooManyRequests, { retryAfterSeconds: 60 })
14
+ */
15
+ class AuthError extends common_1.HttpException {
16
+ code;
17
+ constructor(error, extraDetails = {}) {
18
+ super({ statusCode: error.status, code: error.code, message: error.message, ...extraDetails }, error.status);
19
+ this.code = error.code;
20
+ }
21
+ }
22
+ exports.AuthError = AuthError;
23
+ //# sourceMappingURL=auth-error.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"auth-error.js","sourceRoot":"","sources":["../../src/common/auth-error.ts"],"names":[],"mappings":";;;AAAA,2CAA+C;AAG/C;;;;;;;;;GASG;AACH,MAAa,SAAU,SAAQ,sBAAa;IACjC,IAAI,CAAS;IAEtB,YAAY,KAA0B,EAAE,eAAwC,EAAE;QAChF,KAAK,CAAC,EAAE,UAAU,EAAE,KAAK,CAAC,MAAM,EAAE,IAAI,EAAE,KAAK,CAAC,IAAI,EAAE,OAAO,EAAE,KAAK,CAAC,OAAO,EAAE,GAAG,YAAY,EAAE,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC;QAC7G,IAAI,CAAC,IAAI,GAAG,KAAK,CAAC,IAAI,CAAC;IACzB,CAAC;CACF;AAPD,8BAOC"}
@@ -0,0 +1,24 @@
1
+ import { ArgumentsHost, ExceptionFilter } from '@nestjs/common';
2
+ /**
3
+ * Turns every error into one response format:
4
+ * { "success": false, "code": "INVALID_CREDENTIALS", "message": "Invalid email or password." }
5
+ *
6
+ * - AuthError: sent as is (plus extra fields such as retryAfterSeconds).
7
+ * - Validation errors from DTOs: code VALIDATION_ERROR with the list of problems.
8
+ * - Other Nest HTTP errors (404 route not found...): their status with a generic code.
9
+ * - Anything unexpected: logged on the server, and only "INTERNAL_SERVER_ERROR" is sent back,
10
+ * so stack traces and internal details never reach the client.
11
+ *
12
+ * AuthModule registers it app-wide (APP_FILTER), so your own routes get the same error format.
13
+ * Throw AuthError or any Nest HttpException from your code and it is formatted the same way.
14
+ */
15
+ export declare class AuthExceptionFilter implements ExceptionFilter {
16
+ private readonly logger;
17
+ /**
18
+ * Formats any thrown error into the library's response shape and sends it.
19
+ *
20
+ * @param exception - Whatever was thrown.
21
+ * @param host - Gives access to the HTTP response.
22
+ */
23
+ catch(exception: unknown, host: ArgumentsHost): any;
24
+ }
@@ -0,0 +1,66 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.AuthExceptionFilter = void 0;
10
+ const common_1 = require("@nestjs/common");
11
+ const constants_1 = require("../constants.js");
12
+ /**
13
+ * Turns every error into one response format:
14
+ * { "success": false, "code": "INVALID_CREDENTIALS", "message": "Invalid email or password." }
15
+ *
16
+ * - AuthError: sent as is (plus extra fields such as retryAfterSeconds).
17
+ * - Validation errors from DTOs: code VALIDATION_ERROR with the list of problems.
18
+ * - Other Nest HTTP errors (404 route not found...): their status with a generic code.
19
+ * - Anything unexpected: logged on the server, and only "INTERNAL_SERVER_ERROR" is sent back,
20
+ * so stack traces and internal details never reach the client.
21
+ *
22
+ * AuthModule registers it app-wide (APP_FILTER), so your own routes get the same error format.
23
+ * Throw AuthError or any Nest HttpException from your code and it is formatted the same way.
24
+ */
25
+ let AuthExceptionFilter = class AuthExceptionFilter {
26
+ logger = new common_1.Logger(constants_1.LOG_CONTEXT.exceptionFilter);
27
+ /**
28
+ * Formats any thrown error into the library's response shape and sends it.
29
+ *
30
+ * @param exception - Whatever was thrown.
31
+ * @param host - Gives access to the HTTP response.
32
+ */
33
+ catch(exception, host) {
34
+ const response = host.switchToHttp().getResponse();
35
+ if (exception instanceof common_1.HttpException) {
36
+ const status = exception.getStatus();
37
+ const body = exception.getResponse();
38
+ if (typeof body === 'object' && typeof body.code === 'string') {
39
+ const { statusCode: _statusCode, ...rest } = body;
40
+ if (typeof rest.retryAfterSeconds === 'number')
41
+ response.setHeader(constants_1.HTTP_HEADER.retryAfter, String(rest.retryAfterSeconds));
42
+ return response.status(status).json({ success: false, ...rest });
43
+ }
44
+ // ValidationPipe puts its list of problems in `message` as an array.
45
+ if (status === common_1.HttpStatus.BAD_REQUEST && typeof body === 'object' && Array.isArray(body.message)) {
46
+ return response.status(common_1.HttpStatus.BAD_REQUEST).json({
47
+ success: false,
48
+ code: constants_1.AUTH_ERRORS.validationFailed.code,
49
+ message: constants_1.AUTH_ERRORS.validationFailed.message,
50
+ problems: body.message,
51
+ });
52
+ }
53
+ const message = typeof body === 'string' ? body : (body.message ?? exception.message);
54
+ return response.status(status).json({ success: false, code: constants_1.ERROR_CODE_BY_HTTP_STATUS[status] ?? constants_1.AUTH_ERRORS.internalError.code, message: String(message) });
55
+ }
56
+ this.logger.error(exception instanceof Error ? exception.stack : String(exception));
57
+ return response
58
+ .status(constants_1.AUTH_ERRORS.internalError.status)
59
+ .json({ success: false, code: constants_1.AUTH_ERRORS.internalError.code, message: constants_1.AUTH_ERRORS.internalError.message });
60
+ }
61
+ };
62
+ exports.AuthExceptionFilter = AuthExceptionFilter;
63
+ exports.AuthExceptionFilter = AuthExceptionFilter = __decorate([
64
+ (0, common_1.Catch)()
65
+ ], AuthExceptionFilter);
66
+ //# sourceMappingURL=auth-exception.filter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"auth-exception.filter.js","sourceRoot":"","sources":["../../src/common/auth-exception.filter.ts"],"names":[],"mappings":";;;;;;;;;AAAA,2CAA0G;AAC1G,2CAA+F;AAE/F;;;;;;;;;;;;GAYG;AAEI,IAAM,mBAAmB,GAAzB,MAAM,mBAAmB;IACb,MAAM,GAAG,IAAI,eAAM,CAAC,uBAAW,CAAC,eAAe,CAAC,CAAC;IAElE;;;;;OAKG;IACH,KAAK,CAAC,SAAkB,EAAE,IAAmB;QAC3C,MAAM,QAAQ,GAAG,IAAI,CAAC,YAAY,EAAE,CAAC,WAAW,EAAE,CAAC;QAEnD,IAAI,SAAS,YAAY,sBAAa,EAAE,CAAC;YACvC,MAAM,MAAM,GAAG,SAAS,CAAC,SAAS,EAAE,CAAC;YACrC,MAAM,IAAI,GAAG,SAAS,CAAC,WAAW,EAAkC,CAAC;YAErE,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,OAAO,IAAI,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;gBAC9D,MAAM,EAAE,UAAU,EAAE,WAAW,EAAE,GAAG,IAAI,EAAE,GAAG,IAAI,CAAC;gBAClD,IAAI,OAAO,IAAI,CAAC,iBAAiB,KAAK,QAAQ;oBAAE,QAAQ,CAAC,SAAS,CAAC,uBAAW,CAAC,UAAU,EAAE,MAAM,CAAC,IAAI,CAAC,iBAAiB,CAAC,CAAC,CAAC;gBAC3H,OAAO,QAAQ,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,KAAK,EAAE,GAAG,IAAI,EAAE,CAAC,CAAC;YACnE,CAAC;YAED,qEAAqE;YACrE,IAAI,MAAM,KAAK,mBAAU,CAAC,WAAW,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,KAAK,CAAC,OAAO,CAAC,IAAI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