astro-tokenkit 1.0.21 → 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 CHANGED
@@ -143,12 +143,14 @@ 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`). |
153
+ | `reload` | `boolean` | Whether to reload the page after automatic logout (default: `true`). |
152
154
  | `activeTabOnly` | `boolean` | Whether to track activity only on the active tab to save CPU/memory (default: `true`). |
153
155
  | `alert` | `any` | Custom data to be passed to the `tk:idle` event. Ideal for configuring SweetAlert options. |
154
156
 
@@ -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 {
@@ -3,20 +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
- window.location.reload();
17
+ if (config.idle.reload !== false) {
18
+ window.location.reload();
19
+ }
18
20
  });
19
21
  }
20
- } }));
22
+ }) }));
21
23
  }
22
24
  }