astro-tokenkit 1.0.22 → 1.0.23
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/README.md +2 -1
- package/dist/client/idle-manager.js +14 -1
- package/dist/client/tk-client.js +4 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +6 -3
- package/dist/index.js.map +1 -1
- package/dist/middleware.cjs.map +1 -1
- package/dist/middleware.js.map +1 -1
- package/dist/types.d.ts +6 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -143,11 +143,12 @@ const specializedClient = createClient({
|
|
|
143
143
|
|
|
144
144
|
Astro TokenKit automatically monitors user inactivity and closes the session across all open tabs. This feature uses `BroadcastChannel` to synchronize activity and logout events.
|
|
145
145
|
|
|
146
|
-
**Important:** When using the Astro integration, the `onIdle` function cannot be passed in `astro.config.mjs` because it is not serializable. Instead, listen for the `tk:idle` event on the client.
|
|
146
|
+
**Important:** When using the Astro integration, the `onIdle` function cannot be passed in `astro.config.mjs` because it is not serializable. Instead, you can pass the name of a global function as a string, or listen for the `tk:idle` event on the client.
|
|
147
147
|
|
|
148
148
|
| Property | Type | Description |
|
|
149
149
|
| :--- | :--- | :--- |
|
|
150
150
|
| `timeout` | `number` | **Required.** Inactivity timeout in seconds. |
|
|
151
|
+
| `onIdle` | `Function \| string` | Optional callback when idle timeout is reached. Can be a function or the name of a global function (string). |
|
|
151
152
|
| `autoLogout`| `boolean` | Whether to automatically trigger logout by calling the configured logout endpoint (default: `true`). |
|
|
152
153
|
| `reload` | `boolean` | Whether to reload the page after automatic logout (default: `true`). |
|
|
153
154
|
| `activeTabOnly` | `boolean` | Whether to track activity only on the active tab to save CPU/memory (default: `true`). |
|
|
@@ -13,11 +13,24 @@ export class IdleManager {
|
|
|
13
13
|
this.lastActivity = 0;
|
|
14
14
|
this.config = config;
|
|
15
15
|
this.timeout = config.timeout;
|
|
16
|
-
this.onIdle = config.onIdle || (() => { });
|
|
17
16
|
this.activeTabOnly = (_a = config.activeTabOnly) !== null && _a !== void 0 ? _a : true;
|
|
18
17
|
this.expiredTimeKey = '_tk_idle_expires';
|
|
19
18
|
this.eventHandler = this.reportActivity.bind(this);
|
|
20
19
|
this.isIdle = false;
|
|
20
|
+
const onIdleProp = config.onIdle;
|
|
21
|
+
if (typeof onIdleProp === 'function') {
|
|
22
|
+
this.onIdle = onIdleProp;
|
|
23
|
+
}
|
|
24
|
+
else if (typeof onIdleProp === 'string') {
|
|
25
|
+
this.onIdle = () => {
|
|
26
|
+
if (typeof window !== 'undefined' && typeof window[onIdleProp] === 'function') {
|
|
27
|
+
window[onIdleProp]();
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
this.onIdle = () => { };
|
|
33
|
+
}
|
|
21
34
|
if (typeof window === 'undefined')
|
|
22
35
|
return;
|
|
23
36
|
try {
|
package/dist/client/tk-client.js
CHANGED
|
@@ -3,22 +3,22 @@ if (typeof window !== 'undefined') {
|
|
|
3
3
|
const config = typeof __TOKENKIT_CONFIG__ !== 'undefined' ? __TOKENKIT_CONFIG__ : {};
|
|
4
4
|
// Initialize Idle Monitoring if configured
|
|
5
5
|
if (config.idle && config.idle.timeout > 0) {
|
|
6
|
-
new IdleManager(Object.assign(Object.assign({}, config.idle), { onIdle: () => {
|
|
6
|
+
new IdleManager(Object.assign(Object.assign({}, config.idle), { onIdle: config.idle.onIdle || (() => {
|
|
7
7
|
var _a;
|
|
8
|
+
// Default implementation: auto logout and reload
|
|
8
9
|
// Note: IdleManager dispatches 'tk:idle' automatically
|
|
9
10
|
if (config.idle.autoLogout !== false && ((_a = config.auth) === null || _a === void 0 ? void 0 : _a.logout)) {
|
|
10
11
|
const logoutURL = config.auth.logout.startsWith('http')
|
|
11
12
|
? config.auth.logout
|
|
12
13
|
: (config.baseURL || '') + config.auth.logout;
|
|
13
14
|
fetch(logoutURL, {
|
|
14
|
-
method: 'POST'
|
|
15
|
-
credentials: 'include'
|
|
15
|
+
method: 'POST'
|
|
16
16
|
}).finally(() => {
|
|
17
17
|
if (config.idle.reload !== false) {
|
|
18
18
|
window.location.reload();
|
|
19
19
|
}
|
|
20
20
|
});
|
|
21
21
|
}
|
|
22
|
-
} }));
|
|
22
|
+
}) }));
|
|
23
23
|
}
|
|
24
24
|
}
|