@miguelmorales13/nestkit 0.16.0 → 0.19.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (91) hide show
  1. package/README.md +212 -61
  2. package/dist/audit/index.cjs +12 -12
  3. package/dist/audit/index.js +2 -2
  4. package/dist/auth/index.cjs +12 -11
  5. package/dist/auth/index.js +5 -4
  6. package/dist/auth/oauth/http.d.ts +10 -3
  7. package/dist/auth/oauth/index.cjs +189 -30
  8. package/dist/auth/oauth/index.d.ts +6 -4
  9. package/dist/auth/oauth/index.js +169 -10
  10. package/dist/auth/oauth/magic-link.dto.d.ts +6 -0
  11. package/dist/auth/oauth/memory-magic-link.store.d.ts +19 -0
  12. package/dist/auth/oauth/oauth-auth.service.d.ts +46 -5
  13. package/dist/auth/oauth/oauth.controller.d.ts +5 -1
  14. package/dist/auth/oauth/oauth.options.d.ts +34 -1
  15. package/dist/auth/oauth/oauth.ports.d.ts +38 -0
  16. package/dist/bootstrap/index.cjs +8 -6
  17. package/dist/bootstrap/index.js +7 -5
  18. package/dist/{chunk-UWDHAZVE.cjs → chunk-3AUOOEQO.cjs} +2 -2
  19. package/dist/{chunk-JV4EAOF2.js → chunk-4PFAKLT3.js} +1 -1
  20. package/dist/{chunk-47FXU74G.cjs → chunk-52IOOHNX.cjs} +2 -2
  21. package/dist/chunk-6RAPVLJM.js +212 -0
  22. package/dist/{chunk-KRC47OX6.js → chunk-A2DGK4ZL.js} +26 -3
  23. package/dist/chunk-APUFKA4K.cjs +212 -0
  24. package/dist/chunk-AXMW5JF7.cjs +83 -0
  25. package/dist/{chunk-DABTRWA6.js → chunk-DYHSOULW.js} +1 -1
  26. package/dist/{chunk-KP2ZHJSR.js → chunk-ETRUVZTJ.js} +8 -0
  27. package/dist/{chunk-C5DV4RAX.cjs → chunk-FH5UNWWW.cjs} +6 -6
  28. package/dist/{chunk-4GT66KQS.cjs → chunk-FNUZGYUF.cjs} +4 -4
  29. package/dist/{chunk-FDNGAYTZ.cjs → chunk-HDFPSA2V.cjs} +6 -6
  30. package/dist/{chunk-U7QK24HL.cjs → chunk-HVHYZXBB.cjs} +2 -2
  31. package/dist/{chunk-LK3OINS7.js → chunk-IFNYRZP2.js} +1 -1
  32. package/dist/{chunk-YFYHLYHN.js → chunk-M3GQDRWR.js} +7 -1
  33. package/dist/{chunk-67B7IYAV.cjs → chunk-MSTXS3MU.cjs} +28 -5
  34. package/dist/chunk-PNK5WMPP.cjs +30 -0
  35. package/dist/chunk-Q5Y33L7B.cjs +19 -0
  36. package/dist/{chunk-EBPEMVZU.cjs → chunk-SDWHKPXI.cjs} +8 -0
  37. package/dist/{chunk-MLOQDUBN.js → chunk-TUNDBBQF.js} +4 -4
  38. package/dist/{chunk-UIVBPNH4.js → chunk-UNEVXCFW.js} +4 -4
  39. package/dist/chunk-VVMRZEIW.js +30 -0
  40. package/dist/{chunk-XX5ROVSC.cjs → chunk-W2T2VFLC.cjs} +3 -3
  41. package/dist/chunk-X4PQJZ4G.js +83 -0
  42. package/dist/{chunk-ORWJ7LES.js → chunk-ZFIDAX7K.js} +1 -1
  43. package/dist/{chunk-XHW4CTBS.js → chunk-ZJMLEP5H.js} +1 -1
  44. package/dist/crud/index.cjs +4 -4
  45. package/dist/crud/index.js +3 -3
  46. package/dist/email/adapters/brevo.email.d.ts +22 -0
  47. package/dist/email/adapters/memory.email.d.ts +14 -0
  48. package/dist/email/adapters/message.d.ts +13 -0
  49. package/dist/email/adapters/resend.email.d.ts +28 -0
  50. package/dist/email/adapters/smtp.email.d.ts +22 -0
  51. package/dist/email/address.d.ts +15 -0
  52. package/dist/email/email.module.d.ts +37 -0
  53. package/dist/email/email.options.d.ts +39 -0
  54. package/dist/email/email.port.d.ts +62 -0
  55. package/dist/email/index.cjs +314 -0
  56. package/dist/email/index.d.ts +12 -0
  57. package/dist/email/index.js +314 -0
  58. package/dist/errors/app.exception.d.ts +9 -1
  59. package/dist/errors/common.exceptions.d.ts +6 -5
  60. package/dist/errors/global-exception.filter.d.ts +9 -0
  61. package/dist/errors/index.cjs +8 -5
  62. package/dist/errors/index.js +9 -6
  63. package/dist/i18n/i18n.interceptor.d.ts +16 -0
  64. package/dist/i18n/i18n.module.d.ts +49 -3
  65. package/dist/i18n/index.cjs +26 -2
  66. package/dist/i18n/index.d.ts +8 -0
  67. package/dist/i18n/index.js +25 -1
  68. package/dist/i18n/locale.d.ts +53 -0
  69. package/dist/i18n/message.d.ts +36 -0
  70. package/dist/i18n/nestjs-i18n.translator.d.ts +9 -0
  71. package/dist/i18n/runtime.d.ts +55 -0
  72. package/dist/i18n/translator.port.d.ts +29 -0
  73. package/dist/idempotency/index.cjs +6 -5
  74. package/dist/idempotency/index.js +3 -2
  75. package/dist/index.cjs +38 -16
  76. package/dist/index.js +47 -25
  77. package/dist/invoicing/index.cjs +14 -13
  78. package/dist/invoicing/index.js +3 -2
  79. package/dist/payments/index.cjs +15 -14
  80. package/dist/payments/index.js +3 -2
  81. package/dist/response/index.cjs +3 -3
  82. package/dist/response/index.js +2 -2
  83. package/dist/stripe/index.cjs +4 -3
  84. package/dist/stripe/index.js +3 -2
  85. package/dist/tracking/index.cjs +3 -3
  86. package/dist/tracking/index.js +2 -2
  87. package/dist/tracking/request-context.d.ts +10 -0
  88. package/package.json +6 -1
  89. package/dist/chunk-IYUUYCP5.js +0 -36
  90. package/dist/chunk-M3EL5O6T.cjs +0 -36
  91. package/dist/chunk-R7BVS6CI.cjs +0 -13
@@ -1,13 +1,14 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } async function _asyncNullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return await rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
2
2
 
3
3
 
4
- var _chunkC5DV4RAXcjs = require('../../chunk-C5DV4RAX.cjs');
5
- require('../../chunk-EBPEMVZU.cjs');
4
+ var _chunkFH5UNWWWcjs = require('../../chunk-FH5UNWWW.cjs');
6
5
 
7
6
 
8
7
 
9
- var _chunkFDNGAYTZcjs = require('../../chunk-FDNGAYTZ.cjs');
10
- require('../../chunk-R7BVS6CI.cjs');
8
+ var _chunkHDFPSA2Vcjs = require('../../chunk-HDFPSA2V.cjs');
9
+ require('../../chunk-SDWHKPXI.cjs');
10
+ require('../../chunk-Q5Y33L7B.cjs');
11
+ require('../../chunk-PNK5WMPP.cjs');
11
12
 
12
13
 
13
14
 
@@ -127,6 +128,8 @@ async function exchangeCodeForToken(provider, code) {
127
128
  var OAUTH_AUTH_OPTIONS = /* @__PURE__ */ Symbol("OAUTH_AUTH_OPTIONS");
128
129
  var OAUTH_STORE = /* @__PURE__ */ Symbol("OAUTH_STORE");
129
130
  var REFRESH_TOKEN_STORE = /* @__PURE__ */ Symbol("REFRESH_TOKEN_STORE");
131
+ var MAGIC_LINK_STORE = /* @__PURE__ */ Symbol("MAGIC_LINK_STORE");
132
+ var MAGIC_LINK_SENDER = /* @__PURE__ */ Symbol("MAGIC_LINK_SENDER");
130
133
  function resolveOAuthOptions(options) {
131
134
  const webUrl = options.webUrl.replace(/\/$/, "");
132
135
  return {
@@ -141,6 +144,12 @@ function resolveOAuthOptions(options) {
141
144
  sameSite: _nullishCoalesce(_optionalChain([options, 'access', _7 => _7.cookie, 'optionalAccess', _8 => _8.sameSite]), () => ( "lax")),
142
145
  secure: _nullishCoalesce(_optionalChain([options, 'access', _9 => _9.cookie, 'optionalAccess', _10 => _10.secure]), () => ( process.env.NODE_ENV === "production"))
143
146
  },
147
+ magicLink: {
148
+ ttlSeconds: _nullishCoalesce(_optionalChain([options, 'access', _11 => _11.magicLink, 'optionalAccess', _12 => _12.ttlSeconds]), () => ( 15 * 60)),
149
+ allowSignUp: _nullishCoalesce(_optionalChain([options, 'access', _13 => _13.magicLink, 'optionalAccess', _14 => _14.allowSignUp]), () => ( true)),
150
+ linkUrl: _nullishCoalesce(_optionalChain([options, 'access', _15 => _15.magicLink, 'optionalAccess', _16 => _16.linkUrl]), () => ( ((token) => `${webUrl}/magic-link#token=${encodeURIComponent(token)}`))),
151
+ invalidLinkMessage: _nullishCoalesce(_optionalChain([options, 'access', _17 => _17.magicLink, 'optionalAccess', _18 => _18.invalidLinkMessage]), () => ( (() => "This sign-in link is invalid or has expired. Request a new one.")))
152
+ },
144
153
  linkCookieName: _nullishCoalesce(options.linkCookieName, () => ( "link_intent")),
145
154
  stateCookieName: _nullishCoalesce(options.stateCookieName, () => ( "oauth_state")),
146
155
  accessTtlSeconds: _nullishCoalesce(options.accessTtlSeconds, () => ( 15 * 60)),
@@ -162,6 +171,7 @@ function resolveOAuthOptions(options) {
162
171
  var _common = require('@nestjs/common');
163
172
  var _crypto = require('crypto');
164
173
  var _jsonwebtoken = require('jsonwebtoken'); var _jsonwebtoken2 = _interopRequireDefault(_jsonwebtoken);
174
+ var MAGIC_LINK_PROVIDER = "email";
165
175
  function hashToken(token) {
166
176
  return _crypto.createHash.call(void 0, "sha256").update(token).digest("hex");
167
177
  }
@@ -171,9 +181,11 @@ function requireSecret(explicit, envName) {
171
181
  return value;
172
182
  }
173
183
  var OAuthAuthService = class {
174
- constructor(options, store, sessions) {
184
+ constructor(options, store, sessions, magicLinks, magicLinkSender) {
175
185
  this.store = store;
176
186
  this.sessions = sessions;
187
+ this.magicLinks = magicLinks;
188
+ this.magicLinkSender = magicLinkSender;
177
189
  this.options = resolveOAuthOptions(options);
178
190
  }
179
191
  get config() {
@@ -190,7 +202,7 @@ var OAuthAuthService = class {
190
202
  */
191
203
  provider(name) {
192
204
  const found = this.options.providers.find((p) => p.name === name);
193
- if (!_optionalChain([found, 'optionalAccess', _11 => _11.clientId]) || !found.clientSecret) {
205
+ if (!_optionalChain([found, 'optionalAccess', _19 => _19.clientId]) || !found.clientSecret) {
194
206
  throw new (0, _common.ServiceUnavailableException)(this.options.unavailableMessage(name));
195
207
  }
196
208
  return found;
@@ -256,7 +268,7 @@ var OAuthAuthService = class {
256
268
  const accessToken = await exchangeCodeForToken(provider, code);
257
269
  const profile = await provider.fetchProfile(accessToken);
258
270
  if (!profile.email) {
259
- throw new (0, _chunkFDNGAYTZcjs.UnauthorizedAppException)(this.options.missingEmailMessage(name));
271
+ throw new (0, _chunkHDFPSA2Vcjs.UnauthorizedAppException)(this.options.missingEmailMessage(name));
260
272
  }
261
273
  return { ...profile, email: normaliseEmail(profile.email) };
262
274
  }
@@ -271,7 +283,7 @@ var OAuthAuthService = class {
271
283
  async login(providerName, profile) {
272
284
  const provider = this.provider(providerName).name;
273
285
  const existing = await this.store.findAccount(provider, profile.providerAccountId);
274
- let user = _nullishCoalesce(_optionalChain([existing, 'optionalAccess', _12 => _12.user]), () => ( null));
286
+ let user = _nullishCoalesce(_optionalChain([existing, 'optionalAccess', _20 => _20.user]), () => ( null));
275
287
  if (!user) {
276
288
  const email = normaliseEmail(profile.email);
277
289
  user = await _asyncNullishCoalesce(await this.store.findUserByEmail(email), async () => ( await this.store.createUser({
@@ -296,6 +308,69 @@ var OAuthAuthService = class {
296
308
  if (changes && (changes.name !== user.name || changes.avatarUrl !== user.avatarUrl)) {
297
309
  user = await this.store.updateUser(user.id, changes);
298
310
  }
311
+ return this.establishSession(user);
312
+ }
313
+ /**
314
+ * Sends a sign-in link to an address.
315
+ *
316
+ * Answers nothing, whatever happened: whether the address is known is not
317
+ * something a caller should be able to learn from this endpoint, so an
318
+ * unknown one (with `allowSignUp: false`) simply gets no email. The sender's
319
+ * own failure does propagate — that is the mail provider being down, which
320
+ * says nothing about the address and is something the operator must see.
321
+ *
322
+ * Throttling is the consumer's job: this will send as often as it is asked.
323
+ */
324
+ async requestMagicLink(rawEmail) {
325
+ const { store, sender } = this.requireMagicLink();
326
+ const email = normaliseEmail(rawEmail);
327
+ if (!this.options.magicLink.allowSignUp && !await this.store.findUserByEmail(email)) {
328
+ return;
329
+ }
330
+ const token = _crypto.randomBytes.call(void 0, 32).toString("base64url");
331
+ const expiresAt = new Date(Date.now() + this.options.magicLink.ttlSeconds * 1e3);
332
+ await store.create(hashToken(token), email, expiresAt);
333
+ await sender.send({ email, url: this.options.magicLink.linkUrl(token), expiresAt });
334
+ }
335
+ /**
336
+ * Spends a link and opens a session for whoever it was sent to.
337
+ *
338
+ * Spent before anything else, and only continued when the store reports a
339
+ * live row — the same shape as `refresh`. The user is found by email, so a
340
+ * link lands on the account Google or Facebook created earlier, exactly as
341
+ * signing in with the other provider would. Nothing on the profile is
342
+ * touched: a link carries no name or photo, and running `syncProfile` with
343
+ * an empty profile would erase what the OAuth provider supplied.
344
+ */
345
+ async magicLinkLogin(rawToken) {
346
+ const { store } = this.requireMagicLink();
347
+ const invalid = () => new (0, _chunkHDFPSA2Vcjs.UnauthorizedAppException)(this.options.magicLink.invalidLinkMessage(), void 0, "MAGIC_LINK_INVALID");
348
+ const record = await store.consumeIfActive(hashToken(rawToken));
349
+ if (!record || record.expiresAt.getTime() <= Date.now()) throw invalid();
350
+ const email = normaliseEmail(record.email);
351
+ let user = await this.store.findUserByEmail(email);
352
+ if (!user) {
353
+ if (!this.options.magicLink.allowSignUp) throw invalid();
354
+ user = await this.store.createUser({ email });
355
+ }
356
+ return this.establishSession(user);
357
+ }
358
+ /** Whether the magic-link routes can work in this app. */
359
+ get magicLinkEnabled() {
360
+ return Boolean(this.magicLinks && this.magicLinkSender);
361
+ }
362
+ requireMagicLink() {
363
+ if (!this.magicLinks || !this.magicLinkSender) {
364
+ throw new (0, _common.ServiceUnavailableException)(this.options.unavailableMessage(MAGIC_LINK_PROVIDER));
365
+ }
366
+ return { store: this.magicLinks, sender: this.magicLinkSender };
367
+ }
368
+ /**
369
+ * The one place a session is opened. Every sign-in — OAuth or magic link —
370
+ * ends here, so `singleSession` and the token pair behave identically
371
+ * whichever door was used.
372
+ */
373
+ async establishSession(user) {
299
374
  if (this.options.singleSession) {
300
375
  await this.sessions.revokeAllForUser(user.id);
301
376
  }
@@ -312,9 +387,9 @@ var OAuthAuthService = class {
312
387
  async refresh(rawRefreshToken) {
313
388
  const payload = this.verifyRefresh(rawRefreshToken);
314
389
  const { count } = await this.sessions.revokeIfActive(hashToken(rawRefreshToken));
315
- if (count === 0) throw new (0, _chunkFDNGAYTZcjs.UnauthorizedAppException)("Invalid refresh token.");
390
+ if (count === 0) throw new (0, _chunkHDFPSA2Vcjs.UnauthorizedAppException)("Invalid refresh token.");
316
391
  const user = await this.store.findUserById(payload.sub);
317
- if (!user) throw new (0, _chunkFDNGAYTZcjs.UnauthorizedAppException)("Invalid refresh token.");
392
+ if (!user) throw new (0, _chunkHDFPSA2Vcjs.UnauthorizedAppException)("Invalid refresh token.");
318
393
  return this.issueTokenPair(user);
319
394
  }
320
395
  async logout(rawRefreshToken) {
@@ -322,7 +397,7 @@ var OAuthAuthService = class {
322
397
  }
323
398
  async currentUser(userId) {
324
399
  const user = await this.store.findUserById(userId);
325
- if (!user) throw new (0, _chunkFDNGAYTZcjs.UnauthorizedAppException)("User not found.");
400
+ if (!user) throw new (0, _chunkHDFPSA2Vcjs.UnauthorizedAppException)("User not found.");
326
401
  return this.present(user);
327
402
  }
328
403
  /**
@@ -373,7 +448,7 @@ var OAuthAuthService = class {
373
448
  const provider = this.provider(providerName).name;
374
449
  const existing = await this.store.findAccount(provider, profile.providerAccountId);
375
450
  if (existing && existing.userId !== userId) {
376
- throw new (0, _chunkFDNGAYTZcjs.ValidationAppException)(
451
+ throw new (0, _chunkHDFPSA2Vcjs.ValidationAppException)(
377
452
  "That account is already connected to another profile.",
378
453
  { provider },
379
454
  "OAUTH_ACCOUNT_TAKEN"
@@ -381,7 +456,7 @@ var OAuthAuthService = class {
381
456
  }
382
457
  const alreadyLinked = await this.store.findAccountByUserAndProvider(userId, provider);
383
458
  if (alreadyLinked && alreadyLinked.providerAccountId !== profile.providerAccountId) {
384
- throw new (0, _chunkFDNGAYTZcjs.ValidationAppException)(
459
+ throw new (0, _chunkHDFPSA2Vcjs.ValidationAppException)(
385
460
  "You already have an account from that provider connected.",
386
461
  { provider },
387
462
  "OAUTH_PROVIDER_ALREADY_LINKED"
@@ -444,7 +519,7 @@ var OAuthAuthService = class {
444
519
  requireSecret(this.options.refreshSecret, "JWT_REFRESH_SECRET")
445
520
  );
446
521
  } catch (e4) {
447
- throw new (0, _chunkFDNGAYTZcjs.UnauthorizedAppException)("Invalid refresh token.");
522
+ throw new (0, _chunkHDFPSA2Vcjs.UnauthorizedAppException)("Invalid refresh token.");
448
523
  }
449
524
  }
450
525
  };
@@ -452,12 +527,51 @@ OAuthAuthService = exports.OAuthAuthService = _chunk2REOCMUDcjs.__decorateClass.
452
527
  _common.Injectable.call(void 0, ),
453
528
  _chunk2REOCMUDcjs.__decorateParam.call(void 0, 0, _common.Inject.call(void 0, OAUTH_AUTH_OPTIONS)),
454
529
  _chunk2REOCMUDcjs.__decorateParam.call(void 0, 1, _common.Inject.call(void 0, OAUTH_STORE)),
455
- _chunk2REOCMUDcjs.__decorateParam.call(void 0, 2, _common.Inject.call(void 0, REFRESH_TOKEN_STORE))
530
+ _chunk2REOCMUDcjs.__decorateParam.call(void 0, 2, _common.Inject.call(void 0, REFRESH_TOKEN_STORE)),
531
+ _chunk2REOCMUDcjs.__decorateParam.call(void 0, 3, _common.Optional.call(void 0, )),
532
+ _chunk2REOCMUDcjs.__decorateParam.call(void 0, 3, _common.Inject.call(void 0, MAGIC_LINK_STORE)),
533
+ _chunk2REOCMUDcjs.__decorateParam.call(void 0, 4, _common.Optional.call(void 0, )),
534
+ _chunk2REOCMUDcjs.__decorateParam.call(void 0, 4, _common.Inject.call(void 0, MAGIC_LINK_SENDER))
456
535
  ], OAuthAuthService);
457
536
  function normaliseEmail(email) {
458
537
  return email.trim().toLowerCase();
459
538
  }
460
539
 
540
+ // src/auth/oauth/memory-magic-link.store.ts
541
+ var MemoryMagicLinkStore = class {
542
+ constructor(maxEntries = 1e4) {
543
+ this.maxEntries = maxEntries;
544
+ this.links = /* @__PURE__ */ new Map();
545
+ }
546
+ async create(tokenHash, email, expiresAt) {
547
+ if (this.links.size >= this.maxEntries) {
548
+ const oldest = this.links.keys().next().value;
549
+ if (oldest !== void 0) this.links.delete(oldest);
550
+ }
551
+ this.links.set(tokenHash, { email, expiresAt });
552
+ }
553
+ async consumeIfActive(tokenHash) {
554
+ const record = this.links.get(tokenHash);
555
+ this.links.delete(tokenHash);
556
+ if (!record || record.expiresAt.getTime() <= Date.now()) return null;
557
+ return record;
558
+ }
559
+ };
560
+
561
+ // src/auth/oauth/magic-link.dto.ts
562
+ var _classvalidator = require('class-validator');
563
+ var MagicLinkRequestDto = class {
564
+ };
565
+ _chunk2REOCMUDcjs.__decorateClass.call(void 0, [
566
+ _classvalidator.IsEmail.call(void 0, )
567
+ ], MagicLinkRequestDto.prototype, "email", 2);
568
+ var MagicLinkVerifyDto = class {
569
+ };
570
+ _chunk2REOCMUDcjs.__decorateClass.call(void 0, [
571
+ _classvalidator.IsString.call(void 0, ),
572
+ _classvalidator.MinLength.call(void 0, 16)
573
+ ], MagicLinkVerifyDto.prototype, "token", 2);
574
+
461
575
  // src/auth/oauth/oauth-callback.filter.ts
462
576
 
463
577
 
@@ -473,10 +587,12 @@ function setCookie(reply, name, value, options) {
473
587
  else throw new Error("No cookie support on the reply: register @fastify/cookie or cookie-parser.");
474
588
  }
475
589
  function clearCookie(reply, name, options) {
476
- _optionalChain([reply, 'access', _13 => _13.clearCookie, 'optionalCall', _14 => _14(name, options)]);
590
+ _optionalChain([reply, 'access', _21 => _21.clearCookie, 'optionalCall', _22 => _22(name, options)]);
477
591
  }
478
592
  function redirect(reply, url) {
479
- reply.redirect(url, 302);
593
+ const send = reply.redirect;
594
+ if (typeof reply.code === "function") send.call(reply, url, 302);
595
+ else send.call(reply, 302, url);
480
596
  }
481
597
 
482
598
  // src/auth/oauth/oauth-callback.filter.ts
@@ -511,22 +627,26 @@ OAuthCallbackFilter = exports.OAuthCallbackFilter = _chunk2REOCMUDcjs.__decorate
511
627
 
512
628
 
513
629
 
630
+
631
+
514
632
  function createOAuthController(options = {}) {
515
- const Guard = _nullishCoalesce(options.guard, () => ( _chunkC5DV4RAXcjs.JwtAuthGuard));
633
+ const Guard = _nullishCoalesce(options.guard, () => ( _chunkFH5UNWWWcjs.JwtAuthGuard));
516
634
  const userId = _nullishCoalesce(options.userId, () => ( ((user) => user.sub)));
517
635
  let OAuthControllerHost = class {
636
+ // Explicit token: the build emits no `design:paramtypes`, so a subclass
637
+ // with no constructor of its own would otherwise get `auth` undefined.
518
638
  constructor(auth) {
519
639
  this.auth = auth;
520
640
  }
521
641
  async refresh(request, reply) {
522
- const raw = _optionalChain([request, 'access', _15 => _15.cookies, 'optionalAccess', _16 => _16[this.auth.config.cookie.name]]);
523
- if (!raw) throw new (0, _chunkFDNGAYTZcjs.UnauthorizedAppException)("No refresh token.");
642
+ const raw = _optionalChain([request, 'access', _23 => _23.cookies, 'optionalAccess', _24 => _24[this.auth.config.cookie.name]]);
643
+ if (!raw) throw new (0, _chunkHDFPSA2Vcjs.UnauthorizedAppException)("No refresh token.");
524
644
  const { accessToken, refreshToken, refreshTokenExpiresAt } = await this.auth.refresh(raw);
525
645
  this.writeRefreshCookie(reply, refreshToken, refreshTokenExpiresAt);
526
646
  return { accessToken };
527
647
  }
528
648
  async logout(request, reply) {
529
- const raw = _optionalChain([request, 'access', _17 => _17.cookies, 'optionalAccess', _18 => _18[this.auth.config.cookie.name]]);
649
+ const raw = _optionalChain([request, 'access', _25 => _25.cookies, 'optionalAccess', _26 => _26[this.auth.config.cookie.name]]);
530
650
  if (raw) await this.auth.logout(raw);
531
651
  clearCookie(reply, this.auth.config.cookie.name, { path: this.auth.config.cookie.path });
532
652
  return { success: true };
@@ -545,6 +665,23 @@ function createOAuthController(options = {}) {
545
665
  });
546
666
  return { ok: true };
547
667
  }
668
+ async requestMagicLink(body) {
669
+ await this.auth.requestMagicLink(body.email);
670
+ return { ok: true };
671
+ }
672
+ async verifyMagicLink(body, reply) {
673
+ const { user, accessToken, refreshToken, refreshTokenExpiresAt } = await this.auth.magicLinkLogin(
674
+ body.token
675
+ );
676
+ this.writeRefreshCookie(reply, refreshToken, refreshTokenExpiresAt);
677
+ return { accessToken, user };
678
+ }
679
+ async magicLinkCallback(token, reply) {
680
+ if (!token) throw new (0, _chunkHDFPSA2Vcjs.UnauthorizedAppException)(this.auth.config.magicLink.invalidLinkMessage());
681
+ const { accessToken, refreshToken, refreshTokenExpiresAt } = await this.auth.magicLinkLogin(token);
682
+ this.writeRefreshCookie(reply, refreshToken, refreshTokenExpiresAt);
683
+ redirect(reply, this.auth.config.onLoginRedirect(accessToken));
684
+ }
548
685
  start(provider, reply) {
549
686
  const { url, state, expiresAt } = this.auth.authorizeUrl(provider);
550
687
  setCookie(reply, this.auth.config.stateCookieName, state, {
@@ -554,16 +691,16 @@ function createOAuthController(options = {}) {
554
691
  redirect(reply, url);
555
692
  }
556
693
  async callback(provider, code, state, request, reply) {
557
- if (!code) throw new (0, _chunkFDNGAYTZcjs.UnauthorizedAppException)(this.auth.config.genericErrorMessage());
558
- const stateCookie = _optionalChain([request, 'access', _19 => _19.cookies, 'optionalAccess', _20 => _20[this.auth.config.stateCookieName]]);
694
+ if (!code) throw new (0, _chunkHDFPSA2Vcjs.UnauthorizedAppException)(this.auth.config.genericErrorMessage());
695
+ const stateCookie = _optionalChain([request, 'access', _27 => _27.cookies, 'optionalAccess', _28 => _28[this.auth.config.stateCookieName]]);
559
696
  clearCookie(reply, this.auth.config.stateCookieName, {
560
697
  path: this.auth.config.cookie.path
561
698
  });
562
699
  if (!this.auth.verifyState(state, stateCookie, provider)) {
563
- throw new (0, _chunkFDNGAYTZcjs.UnauthorizedAppException)(this.auth.config.genericErrorMessage());
700
+ throw new (0, _chunkHDFPSA2Vcjs.UnauthorizedAppException)(this.auth.config.genericErrorMessage());
564
701
  }
565
702
  const profile = await this.auth.profileFromCode(provider, code);
566
- const linkUserId = this.auth.readLinkToken(_optionalChain([request, 'access', _21 => _21.cookies, 'optionalAccess', _22 => _22[this.auth.config.linkCookieName]]));
703
+ const linkUserId = this.auth.readLinkToken(_optionalChain([request, 'access', _29 => _29.cookies, 'optionalAccess', _30 => _30[this.auth.config.linkCookieName]]));
567
704
  if (linkUserId) {
568
705
  clearCookie(reply, this.auth.config.linkCookieName, { path: this.auth.config.cookie.path });
569
706
  try {
@@ -606,19 +743,34 @@ function createOAuthController(options = {}) {
606
743
  _chunk2REOCMUDcjs.__decorateClass.call(void 0, [
607
744
  _common.UseGuards.call(void 0, Guard),
608
745
  _common.Get.call(void 0, "me"),
609
- _chunk2REOCMUDcjs.__decorateParam.call(void 0, 0, _chunkC5DV4RAXcjs.CurrentUser.call(void 0, ))
746
+ _chunk2REOCMUDcjs.__decorateParam.call(void 0, 0, _chunkFH5UNWWWcjs.CurrentUser.call(void 0, ))
610
747
  ], OAuthControllerHost.prototype, "me", 1);
611
748
  _chunk2REOCMUDcjs.__decorateClass.call(void 0, [
612
749
  _common.UseGuards.call(void 0, Guard),
613
750
  _common.Get.call(void 0, "linked-accounts"),
614
- _chunk2REOCMUDcjs.__decorateParam.call(void 0, 0, _chunkC5DV4RAXcjs.CurrentUser.call(void 0, ))
751
+ _chunk2REOCMUDcjs.__decorateParam.call(void 0, 0, _chunkFH5UNWWWcjs.CurrentUser.call(void 0, ))
615
752
  ], OAuthControllerHost.prototype, "linkedAccounts", 1);
616
753
  _chunk2REOCMUDcjs.__decorateClass.call(void 0, [
617
754
  _common.UseGuards.call(void 0, Guard),
618
755
  _common.Post.call(void 0, "link-intent"),
619
- _chunk2REOCMUDcjs.__decorateParam.call(void 0, 0, _chunkC5DV4RAXcjs.CurrentUser.call(void 0, )),
756
+ _chunk2REOCMUDcjs.__decorateParam.call(void 0, 0, _chunkFH5UNWWWcjs.CurrentUser.call(void 0, )),
620
757
  _chunk2REOCMUDcjs.__decorateParam.call(void 0, 1, _common.Res.call(void 0, { passthrough: true }))
621
758
  ], OAuthControllerHost.prototype, "linkIntent", 1);
759
+ _chunk2REOCMUDcjs.__decorateClass.call(void 0, [
760
+ _common.Post.call(void 0, "magic-link"),
761
+ _chunk2REOCMUDcjs.__decorateParam.call(void 0, 0, _common.Body.call(void 0, ))
762
+ ], OAuthControllerHost.prototype, "requestMagicLink", 1);
763
+ _chunk2REOCMUDcjs.__decorateClass.call(void 0, [
764
+ _common.Post.call(void 0, "magic-link/verify"),
765
+ _chunk2REOCMUDcjs.__decorateParam.call(void 0, 0, _common.Body.call(void 0, )),
766
+ _chunk2REOCMUDcjs.__decorateParam.call(void 0, 1, _common.Res.call(void 0, { passthrough: true }))
767
+ ], OAuthControllerHost.prototype, "verifyMagicLink", 1);
768
+ _chunk2REOCMUDcjs.__decorateClass.call(void 0, [
769
+ _common.Get.call(void 0, "magic-link/callback"),
770
+ _common.UseFilters.call(void 0, OAuthCallbackFilter),
771
+ _chunk2REOCMUDcjs.__decorateParam.call(void 0, 0, _common.Query.call(void 0, "token")),
772
+ _chunk2REOCMUDcjs.__decorateParam.call(void 0, 1, _common.Res.call(void 0, ))
773
+ ], OAuthControllerHost.prototype, "magicLinkCallback", 1);
622
774
  _chunk2REOCMUDcjs.__decorateClass.call(void 0, [
623
775
  _common.Get.call(void 0, ":provider"),
624
776
  _chunk2REOCMUDcjs.__decorateParam.call(void 0, 0, _common.Param.call(void 0, "provider")),
@@ -634,7 +786,8 @@ function createOAuthController(options = {}) {
634
786
  _chunk2REOCMUDcjs.__decorateParam.call(void 0, 4, _common.Res.call(void 0, ))
635
787
  ], OAuthControllerHost.prototype, "callback", 1);
636
788
  OAuthControllerHost = _chunk2REOCMUDcjs.__decorateClass.call(void 0, [
637
- _common.Controller.call(void 0, _nullishCoalesce(options.path, () => ( "auth")))
789
+ _common.Controller.call(void 0, _nullishCoalesce(options.path, () => ( "auth"))),
790
+ _chunk2REOCMUDcjs.__decorateParam.call(void 0, 0, _common.Inject.call(void 0, OAuthAuthService))
638
791
  ], OAuthControllerHost);
639
792
  return OAuthControllerHost;
640
793
  }
@@ -675,4 +828,10 @@ OAuthAuthModule = exports.OAuthAuthModule = _chunk2REOCMUDcjs.__decorateClass.ca
675
828
 
676
829
 
677
830
 
678
- exports.OAUTH_AUTH_OPTIONS = OAUTH_AUTH_OPTIONS; exports.OAUTH_STORE = OAUTH_STORE; exports.OAuthAuthModule = OAuthAuthModule; exports.OAuthAuthService = OAuthAuthService; exports.OAuthCallbackFilter = OAuthCallbackFilter; exports.REFRESH_TOKEN_STORE = REFRESH_TOKEN_STORE; exports.buildAuthorizeUrl = buildAuthorizeUrl; exports.createOAuthController = createOAuthController; exports.exchangeCodeForToken = exchangeCodeForToken; exports.facebookProvider = facebookProvider; exports.googleProvider = googleProvider; exports.resolveOAuthOptions = resolveOAuthOptions;
831
+
832
+
833
+
834
+
835
+
836
+
837
+ exports.MAGIC_LINK_PROVIDER = MAGIC_LINK_PROVIDER; exports.MAGIC_LINK_SENDER = MAGIC_LINK_SENDER; exports.MAGIC_LINK_STORE = MAGIC_LINK_STORE; exports.MagicLinkRequestDto = MagicLinkRequestDto; exports.MagicLinkVerifyDto = MagicLinkVerifyDto; exports.MemoryMagicLinkStore = MemoryMagicLinkStore; exports.OAUTH_AUTH_OPTIONS = OAUTH_AUTH_OPTIONS; exports.OAUTH_STORE = OAUTH_STORE; exports.OAuthAuthModule = OAuthAuthModule; exports.OAuthAuthService = OAuthAuthService; exports.OAuthCallbackFilter = OAuthCallbackFilter; exports.REFRESH_TOKEN_STORE = REFRESH_TOKEN_STORE; exports.buildAuthorizeUrl = buildAuthorizeUrl; exports.createOAuthController = createOAuthController; exports.exchangeCodeForToken = exchangeCodeForToken; exports.facebookProvider = facebookProvider; exports.googleProvider = googleProvider; exports.resolveOAuthOptions = resolveOAuthOptions;
@@ -1,9 +1,11 @@
1
1
  export { googleProvider, facebookProvider, buildAuthorizeUrl, exchangeCodeForToken, } from './oauth-provider.js';
2
2
  export type { OAuthProfile, OAuthProviderConfig, ProviderCredentials, } from './oauth-provider.js';
3
- export type { OAuthUser, OAuthAccount, OAuthStorePort, RefreshTokenStorePort, ProfileSyncPolicy, } from './oauth.ports.js';
4
- export { OAUTH_AUTH_OPTIONS, OAUTH_STORE, REFRESH_TOKEN_STORE, resolveOAuthOptions, } from './oauth.options.js';
5
- export type { OAuthAuthOptions, OAuthCookieOptions, ResolvedOAuthOptions, } from './oauth.options.js';
6
- export { OAuthAuthService } from './oauth-auth.service.js';
3
+ export type { OAuthUser, OAuthAccount, OAuthStorePort, RefreshTokenStorePort, ProfileSyncPolicy, MagicLinkStorePort, MagicLinkSenderPort, MagicLinkDelivery, } from './oauth.ports.js';
4
+ export { OAUTH_AUTH_OPTIONS, OAUTH_STORE, REFRESH_TOKEN_STORE, MAGIC_LINK_STORE, MAGIC_LINK_SENDER, resolveOAuthOptions, } from './oauth.options.js';
5
+ export type { OAuthAuthOptions, OAuthCookieOptions, MagicLinkOptions, ResolvedOAuthOptions, } from './oauth.options.js';
6
+ export { OAuthAuthService, MAGIC_LINK_PROVIDER } from './oauth-auth.service.js';
7
+ export { MemoryMagicLinkStore } from './memory-magic-link.store.js';
8
+ export { MagicLinkRequestDto, MagicLinkVerifyDto } from './magic-link.dto.js';
7
9
  export type { TokenPair } from './oauth-auth.service.js';
8
10
  export { OAuthCallbackFilter } from './oauth-callback.filter.js';
9
11
  export { createOAuthController } from './oauth.controller.js';