@devopsplaybook.io/common-web 0.12.3 → 0.13.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.
- package/CHANGELOG.md +14 -0
- package/README.md +32 -0
- package/package.json +1 -1
- package/services/AuthService.ts +60 -0
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,20 @@ All notable changes to this project are documented here.
|
|
|
5
5
|
This project follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/)
|
|
6
6
|
and [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
+
## [0.13.0] - 2026-11-25
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
|
|
12
|
+
- `AuthService.startSessionRenewal()`: the standard client renewal loop. It
|
|
13
|
+
checks the token immediately, on a visible `visibilitychange`, on `focus` and
|
|
14
|
+
every five minutes, and renews when 20 % or less of the lifetime remains
|
|
15
|
+
(the existing `getToken()` behaviour). It is opt-in, idempotent, SSR-safe and
|
|
16
|
+
returns a stop function that clears the interval and listeners.
|
|
17
|
+
- `SESSION_RENEWAL_CHECK_INTERVAL_MS` (five minutes) and an optional
|
|
18
|
+
`intervalMs` override.
|
|
19
|
+
- Tests for the start, visibility, focus, interval, stop, double-start,
|
|
20
|
+
double-stop and SSR paths; README documentation of the client-plugin snippet.
|
|
21
|
+
|
|
8
22
|
## [0.12.0] - 2026-11-01
|
|
9
23
|
|
|
10
24
|
### Added
|
package/README.md
CHANGED
|
@@ -166,6 +166,38 @@ endpoint never stalls a caller.
|
|
|
166
166
|
compare-and-remove operation: it clears the session only when the stored token
|
|
167
167
|
still equals `token`, so a stale response cannot discard a newer session.
|
|
168
168
|
|
|
169
|
+
### Standard client renewal: `startSessionRenewal()`
|
|
170
|
+
|
|
171
|
+
`AuthService.startSessionRenewal()` is the standard mechanism for every Nuxt
|
|
172
|
+
application. `getToken()` alone only renews when it is called again (for
|
|
173
|
+
example on a data access), so a tab that stays visible and idle can reach
|
|
174
|
+
expiry without ever renewing. The helper closes that gap:
|
|
175
|
+
|
|
176
|
+
```ts
|
|
177
|
+
// plugins/auth-renewal.client.ts
|
|
178
|
+
import { AuthService } from "@devopsplaybook.io/common-web/services/AuthService";
|
|
179
|
+
|
|
180
|
+
export default defineNuxtPlugin(() => {
|
|
181
|
+
AuthService.startSessionRenewal();
|
|
182
|
+
});
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
It checks the token immediately, on `visibilitychange` while the document is
|
|
186
|
+
visible, on `focus`, and every five minutes (`SESSION_RENEWAL_CHECK_INTERVAL_MS`;
|
|
187
|
+
override with `startSessionRenewal({ intervalMs })`). Each check delegates to
|
|
188
|
+
`getToken()`, which renews through `POST /api/users/session/refresh` when 20 %
|
|
189
|
+
or less of the lifetime remains. The threshold must stay below the token
|
|
190
|
+
lifetime, otherwise it can never fire.
|
|
191
|
+
|
|
192
|
+
The helper is opt-in (nothing auto-starts it, and the layer registers no auth
|
|
193
|
+
plugin), idempotent (a second call returns the same stop function), safe to
|
|
194
|
+
call twice for stopping, and a no-op during SSR. The returned function clears
|
|
195
|
+
the interval and both listeners.
|
|
196
|
+
|
|
197
|
+
The session model of each application (validity, absolute cap, renewal
|
|
198
|
+
mechanism) is documented in the
|
|
199
|
+
[common-utils auth session policy](https://github.com/devopsplaybook-io/common-utils/blob/main/docs/auth-session-policy.md).
|
|
200
|
+
|
|
169
201
|
## Authentication state and HTTP integration
|
|
170
202
|
|
|
171
203
|
`AuthService.saveToken()` and `removeToken()` emit
|
package/package.json
CHANGED
package/services/AuthService.ts
CHANGED
|
@@ -9,6 +9,9 @@ import Config from "./Config";
|
|
|
9
9
|
import { Timeout } from "./Timeout";
|
|
10
10
|
|
|
11
11
|
export const AUTH_TOKEN_KEY = "auth_token";
|
|
12
|
+
|
|
13
|
+
/** Default polling cadence of {@link AuthService.startSessionRenewal}. */
|
|
14
|
+
export const SESSION_RENEWAL_CHECK_INTERVAL_MS = 5 * 60 * 1000;
|
|
12
15
|
const LEGACY_TOKEN_KEYS = ["AUTH_TOKEN", "AUTH_TOKEN_KEY"] as const;
|
|
13
16
|
|
|
14
17
|
const RENEWAL_TIMEOUT_MS = 3_000;
|
|
@@ -53,6 +56,63 @@ export class AuthService {
|
|
|
53
56
|
|
|
54
57
|
private static renewalFailure: { token: string; at: number } | null = null;
|
|
55
58
|
|
|
59
|
+
private static sessionRenewalStop: (() => void) | null = null;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Start the standard client session renewal loop.
|
|
63
|
+
*
|
|
64
|
+
* Checks the token immediately, on `visibilitychange` while the document is
|
|
65
|
+
* visible, on `focus`, and every `intervalMs` (default 5 minutes). Each check
|
|
66
|
+
* delegates to {@link getToken}, which renews when 20 % or less of the token
|
|
67
|
+
* lifetime remains, so a continuously visible idle tab still renews before
|
|
68
|
+
* expiry.
|
|
69
|
+
*
|
|
70
|
+
* Opt-in: never started on import or through an auto-registered plugin.
|
|
71
|
+
* Idempotent: a second call returns the existing stop function. Safe during
|
|
72
|
+
* SSR (no-op). Returns a stop function that clears the interval and both
|
|
73
|
+
* listeners; calling it twice is safe.
|
|
74
|
+
*/
|
|
75
|
+
static startSessionRenewal(options?: { intervalMs?: number }): () => void {
|
|
76
|
+
if (typeof window === "undefined" || typeof document === "undefined") {
|
|
77
|
+
return () => {};
|
|
78
|
+
}
|
|
79
|
+
if (AuthService.sessionRenewalStop) {
|
|
80
|
+
return AuthService.sessionRenewalStop;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const requestedInterval = options?.intervalMs;
|
|
84
|
+
const intervalMs =
|
|
85
|
+
typeof requestedInterval === "number" &&
|
|
86
|
+
Number.isFinite(requestedInterval) &&
|
|
87
|
+
requestedInterval > 0
|
|
88
|
+
? requestedInterval
|
|
89
|
+
: SESSION_RENEWAL_CHECK_INTERVAL_MS;
|
|
90
|
+
|
|
91
|
+
const check = (): void => {
|
|
92
|
+
void AuthService.getToken().catch(() => {});
|
|
93
|
+
};
|
|
94
|
+
const onVisibilityChange = (): void => {
|
|
95
|
+
if (document.visibilityState === "visible") check();
|
|
96
|
+
};
|
|
97
|
+
const onFocus = (): void => check();
|
|
98
|
+
|
|
99
|
+
const interval = setInterval(check, intervalMs);
|
|
100
|
+
document.addEventListener("visibilitychange", onVisibilityChange);
|
|
101
|
+
window.addEventListener("focus", onFocus);
|
|
102
|
+
|
|
103
|
+
const stop = (): void => {
|
|
104
|
+
if (AuthService.sessionRenewalStop !== stop) return;
|
|
105
|
+
clearInterval(interval);
|
|
106
|
+
document.removeEventListener("visibilitychange", onVisibilityChange);
|
|
107
|
+
window.removeEventListener("focus", onFocus);
|
|
108
|
+
AuthService.sessionRenewalStop = null;
|
|
109
|
+
};
|
|
110
|
+
AuthService.sessionRenewalStop = stop;
|
|
111
|
+
|
|
112
|
+
check();
|
|
113
|
+
return stop;
|
|
114
|
+
}
|
|
115
|
+
|
|
56
116
|
static async isAuthenticated(): Promise<boolean> {
|
|
57
117
|
return Boolean(await AuthService.getToken());
|
|
58
118
|
}
|