@softr/sdk 1.3.0-alpha.0 → 1.3.1

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.
Files changed (43) hide show
  1. package/CHANGELOG.md +13 -1
  2. package/dist/auth/isomorphic.d.ts +11 -4
  3. package/dist/auth/isomorphic.js +5 -2
  4. package/dist/auth/methods/sso.d.ts +19 -0
  5. package/dist/auth/methods/sso.js +28 -0
  6. package/dist/auth/platform.js +46 -2
  7. package/dist/auth/returnPath.js +17 -15
  8. package/dist/auth/server.d.ts +2 -0
  9. package/dist/auth/server.js +2 -0
  10. package/dist/auth/session.js +45 -2
  11. package/dist/auth/sso.d.ts +24 -0
  12. package/dist/auth/sso.js +204 -0
  13. package/dist/cookies.js +2 -2
  14. package/dist/fetch.js +13 -2
  15. package/dist/formatting.d.ts +3 -3
  16. package/dist/index.d.ts +2 -2
  17. package/dist/index.js +2 -2
  18. package/dist/mcp.d.ts +2 -2
  19. package/dist/package.js +1 -1
  20. package/dist/pwa.d.ts +5 -4
  21. package/dist/pwa.js +54 -9
  22. package/dist/softr-config.schema.js +2 -1
  23. package/package.json +10 -34
  24. package/schema/softr.schema.json +10 -1
  25. package/skills/softr-sdk/SKILL.md +2 -2
  26. package/skills/softr-sdk/ai/SKILL.md +1 -1
  27. package/skills/softr-sdk/auth/SKILL.md +1 -1
  28. package/skills/softr-sdk/auth/references/single-sign-on.md +50 -0
  29. package/skills/softr-sdk/charts-and-metrics/SKILL.md +1 -1
  30. package/skills/softr-sdk/current-user/SKILL.md +1 -1
  31. package/skills/softr-sdk/file-uploads/SKILL.md +1 -1
  32. package/skills/softr-sdk/formatting/SKILL.md +1 -1
  33. package/skills/softr-sdk/integrations/SKILL.md +1 -1
  34. package/skills/softr-sdk/mcp/SKILL.md +1 -1
  35. package/skills/softr-sdk/push-notifications/SKILL.md +1 -1
  36. package/skills/softr-sdk/pwa/SKILL.md +7 -7
  37. package/skills/softr-sdk/record-mutations/SKILL.md +1 -1
  38. package/skills/softr-sdk/record-queries/SKILL.md +1 -1
  39. package/skills/softr-sdk/region-visibility/SKILL.md +1 -1
  40. package/skills/softr-sdk/routes-and-visibility/SKILL.md +1 -1
  41. package/skills/softr-sdk/workflows/SKILL.md +1 -1
  42. package/dist/auth/idle-timeout.d.ts +0 -17
  43. package/dist/auth/idle-timeout.js +0 -200
@@ -1,200 +0,0 @@
1
- import { clearSession } from "./session.js";
2
- import { readSoftrContext } from "./context.js";
3
- import { settings } from "./methods/settings.js";
4
- import { useCurrentUser } from "./isomorphic.js";
5
- import { useEffect } from "react";
6
- import { createServerFn } from "@tanstack/react-start";
7
-
8
- //#region src/auth/idle-timeout.ts
9
- /**
10
- * The "End session if idle" setting from Studio, in milliseconds. `null` when it
11
- * does not apply: nobody is signed in, it is set to never, or this is not a
12
- * production build. Preview runs on a dev server and is the owner's own session.
13
- */
14
- const readIdleTimeout = createServerFn({ method: "GET" }).handler(async () => {
15
- if (process.env.NODE_ENV !== "production") return null;
16
- if (readSoftrContext().user === null) return null;
17
- const minutes = (await settings()).inactivityTimeoutMinutes;
18
- return minutes === null ? null : minutes * 6e4;
19
- });
20
- /**
21
- * Signs this browser out and nothing else. Unlike `auth.signOut()`, the user's
22
- * other devices stay signed in.
23
- */
24
- const endIdleSession = createServerFn({ method: "POST" }).handler(() => {
25
- clearSession();
26
- });
27
- /**
28
- * Signs the visitor out of this browser after the idle time set in Studio's
29
- * "End session if idle", and sends them to `/`. Activity in any tab of the app
30
- * keeps the session alive. Does nothing for anonymous visitors.
31
- *
32
- * Call it once, in the root route's document component.
33
- *
34
- * @example
35
- * function RootDocument({ children }: { children: React.ReactNode }) {
36
- * useIdleTimeout()
37
- * // ...
38
- * }
39
- */
40
- function useIdleTimeout() {
41
- const signedIn = useCurrentUser() !== null;
42
- useEffect(() => {
43
- if (!signedIn) return;
44
- let stop = null;
45
- let unmounted = false;
46
- readIdleTimeout().then((idleMs) => {
47
- if (unmounted || idleMs === null) return;
48
- stop = startIdleClock(idleMs);
49
- }, () => {});
50
- return () => {
51
- unmounted = true;
52
- stop?.();
53
- };
54
- }, [signedIn]);
55
- }
56
- const KEY_LAST_ACTIVITY = "softr.idle.v1.lastActivity";
57
- const KEY_SIGNED_OUT = "softr.idle.v1.signedOutAt";
58
- /** Shared clock writes at most this often, so a mousemove is not a storage write in every tab. */
59
- const WRITE_EVERY_MS = 5e3;
60
- /**
61
- * Activity events, listened for on the document in the capture phase. `scroll`
62
- * does not bubble, and capture still sees events a handler stops.
63
- */
64
- const ACTIVITY_EVENTS = [
65
- "pointerdown",
66
- "pointermove",
67
- "mousedown",
68
- "mousemove",
69
- "wheel",
70
- "keydown",
71
- "touchstart",
72
- "touchmove",
73
- "scroll",
74
- "click",
75
- "input",
76
- "focusin",
77
- "submit"
78
- ];
79
- /**
80
- * Starts the idle clock and returns what stops it.
81
- *
82
- * Idle time is checked against the wall clock on a tick, not with one long
83
- * timeout. Browsers delay timers in background tabs, and a laptop that wakes
84
- * from sleep resumes a pending timeout with its full delay left.
85
- */
86
- function startIdleClock(idleMs) {
87
- const tickMs = Math.min(15e3, Math.max(2e3, Math.floor(idleMs / 30)));
88
- const graceCapMs = idleMs;
89
- let localLast = Date.now();
90
- let lastWrite = 0;
91
- let graceUsed = 0;
92
- let ending = false;
93
- let finished = false;
94
- const listeners = new AbortController();
95
- function lastActivity() {
96
- const shared = Number(readStorage(KEY_LAST_ACTIVITY));
97
- return Number.isFinite(shared) && shared > localLast ? shared : localLast;
98
- }
99
- function mark(now) {
100
- if (now > localLast) localLast = now;
101
- if (now - lastWrite >= WRITE_EVERY_MS) {
102
- lastWrite = now;
103
- writeStorage(KEY_LAST_ACTIVITY, String(now));
104
- }
105
- }
106
- function onActivity() {
107
- graceUsed = 0;
108
- mark(Date.now());
109
- }
110
- function stop() {
111
- finished = true;
112
- clearInterval(ticker);
113
- listeners.abort();
114
- }
115
- function leave() {
116
- if (finished) return;
117
- stop();
118
- window.location.replace("/");
119
- }
120
- async function endSession() {
121
- if (ending) return;
122
- ending = true;
123
- try {
124
- await endIdleSession();
125
- } catch {
126
- ending = false;
127
- return;
128
- }
129
- writeStorage(KEY_SIGNED_OUT, String(Date.now()));
130
- leave();
131
- }
132
- function tick() {
133
- if (finished) return;
134
- if (readStorage(KEY_SIGNED_OUT)) {
135
- leave();
136
- return;
137
- }
138
- const now = Date.now();
139
- if (now - lastActivity() < idleMs) return;
140
- if (iframeHasFocus() && graceUsed < graceCapMs) {
141
- graceUsed += tickMs;
142
- mark(now);
143
- return;
144
- }
145
- endSession();
146
- }
147
- removeStorage(KEY_SIGNED_OUT);
148
- mark(Date.now());
149
- const ticker = setInterval(tick, tickMs);
150
- const { signal } = listeners;
151
- for (const event of ACTIVITY_EVENTS) document.addEventListener(event, onActivity, {
152
- capture: true,
153
- passive: true,
154
- signal
155
- });
156
- window.addEventListener("focus", onActivity, {
157
- capture: true,
158
- signal
159
- });
160
- document.addEventListener("visibilitychange", () => {
161
- if (document.visibilityState !== "visible") return;
162
- tick();
163
- if (!finished && !ending) onActivity();
164
- }, { signal });
165
- window.addEventListener("storage", (event) => {
166
- if (event.key === KEY_SIGNED_OUT && event.newValue) {
167
- leave();
168
- return;
169
- }
170
- if (event.key === KEY_LAST_ACTIVITY && event.newValue) {
171
- const shared = Number(event.newValue);
172
- if (Number.isFinite(shared) && shared > localLast) localLast = shared;
173
- }
174
- }, { signal });
175
- return stop;
176
- }
177
- function iframeHasFocus() {
178
- const active = document.activeElement;
179
- return active !== null && active.tagName === "IFRAME" && document.hasFocus();
180
- }
181
- function readStorage(key) {
182
- try {
183
- return window.localStorage.getItem(key);
184
- } catch {
185
- return null;
186
- }
187
- }
188
- function writeStorage(key, value) {
189
- try {
190
- window.localStorage.setItem(key, value);
191
- } catch {}
192
- }
193
- function removeStorage(key) {
194
- try {
195
- window.localStorage.removeItem(key);
196
- } catch {}
197
- }
198
-
199
- //#endregion
200
- export { useIdleTimeout };