astro-tokenkit 1.0.22 → 1.0.24

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,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`). |
@@ -181,6 +182,25 @@ tokenKit({
181
182
  </script>
182
183
  ```
183
184
 
185
+ #### Overriding Auto-Logout Behavior
186
+
187
+ By default, TokenKit automatically calls your logout endpoint and reloads the page. You can override this behavior by providing an `onIdle` callback (function or string) in your configuration.
188
+
189
+ ```javascript
190
+ // astro.config.mjs
191
+ export default defineConfig({
192
+ integrations: [
193
+ tokenKit({
194
+ idle: {
195
+ timeout: 60 * 15,
196
+ // Disables default logout/reload and runs this instead
197
+ onIdle: 'myCustomLogout'
198
+ }
199
+ })
200
+ ]
201
+ });
202
+ ```
203
+
184
204
  ### Login Options
185
205
 
186
206
  | Property | Type | Description |
@@ -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,9 +3,9 @@ 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
- // Note: IdleManager dispatches 'tk:idle' automatically
8
+ // Default implementation: auto logout and reload
9
9
  if (config.idle.autoLogout !== false && ((_a = config.auth) === null || _a === void 0 ? void 0 : _a.logout)) {
10
10
  const logoutURL = config.auth.logout.startsWith('http')
11
11
  ? config.auth.logout
@@ -19,6 +19,6 @@ if (typeof window !== 'undefined') {
19
19
  }
20
20
  });
21
21
  }
22
- } }));
22
+ }) }));
23
23
  }
24
24
  }
package/dist/index.cjs CHANGED
@@ -1392,7 +1392,11 @@ function createClient(config) {
1392
1392
  * ```
1393
1393
  */
1394
1394
  function tokenKit(config) {
1395
+ var _a;
1395
1396
  setConfig(config);
1397
+ if (((_a = config.idle) === null || _a === void 0 ? void 0 : _a.onIdle) && typeof config.idle.onIdle === 'function') {
1398
+ logger.warn('[TokenKit] Passing a function to "idle.onIdle" in astro.config.mjs is not supported because it is not serializable. Use a string name of a global function or window.addEventListener("tk:idle", ...) instead.');
1399
+ }
1396
1400
  // Create a serializable version of the config for the runtime
1397
1401
  const serializableConfig = JSON.parse(JSON.stringify(config, (key, value) => {
1398
1402
  if (typeof value === 'function')