@dudousxd/nestjs-agent-dashboard 0.10.0 → 0.10.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.
@@ -303,6 +303,30 @@ function clearSessionCookie(context) {
303
303
  appendSetCookie(context.response, cookie);
304
304
  }
305
305
  __name(clearSessionCookie, "clearSessionCookie");
306
+ var inFlightRevalidations = /* @__PURE__ */ new Map();
307
+ function revalidationKey(session) {
308
+ return `${session.sub}:${session.iat}`;
309
+ }
310
+ __name(revalidationKey, "revalidationKey");
311
+ function getOrStartRevalidation(auth, session, user) {
312
+ const hook = auth.revalidate;
313
+ if (!hook) return Promise.resolve(true);
314
+ const key = revalidationKey(session);
315
+ const inFlight = inFlightRevalidations.get(key);
316
+ if (inFlight) return inFlight;
317
+ const outcome = (async () => {
318
+ try {
319
+ return await hook(user);
320
+ } catch {
321
+ return false;
322
+ }
323
+ })().finally(() => {
324
+ inFlightRevalidations.delete(key);
325
+ });
326
+ inFlightRevalidations.set(key, outcome);
327
+ return outcome;
328
+ }
329
+ __name(getOrStartRevalidation, "getOrStartRevalidation");
306
330
  async function maybeRenewSession(auth, session, request, response, now = Date.now()) {
307
331
  if (now - session.iat <= auth.ttlMs / 2) return true;
308
332
  const user = {
@@ -312,20 +336,13 @@ async function maybeRenewSession(auth, session, request, response, now = Date.no
312
336
  } : {},
313
337
  roles: session.roles
314
338
  };
315
- if (auth.revalidate) {
316
- let allowed;
317
- try {
318
- allowed = await auth.revalidate(user);
319
- } catch {
320
- allowed = false;
321
- }
322
- if (!allowed) {
323
- clearSessionCookie({
324
- request,
325
- response
326
- });
327
- return false;
328
- }
339
+ const allowed = await getOrStartRevalidation(auth, session, user);
340
+ if (!allowed) {
341
+ clearSessionCookie({
342
+ request,
343
+ response
344
+ });
345
+ return false;
329
346
  }
330
347
  issueSessionCookie(user, {
331
348
  auth,
@@ -1347,7 +1364,7 @@ var AgentDashboardAuthController = class _AgentDashboardAuthController {
1347
1364
  }
1348
1365
  loginPage(req, res, returnToQuery, error) {
1349
1366
  const auth = this.requireAuth();
1350
- if (!auth.login) throw new import_common7.NotFoundException();
1367
+ if (!auth.modes.includes("login")) throw new import_common7.NotFoundException();
1351
1368
  const returnTo = sanitizeReturnTo(this.basePath, returnToQuery);
1352
1369
  if (this.currentSession(req)) {
1353
1370
  res.status(302);
@@ -1362,7 +1379,7 @@ var AgentDashboardAuthController = class _AgentDashboardAuthController {
1362
1379
  }
1363
1380
  async login(body, req, res) {
1364
1381
  const auth = this.requireAuth();
1365
- if (!auth.login) throw new import_common7.NotFoundException();
1382
+ if (!auth.modes.includes("login")) throw new import_common7.NotFoundException();
1366
1383
  const returnTo = sanitizeReturnTo(this.basePath, body?.returnTo);
1367
1384
  const username = body?.username;
1368
1385
  const password = body?.password;
@@ -1390,14 +1407,14 @@ var AgentDashboardAuthController = class _AgentDashboardAuthController {
1390
1407
  response: res
1391
1408
  });
1392
1409
  res.status(302);
1393
- res.setHeader("Location", this.auth && !this.auth.login ? `${this.basePath}/auth/session-required` : `${this.basePath}/auth/login`);
1410
+ res.setHeader("Location", this.auth && !this.auth.modes.includes("login") ? `${this.basePath}/auth/session-required` : `${this.basePath}/auth/login`);
1394
1411
  return "";
1395
1412
  }
1396
1413
  // Mode A: the host frontend (carrying its own auth) POSTs here; the host hook validates the raw
1397
1414
  // request and returns the session user, or `null` to deny. No credential reaches this library.
1398
1415
  async session(request, response) {
1399
1416
  const auth = this.requireAuth();
1400
- if (!auth.session) throw new import_common7.NotFoundException();
1417
+ if (!auth.modes.includes("session")) throw new import_common7.NotFoundException();
1401
1418
  const user = await this.runHook("session", () => auth.session?.(request) ?? null);
1402
1419
  if (!user) throw new import_common7.UnauthorizedException();
1403
1420
  issueSessionCookie(user, {
@@ -1411,7 +1428,7 @@ var AgentDashboardAuthController = class _AgentDashboardAuthController {
1411
1428
  // 404s under Mode A (see `loginPage` above).
1412
1429
  sessionRequiredPage() {
1413
1430
  const auth = this.requireAuth();
1414
- if (auth.login) throw new import_common7.NotFoundException();
1431
+ if (auth.modes.includes("login")) throw new import_common7.NotFoundException();
1415
1432
  return renderSessionRequiredPage();
1416
1433
  }
1417
1434
  /** Uniform failure: same redirect (generic error flag) whether the user is unknown or the password is wrong. */
@@ -1657,7 +1674,7 @@ var DashboardAuthPageGuard = class {
1657
1674
  * asserting away) the nullability.
1658
1675
  */
1659
1676
  deny(auth, request) {
1660
- if (!auth.login) {
1677
+ if (!auth.modes.includes("login")) {
1661
1678
  throw new DashboardAuthRedirect(`${this.basePath}/auth/session-required`);
1662
1679
  }
1663
1680
  const returnTo = readOriginalUrl(request);