auth-vir 2.3.6 → 2.3.8

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.
@@ -1,4 +1,4 @@
1
- import { createBlockingInterval, type JsonCompatibleObject, type MaybePromise, type PartialWithUndefined, type SelectFrom } from '@augment-vir/common';
1
+ import { type createBlockingInterval, type JsonCompatibleObject, type MaybePromise, type PartialWithUndefined, type SelectFrom } from '@augment-vir/common';
2
2
  import { type AnyDuration } from 'date-vir';
3
3
  import { type EmptyObject } from 'type-fest';
4
4
  /**
@@ -26,12 +26,19 @@ export type FrontendAuthClientConfig = PartialWithUndefined<{
26
26
  *
27
27
  * If the user is not currently authorized, this should return `undefined` to prevent
28
28
  * unnecessary network traffic.
29
+ *
30
+ * This will be called any time the user interacts with the page, debounced by the adjacent
31
+ * `debounce` property.
29
32
  */
30
33
  performCheck: () => MaybePromise<SelectFrom<Response, {
31
34
  status: true;
32
35
  }> | undefined>;
33
- /** @default {minutes: 1} */
34
- interval?: AnyDuration | undefined;
36
+ /**
37
+ * Debounce for firing `performCheck`.
38
+ *
39
+ * @default {minutes: 1}
40
+ */
41
+ debounce?: AnyDuration | undefined;
35
42
  };
36
43
  overrides: PartialWithUndefined<{
37
44
  localStorage: Pick<Storage, 'setItem' | 'removeItem' | 'getItem'>;
@@ -1,5 +1,5 @@
1
- import { createBlockingInterval, HttpStatus, } from '@augment-vir/common';
2
- import { isPageActive } from 'page-active';
1
+ import { HttpStatus, } from '@augment-vir/common';
2
+ import { listenToActivity } from 'detect-activity';
3
3
  import { CsrfTokenFailureReason, extractCsrfTokenHeader, getCurrentCsrfToken, storeCsrfToken, wipeCurrentCsrfToken, } from '../csrf-token.js';
4
4
  import { AuthHeaderName } from '../headers.js';
5
5
  /**
@@ -15,18 +15,18 @@ export class FrontendAuthClient {
15
15
  constructor(config = {}) {
16
16
  this.config = config;
17
17
  if (config.checkUser) {
18
- this.userCheckInterval = createBlockingInterval(async () => {
19
- if (!isPageActive()) {
20
- /** Do not refresh the user when the page is inactive. */
21
- return;
22
- }
23
- const response = await config.checkUser?.performCheck();
24
- if (response) {
25
- await this.verifyResponseAuth({
26
- status: response.status,
27
- });
28
- }
29
- }, config.checkUser.interval || { minutes: 1 });
18
+ listenToActivity({
19
+ listener: async () => {
20
+ const response = await config.checkUser?.performCheck();
21
+ if (response) {
22
+ await this.verifyResponseAuth({
23
+ status: response.status,
24
+ });
25
+ }
26
+ },
27
+ debounce: config.checkUser.debounce || { minutes: 1 },
28
+ fireImmediately: false,
29
+ });
30
30
  }
31
31
  }
32
32
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "auth-vir",
3
- "version": "2.3.6",
3
+ "version": "2.3.8",
4
4
  "description": "Auth made easy and secure via JWT cookies, CSRF tokens, and password hashing helpers.",
5
5
  "keywords": [
6
6
  "auth",
@@ -45,10 +45,10 @@
45
45
  "@augment-vir/assert": "^31.47.0",
46
46
  "@augment-vir/common": "^31.47.0",
47
47
  "date-vir": "^8.0.0",
48
+ "detect-activity": "^0.0.1",
48
49
  "hash-wasm": "^4.12.0",
49
50
  "jose": "^6.1.0",
50
51
  "object-shape-tester": "^6.9.3",
51
- "page-active": "^1.0.3",
52
52
  "type-fest": "^5.1.0",
53
53
  "url-vir": "^2.1.6"
54
54
  },
@@ -1,5 +1,5 @@
1
1
  import {
2
- createBlockingInterval,
2
+ type createBlockingInterval,
3
3
  HttpStatus,
4
4
  type JsonCompatibleObject,
5
5
  type MaybePromise,
@@ -7,7 +7,7 @@ import {
7
7
  type SelectFrom,
8
8
  } from '@augment-vir/common';
9
9
  import {type AnyDuration} from 'date-vir';
10
- import {isPageActive} from 'page-active';
10
+ import {listenToActivity} from 'detect-activity';
11
11
  import {type EmptyObject} from 'type-fest';
12
12
  import {
13
13
  CsrfTokenFailureReason,
@@ -44,6 +44,9 @@ export type FrontendAuthClientConfig = PartialWithUndefined<{
44
44
  *
45
45
  * If the user is not currently authorized, this should return `undefined` to prevent
46
46
  * unnecessary network traffic.
47
+ *
48
+ * This will be called any time the user interacts with the page, debounced by the adjacent
49
+ * `debounce` property.
47
50
  */
48
51
  performCheck: () => MaybePromise<
49
52
  | SelectFrom<
@@ -54,8 +57,12 @@ export type FrontendAuthClientConfig = PartialWithUndefined<{
54
57
  >
55
58
  | undefined
56
59
  >;
57
- /** @default {minutes: 1} */
58
- interval?: AnyDuration | undefined;
60
+ /**
61
+ * Debounce for firing `performCheck`.
62
+ *
63
+ * @default {minutes: 1}
64
+ */
65
+ debounce?: AnyDuration | undefined;
59
66
  };
60
67
 
61
68
  overrides: PartialWithUndefined<{
@@ -77,12 +84,8 @@ export class FrontendAuthClient<AssumedUserParams extends JsonCompatibleObject =
77
84
 
78
85
  constructor(protected readonly config: FrontendAuthClientConfig = {}) {
79
86
  if (config.checkUser) {
80
- this.userCheckInterval = createBlockingInterval(
81
- async () => {
82
- if (!isPageActive()) {
83
- /** Do not refresh the user when the page is inactive. */
84
- return;
85
- }
87
+ listenToActivity({
88
+ listener: async () => {
86
89
  const response = await config.checkUser?.performCheck();
87
90
 
88
91
  if (response) {
@@ -91,8 +94,9 @@ export class FrontendAuthClient<AssumedUserParams extends JsonCompatibleObject =
91
94
  });
92
95
  }
93
96
  },
94
- config.checkUser.interval || {minutes: 1},
95
- );
97
+ debounce: config.checkUser.debounce || {minutes: 1},
98
+ fireImmediately: false,
99
+ });
96
100
  }
97
101
  }
98
102