auth-vir 2.3.5 → 2.3.6

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.
@@ -99,7 +99,7 @@ export type BackendAuthClientConfig<DatabaseUser extends AnyObject, UserId exten
99
99
  * How long before a user's session times out when we should start trying to refresh their
100
100
  * session.
101
101
  *
102
- * @default {minutes: 5}
102
+ * @default {minutes: 10}
103
103
  */
104
104
  sessionRefreshThreshold: Readonly<AnyDuration>;
105
105
  overrides: PartialWithUndefined<{
@@ -7,6 +7,9 @@ import { parseJwtKeys } from '../jwt/jwt-keys.js';
7
7
  const defaultSessionIdleTimeout = {
8
8
  minutes: 20,
9
9
  };
10
+ const defaultSessionRefreshThreshold = {
11
+ minutes: 10,
12
+ };
10
13
  /**
11
14
  * An auth client for creating and validating JWTs embedded in cookies. This should only be used in
12
15
  * a backend environment as it accesses native Node packages.
@@ -72,9 +75,7 @@ export class BackendAuthClient {
72
75
  * - Z = JWT expiration outside the refresh threshold: {@link isRefreshReady} = false.
73
76
  */
74
77
  const isRefreshReady = isDateAfter({
75
- fullDate: calculateRelativeDate(now, this.config.sessionRefreshThreshold || {
76
- minutes: 5,
77
- }),
78
+ fullDate: calculateRelativeDate(now, this.config.sessionRefreshThreshold || defaultSessionRefreshThreshold),
78
79
  relativeTo: userIdResult.jwtExpiration,
79
80
  });
80
81
  if (isRefreshReady) {
@@ -1,4 +1,5 @@
1
1
  import { createBlockingInterval, HttpStatus, } from '@augment-vir/common';
2
+ import { isPageActive } from 'page-active';
2
3
  import { CsrfTokenFailureReason, extractCsrfTokenHeader, getCurrentCsrfToken, storeCsrfToken, wipeCurrentCsrfToken, } from '../csrf-token.js';
3
4
  import { AuthHeaderName } from '../headers.js';
4
5
  /**
@@ -15,6 +16,10 @@ export class FrontendAuthClient {
15
16
  this.config = config;
16
17
  if (config.checkUser) {
17
18
  this.userCheckInterval = createBlockingInterval(async () => {
19
+ if (!isPageActive()) {
20
+ /** Do not refresh the user when the page is inactive. */
21
+ return;
22
+ }
18
23
  const response = await config.checkUser?.performCheck();
19
24
  if (response) {
20
25
  await this.verifyResponseAuth({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "auth-vir",
3
- "version": "2.3.5",
3
+ "version": "2.3.6",
4
4
  "description": "Auth made easy and secure via JWT cookies, CSRF tokens, and password hashing helpers.",
5
5
  "keywords": [
6
6
  "auth",
@@ -48,6 +48,7 @@
48
48
  "hash-wasm": "^4.12.0",
49
49
  "jose": "^6.1.0",
50
50
  "object-shape-tester": "^6.9.3",
51
+ "page-active": "^1.0.3",
51
52
  "type-fest": "^5.1.0",
52
53
  "url-vir": "^2.1.6"
53
54
  },
@@ -123,7 +123,7 @@ export type BackendAuthClientConfig<
123
123
  * How long before a user's session times out when we should start trying to refresh their
124
124
  * session.
125
125
  *
126
- * @default {minutes: 5}
126
+ * @default {minutes: 10}
127
127
  */
128
128
  sessionRefreshThreshold: Readonly<AnyDuration>;
129
129
  overrides: PartialWithUndefined<{
@@ -137,6 +137,10 @@ const defaultSessionIdleTimeout: Readonly<AnyDuration> = {
137
137
  minutes: 20,
138
138
  };
139
139
 
140
+ const defaultSessionRefreshThreshold: Readonly<AnyDuration> = {
141
+ minutes: 10,
142
+ };
143
+
140
144
  /**
141
145
  * An auth client for creating and validating JWTs embedded in cookies. This should only be used in
142
146
  * a backend environment as it accesses native Node packages.
@@ -245,9 +249,7 @@ export class BackendAuthClient<
245
249
  const isRefreshReady = isDateAfter({
246
250
  fullDate: calculateRelativeDate(
247
251
  now,
248
- this.config.sessionRefreshThreshold || {
249
- minutes: 5,
250
- },
252
+ this.config.sessionRefreshThreshold || defaultSessionRefreshThreshold,
251
253
  ),
252
254
  relativeTo: userIdResult.jwtExpiration,
253
255
  });
@@ -7,6 +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
11
  import {type EmptyObject} from 'type-fest';
11
12
  import {
12
13
  CsrfTokenFailureReason,
@@ -78,6 +79,10 @@ export class FrontendAuthClient<AssumedUserParams extends JsonCompatibleObject =
78
79
  if (config.checkUser) {
79
80
  this.userCheckInterval = createBlockingInterval(
80
81
  async () => {
82
+ if (!isPageActive()) {
83
+ /** Do not refresh the user when the page is inactive. */
84
+ return;
85
+ }
81
86
  const response = await config.checkUser?.performCheck();
82
87
 
83
88
  if (response) {