@oauth-spa-kit/core 2.2.0 → 2.3.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.
@@ -36,6 +36,17 @@ export interface SessionClientOptions {
36
36
  * for anything beyond reactive state, e.g. redirecting straight to `login()`.
37
37
  */
38
38
  onSessionExpired?: () => void;
39
+ /**
40
+ * Inactivity window (ms) before an authenticated session is force-logged-out client-side,
41
+ * regardless of whether the server-side session/token is still valid. Neither the liveness
42
+ * poller above nor the navigation-triggered refresh() ever notice a merely idle tab -- both
43
+ * only catch a session the server already killed -- so without this, a tab left open and
44
+ * untouched with an otherwise-valid session stays logged in forever. Reset on `mousemove`,
45
+ * `keydown`, `click`, `scroll`, and `touchstart`. Defaults to 15 minutes; pass `0` to disable.
46
+ */
47
+ idleTimeoutMs?: number;
48
+ /** Called immediately before the idle-triggered logout redirect fires. */
49
+ onIdleTimeout?: () => void;
39
50
  }
40
51
  export interface SessionClient {
41
52
  getState(): SessionState;
@@ -1,6 +1,8 @@
1
1
  const initialState = { ready: false, loggedIn: false, user: null };
2
2
  /** Consecutive liveness-check failures required before treating a session as actually dead. */
3
3
  const LIVENESS_FAILURE_THRESHOLD = 2;
4
+ const DEFAULT_IDLE_TIMEOUT_MS = 15 * 60_000;
5
+ const IDLE_ACTIVITY_EVENTS = ["mousemove", "keydown", "click", "scroll", "touchstart"];
4
6
  export function createSessionClient(options = {}) {
5
7
  const baseUrl = options.baseUrl ?? "";
6
8
  const fetchImpl = options.fetchImpl ?? fetch;
@@ -10,8 +12,26 @@ export function createSessionClient(options = {}) {
10
12
  : null;
11
13
  let state = initialState;
12
14
  const listeners = new Set();
15
+ const idleTimeoutMs = options.idleTimeoutMs ?? DEFAULT_IDLE_TIMEOUT_MS;
16
+ const idleTrackingEnabled = typeof window !== "undefined" && idleTimeoutMs > 0;
17
+ let idleTimer;
18
+ // Restarted on every activity event and on every state change (so a fresh login starts the
19
+ // window immediately rather than waiting for the first mouse move) -- see `idleTimeoutMs` above.
20
+ function resetIdleTimer() {
21
+ if (!idleTrackingEnabled)
22
+ return;
23
+ if (idleTimer)
24
+ clearTimeout(idleTimer);
25
+ idleTimer = state.loggedIn
26
+ ? setTimeout(() => {
27
+ options.onIdleTimeout?.();
28
+ void logout();
29
+ }, idleTimeoutMs)
30
+ : undefined;
31
+ }
13
32
  function setState(next) {
14
33
  state = next;
34
+ resetIdleTimer();
15
35
  for (const listener of listeners)
16
36
  listener(state);
17
37
  }
@@ -71,6 +91,21 @@ export function createSessionClient(options = {}) {
71
91
  })();
72
92
  }, options.livenessCheckIntervalMs);
73
93
  }
94
+ function login(returnTo) {
95
+ const url = new URL(`${baseUrl}/auth/login`, window.location.origin);
96
+ if (returnTo)
97
+ url.searchParams.set("returnTo", returnTo);
98
+ window.location.assign(url.toString());
99
+ }
100
+ async function logout() {
101
+ const url = new URL(`${baseUrl}/auth/logout`, window.location.origin);
102
+ window.location.assign(url.toString());
103
+ }
104
+ if (idleTrackingEnabled) {
105
+ for (const event of IDLE_ACTIVITY_EVENTS) {
106
+ window.addEventListener(event, resetIdleTimer, { passive: true });
107
+ }
108
+ }
74
109
  return {
75
110
  getState: () => state,
76
111
  subscribe(callback) {
@@ -78,15 +113,7 @@ export function createSessionClient(options = {}) {
78
113
  return () => listeners.delete(callback);
79
114
  },
80
115
  refresh,
81
- login(returnTo) {
82
- const url = new URL(`${baseUrl}/auth/login`, window.location.origin);
83
- if (returnTo)
84
- url.searchParams.set("returnTo", returnTo);
85
- window.location.assign(url.toString());
86
- },
87
- async logout() {
88
- const url = new URL(`${baseUrl}/auth/logout`, window.location.origin);
89
- window.location.assign(url.toString());
90
- },
116
+ login,
117
+ logout,
91
118
  };
92
119
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oauth-spa-kit/core",
3
- "version": "2.2.0",
3
+ "version": "2.3.0",
4
4
  "description": "Framework- and runtime-agnostic OAuth 2.1 / OIDC primitives (PKCE, discovery, token exchange, DPoP) plus a fetch-based session client. Runs in the browser, Node, and edge runtimes -- no DOM or Node-only APIs.",
5
5
  "license": "MIT",
6
6
  "repository": {