@every-app/sdk 0.0.6 → 0.0.7
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/dist/client/EmbeddedAppProvider.d.ts +5 -8
- package/dist/client/EmbeddedAppProvider.d.ts.map +1 -1
- package/dist/client/EmbeddedAppProvider.js +28 -27
- package/dist/client/GatewayRequiredError.d.ts +20 -0
- package/dist/client/GatewayRequiredError.d.ts.map +1 -0
- package/dist/client/GatewayRequiredError.js +97 -0
- package/dist/client/_internal/useEveryAppRouter.d.ts +3 -5
- package/dist/client/_internal/useEveryAppRouter.js +53 -56
- package/dist/client/_internal/useEveryAppSession.d.ts +8 -10
- package/dist/client/_internal/useEveryAppSession.d.ts.map +1 -1
- package/dist/client/_internal/useEveryAppSession.js +32 -27
- package/dist/client/authenticatedFetch.d.ts +2 -5
- package/dist/client/authenticatedFetch.js +17 -17
- package/dist/client/index.d.ts +3 -2
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +2 -1
- package/dist/client/lazyInitForWorkers.d.ts +2 -4
- package/dist/client/lazyInitForWorkers.js +42 -45
- package/dist/client/session-manager.d.ts +29 -23
- package/dist/client/session-manager.d.ts.map +1 -1
- package/dist/client/session-manager.js +143 -126
- package/dist/client/useSessionTokenClientMiddleware.d.ts +2 -8
- package/dist/client/useSessionTokenClientMiddleware.js +17 -20
- package/dist/server/auth-config.d.ts +1 -1
- package/dist/server/auth-config.js +4 -4
- package/dist/server/authenticateRequest.d.ts +11 -16
- package/dist/server/authenticateRequest.js +53 -57
- package/dist/server/getLocalD1Url.d.ts +1 -1
- package/dist/server/getLocalD1Url.js +29 -42
- package/dist/server/index.d.ts +1 -1
- package/dist/server/types.d.ts +3 -3
- package/package.json +2 -2
- package/src/client/EmbeddedAppProvider.tsx +11 -0
- package/src/client/GatewayRequiredError.tsx +161 -0
- package/src/client/_internal/useEveryAppSession.tsx +3 -0
- package/src/client/index.ts +2 -1
- package/src/client/session-manager.ts +16 -0
|
@@ -1,12 +1,9 @@
|
|
|
1
1
|
import React from "react";
|
|
2
2
|
import { SessionManagerConfig } from "./session-manager";
|
|
3
3
|
interface EmbeddedProviderConfig extends SessionManagerConfig {
|
|
4
|
-
|
|
4
|
+
children: React.ReactNode;
|
|
5
5
|
}
|
|
6
|
-
export declare function EmbeddedAppProvider({
|
|
7
|
-
children,
|
|
8
|
-
...config
|
|
9
|
-
}: EmbeddedProviderConfig): import("react/jsx-runtime").JSX.Element | null;
|
|
6
|
+
export declare function EmbeddedAppProvider({ children, ...config }: EmbeddedProviderConfig): import("react/jsx-runtime").JSX.Element | null;
|
|
10
7
|
/**
|
|
11
8
|
* Hook to get the current authenticated user.
|
|
12
9
|
* Returns the user's ID and email extracted from the JWT token,
|
|
@@ -26,8 +23,8 @@ export declare function EmbeddedAppProvider({
|
|
|
26
23
|
* ```
|
|
27
24
|
*/
|
|
28
25
|
export declare function useCurrentUser(): {
|
|
29
|
-
|
|
30
|
-
|
|
26
|
+
userId: string;
|
|
27
|
+
email: string;
|
|
31
28
|
} | null;
|
|
32
29
|
export {};
|
|
33
|
-
//# sourceMappingURL=EmbeddedAppProvider.d.ts.map
|
|
30
|
+
//# sourceMappingURL=EmbeddedAppProvider.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"EmbeddedAppProvider.d.ts","sourceRoot":"","sources":["../../src/client/EmbeddedAppProvider.tsx"],"names":[],"mappings":"AAAA,OAAO,KAA6C,MAAM,OAAO,CAAC;AAClE,OAAO,EAAkB,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;
|
|
1
|
+
{"version":3,"file":"EmbeddedAppProvider.d.ts","sourceRoot":"","sources":["../../src/client/EmbeddedAppProvider.tsx"],"names":[],"mappings":"AAAA,OAAO,KAA6C,MAAM,OAAO,CAAC;AAClE,OAAO,EAAkB,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AAKzE,UAAU,sBAAuB,SAAQ,oBAAoB;IAC3D,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;CAC3B;AAUD,wBAAgB,mBAAmB,CAAC,EAClC,QAAQ,EACR,GAAG,MAAM,EACV,EAAE,sBAAsB,kDA6BxB;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,cAAc,IAAI;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GAAG,IAAI,CAmBzE"}
|
|
@@ -2,22 +2,25 @@ import { jsx as _jsx } from "react/jsx-runtime";
|
|
|
2
2
|
import { createContext, useContext, useMemo } from "react";
|
|
3
3
|
import { useEveryAppSession } from "./_internal/useEveryAppSession";
|
|
4
4
|
import { useEveryAppRouter } from "./_internal/useEveryAppRouter";
|
|
5
|
+
import { GatewayRequiredError } from "./GatewayRequiredError";
|
|
5
6
|
const EmbeddedAppContext = createContext(null);
|
|
6
7
|
export function EmbeddedAppProvider({ children, ...config }) {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
8
|
+
const { sessionManager, sessionTokenState } = useEveryAppSession({
|
|
9
|
+
sessionManagerConfig: config,
|
|
10
|
+
});
|
|
11
|
+
useEveryAppRouter({ sessionManager });
|
|
12
|
+
if (!sessionManager)
|
|
13
|
+
return null;
|
|
14
|
+
// Check if the app is running outside of the Gateway iframe
|
|
15
|
+
if (!sessionManager.isInIframe) {
|
|
16
|
+
return (_jsx(GatewayRequiredError, { gatewayOrigin: sessionManager.parentOrigin, appId: config.appId }));
|
|
17
|
+
}
|
|
18
|
+
const value = {
|
|
19
|
+
sessionManager,
|
|
20
|
+
isAuthenticated: sessionTokenState.status === "VALID",
|
|
21
|
+
sessionTokenState,
|
|
22
|
+
};
|
|
23
|
+
return (_jsx(EmbeddedAppContext.Provider, { value: value, children: children }));
|
|
21
24
|
}
|
|
22
25
|
/**
|
|
23
26
|
* Hook to get the current authenticated user.
|
|
@@ -38,18 +41,16 @@ export function EmbeddedAppProvider({ children, ...config }) {
|
|
|
38
41
|
* ```
|
|
39
42
|
*/
|
|
40
43
|
export function useCurrentUser() {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
"useCurrentUser must be used within an EmbeddedAppProvider",
|
|
45
|
-
);
|
|
46
|
-
}
|
|
47
|
-
const { sessionManager, sessionTokenState } = context;
|
|
48
|
-
return useMemo(() => {
|
|
49
|
-
// Only return user if we have a valid token
|
|
50
|
-
if (sessionTokenState.status !== "VALID") {
|
|
51
|
-
return null;
|
|
44
|
+
const context = useContext(EmbeddedAppContext);
|
|
45
|
+
if (!context) {
|
|
46
|
+
throw new Error("useCurrentUser must be used within an EmbeddedAppProvider");
|
|
52
47
|
}
|
|
53
|
-
|
|
54
|
-
|
|
48
|
+
const { sessionManager, sessionTokenState } = context;
|
|
49
|
+
return useMemo(() => {
|
|
50
|
+
// Only return user if we have a valid token
|
|
51
|
+
if (sessionTokenState.status !== "VALID") {
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
return sessionManager.getUser();
|
|
55
|
+
}, [sessionManager, sessionTokenState]);
|
|
55
56
|
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
interface GatewayRequiredErrorProps {
|
|
2
|
+
/**
|
|
3
|
+
* The origin of the Gateway (e.g., "https://gateway.example.com").
|
|
4
|
+
*/
|
|
5
|
+
gatewayOrigin: string;
|
|
6
|
+
/**
|
|
7
|
+
* The app ID used in the Gateway URL path.
|
|
8
|
+
*/
|
|
9
|
+
appId: string;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Error component displayed when an embedded app is accessed directly
|
|
13
|
+
* instead of through the Every App Gateway.
|
|
14
|
+
*
|
|
15
|
+
* This component informs users that authentication requires accessing
|
|
16
|
+
* the app through the Gateway and provides a link to do so.
|
|
17
|
+
*/
|
|
18
|
+
export declare function GatewayRequiredError({ gatewayOrigin, appId, }: GatewayRequiredErrorProps): import("react/jsx-runtime").JSX.Element;
|
|
19
|
+
export {};
|
|
20
|
+
//# sourceMappingURL=GatewayRequiredError.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"GatewayRequiredError.d.ts","sourceRoot":"","sources":["../../src/client/GatewayRequiredError.tsx"],"names":[],"mappings":"AAEA,UAAU,yBAAyB;IACjC;;OAEG;IACH,aAAa,EAAE,MAAM,CAAC;IACtB;;OAEG;IACH,KAAK,EAAE,MAAM,CAAC;CACf;AAwFD;;;;;;GAMG;AACH,wBAAgB,oBAAoB,CAAC,EACnC,aAAa,EACb,KAAK,GACN,EAAE,yBAAyB,2CAmD3B"}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
// Button gradient styles
|
|
3
|
+
const BUTTON_GRADIENT = "linear-gradient(180deg, #424242 0%, #353535 50%, #2a2a2a 100%)";
|
|
4
|
+
const BUTTON_GRADIENT_HOVER = "linear-gradient(180deg, #4d4d4d 0%, #404040 50%, #353535 100%)";
|
|
5
|
+
// CSS custom properties for theming
|
|
6
|
+
const CSS_VARIABLES = `
|
|
7
|
+
@media (prefers-color-scheme: light) {
|
|
8
|
+
:root {
|
|
9
|
+
--gateway-bg: oklch(100% 0 0);
|
|
10
|
+
--gateway-text: oklch(0% 0 0);
|
|
11
|
+
--gateway-text-muted: oklch(40% 0 0);
|
|
12
|
+
--gateway-icon-bg: oklch(94% 0 0);
|
|
13
|
+
--gateway-icon-stroke: oklch(55% 0.22 25);
|
|
14
|
+
--gateway-border: oklch(94% 0 0);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
@media (prefers-color-scheme: dark) {
|
|
18
|
+
:root {
|
|
19
|
+
--gateway-bg: #0a0f0d;
|
|
20
|
+
--gateway-text: oklch(92% 0 0);
|
|
21
|
+
--gateway-text-muted: oklch(60% 0 0);
|
|
22
|
+
--gateway-icon-bg: oklch(22% 0 0);
|
|
23
|
+
--gateway-icon-stroke: oklch(65% 0.2 25);
|
|
24
|
+
--gateway-border: oklch(30% 0 0);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
`;
|
|
28
|
+
const styles = {
|
|
29
|
+
container: {
|
|
30
|
+
display: "flex",
|
|
31
|
+
flexDirection: "column",
|
|
32
|
+
alignItems: "center",
|
|
33
|
+
justifyContent: "center",
|
|
34
|
+
minHeight: "100vh",
|
|
35
|
+
padding: "24px",
|
|
36
|
+
fontFamily: '-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Oxygen", "Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue", sans-serif',
|
|
37
|
+
backgroundColor: "var(--gateway-bg, oklch(100% 0 0))",
|
|
38
|
+
color: "var(--gateway-text, oklch(0% 0 0))",
|
|
39
|
+
colorScheme: "light dark",
|
|
40
|
+
},
|
|
41
|
+
content: {
|
|
42
|
+
maxWidth: "380px",
|
|
43
|
+
width: "100%",
|
|
44
|
+
textAlign: "left",
|
|
45
|
+
},
|
|
46
|
+
iconContainer: {
|
|
47
|
+
width: "44px",
|
|
48
|
+
height: "44px",
|
|
49
|
+
marginBottom: "16px",
|
|
50
|
+
borderRadius: "0.25rem",
|
|
51
|
+
backgroundColor: "var(--gateway-icon-bg, oklch(94% 0 0))",
|
|
52
|
+
display: "flex",
|
|
53
|
+
alignItems: "center",
|
|
54
|
+
justifyContent: "center",
|
|
55
|
+
border: "1px solid var(--gateway-border, oklch(94% 0 0))",
|
|
56
|
+
},
|
|
57
|
+
title: {
|
|
58
|
+
fontSize: "18px",
|
|
59
|
+
fontWeight: 600,
|
|
60
|
+
marginBottom: "8px",
|
|
61
|
+
color: "var(--gateway-text, oklch(0% 0 0))",
|
|
62
|
+
letterSpacing: "-0.01em",
|
|
63
|
+
},
|
|
64
|
+
description: {
|
|
65
|
+
fontSize: "14px",
|
|
66
|
+
lineHeight: 1.5,
|
|
67
|
+
color: "var(--gateway-text-muted, oklch(40% 0 0))",
|
|
68
|
+
marginBottom: "20px",
|
|
69
|
+
},
|
|
70
|
+
button: {
|
|
71
|
+
display: "inline-block",
|
|
72
|
+
padding: "10px 20px",
|
|
73
|
+
fontSize: "14px",
|
|
74
|
+
fontWeight: 500,
|
|
75
|
+
color: "#ffffff",
|
|
76
|
+
background: BUTTON_GRADIENT,
|
|
77
|
+
borderRadius: "0.25rem",
|
|
78
|
+
textDecoration: "none",
|
|
79
|
+
border: "none",
|
|
80
|
+
},
|
|
81
|
+
};
|
|
82
|
+
/**
|
|
83
|
+
* Error component displayed when an embedded app is accessed directly
|
|
84
|
+
* instead of through the Every App Gateway.
|
|
85
|
+
*
|
|
86
|
+
* This component informs users that authentication requires accessing
|
|
87
|
+
* the app through the Gateway and provides a link to do so.
|
|
88
|
+
*/
|
|
89
|
+
export function GatewayRequiredError({ gatewayOrigin, appId, }) {
|
|
90
|
+
const displayName = appId || "This app";
|
|
91
|
+
const gatewayUrl = `${gatewayOrigin}/apps/${appId}${window.location.pathname}`;
|
|
92
|
+
return (_jsxs("div", { style: styles.container, children: [_jsx("style", { children: CSS_VARIABLES }), _jsxs("div", { style: styles.content, children: [_jsx("div", { style: styles.iconContainer, children: _jsxs("svg", { width: "22", height: "22", viewBox: "0 0 24 24", fill: "none", stroke: "var(--gateway-icon-stroke, oklch(55% 0.22 25))", strokeWidth: "2", strokeLinecap: "round", strokeLinejoin: "round", children: [_jsx("path", { d: "M10.29 3.86L1.82 18a2 2 0 0 0 1.71 3h16.94a2 2 0 0 0 1.71-3L13.71 3.86a2 2 0 0 0-3.42 0z" }), _jsx("line", { x1: "12", y1: "9", x2: "12", y2: "13" }), _jsx("line", { x1: "12", y1: "17", x2: "12.01", y2: "17" })] }) }), _jsx("h1", { style: styles.title, children: "Gateway Required" }), _jsxs("p", { style: styles.description, children: [displayName, " needs to be accessed through the Gateway for authentication to work properly."] }), _jsx("a", { href: gatewayUrl, style: styles.button, onMouseOver: (e) => {
|
|
93
|
+
e.currentTarget.style.background = BUTTON_GRADIENT_HOVER;
|
|
94
|
+
}, onMouseOut: (e) => {
|
|
95
|
+
e.currentTarget.style.background = BUTTON_GRADIENT;
|
|
96
|
+
}, children: "Open in Gateway" })] })] }));
|
|
97
|
+
}
|
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
import { SessionManager } from "../session-manager";
|
|
2
2
|
interface UseEveryAppRouterParams {
|
|
3
|
-
|
|
3
|
+
sessionManager: SessionManager | null;
|
|
4
4
|
}
|
|
5
|
-
export declare function useEveryAppRouter({
|
|
6
|
-
sessionManager,
|
|
7
|
-
}: UseEveryAppRouterParams): void;
|
|
5
|
+
export declare function useEveryAppRouter({ sessionManager }: UseEveryAppRouterParams): void;
|
|
8
6
|
export {};
|
|
9
|
-
//# sourceMappingURL=useEveryAppRouter.d.ts.map
|
|
7
|
+
//# sourceMappingURL=useEveryAppRouter.d.ts.map
|
|
@@ -1,60 +1,57 @@
|
|
|
1
1
|
import { useEffect } from "react";
|
|
2
2
|
import { useRouter } from "@tanstack/react-router";
|
|
3
3
|
export function useEveryAppRouter({ sessionManager }) {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
clearInterval(pollInterval);
|
|
58
|
-
};
|
|
59
|
-
}, [sessionManager]);
|
|
4
|
+
const router = useRouter();
|
|
5
|
+
// Route synchronization effect
|
|
6
|
+
useEffect(() => {
|
|
7
|
+
if (!sessionManager)
|
|
8
|
+
return;
|
|
9
|
+
// Listen for route sync messages from parent
|
|
10
|
+
const handleMessage = (event) => {
|
|
11
|
+
if (event.origin !== sessionManager.parentOrigin)
|
|
12
|
+
return;
|
|
13
|
+
if (event.data.type === "ROUTE_CHANGE" &&
|
|
14
|
+
event.data.direction === "parent-to-child") {
|
|
15
|
+
const targetRoute = event.data.route;
|
|
16
|
+
const currentRoute = window.location.pathname;
|
|
17
|
+
// Only navigate if the route is different from current location
|
|
18
|
+
if (targetRoute && targetRoute !== currentRoute) {
|
|
19
|
+
router.navigate({ to: targetRoute });
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
window.addEventListener("message", handleMessage);
|
|
24
|
+
// Simplified route change detection with 2 reliable methods
|
|
25
|
+
let lastReportedPath = window.location.pathname;
|
|
26
|
+
const handleRouteChange = () => {
|
|
27
|
+
const currentPath = window.location.pathname;
|
|
28
|
+
// Only report if the path actually changed
|
|
29
|
+
if (currentPath === lastReportedPath) {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
lastReportedPath = currentPath;
|
|
33
|
+
if (window.parent !== window) {
|
|
34
|
+
window.parent.postMessage({
|
|
35
|
+
type: "ROUTE_CHANGE",
|
|
36
|
+
route: currentPath,
|
|
37
|
+
appId: sessionManager.appId,
|
|
38
|
+
direction: "child-to-parent",
|
|
39
|
+
}, sessionManager.parentOrigin);
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
// Listen to popstate for browser back/forward
|
|
43
|
+
window.addEventListener("popstate", handleRouteChange);
|
|
44
|
+
// Polling to detect route changes (catches router navigation)
|
|
45
|
+
const pollInterval = setInterval(() => {
|
|
46
|
+
const currentPath = window.location.pathname;
|
|
47
|
+
if (currentPath !== lastReportedPath) {
|
|
48
|
+
handleRouteChange();
|
|
49
|
+
}
|
|
50
|
+
}, 100);
|
|
51
|
+
return () => {
|
|
52
|
+
window.removeEventListener("message", handleMessage);
|
|
53
|
+
window.removeEventListener("popstate", handleRouteChange);
|
|
54
|
+
clearInterval(pollInterval);
|
|
55
|
+
};
|
|
56
|
+
}, [sessionManager]);
|
|
60
57
|
}
|
|
@@ -1,15 +1,13 @@
|
|
|
1
1
|
import { SessionManager, SessionManagerConfig } from "../session-manager";
|
|
2
2
|
interface UseEveryAppSessionParams {
|
|
3
|
-
|
|
3
|
+
sessionManagerConfig: SessionManagerConfig;
|
|
4
4
|
}
|
|
5
|
-
export declare function useEveryAppSession({
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
token: string | null;
|
|
12
|
-
};
|
|
5
|
+
export declare function useEveryAppSession({ sessionManagerConfig, }: UseEveryAppSessionParams): {
|
|
6
|
+
sessionManager: SessionManager | null;
|
|
7
|
+
sessionTokenState: {
|
|
8
|
+
status: "NO_TOKEN" | "VALID" | "EXPIRED" | "REFRESHING";
|
|
9
|
+
token: string | null;
|
|
10
|
+
};
|
|
13
11
|
};
|
|
14
12
|
export {};
|
|
15
|
-
//# sourceMappingURL=useEveryAppSession.d.ts.map
|
|
13
|
+
//# sourceMappingURL=useEveryAppSession.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useEveryAppSession.d.ts","sourceRoot":"","sources":["../../../src/client/_internal/useEveryAppSession.tsx"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,oBAAoB,EAAE,MAAM,oBAAoB,CAAC;AAE1E,UAAU,wBAAwB;IAChC,oBAAoB,EAAE,oBAAoB,CAAC;CAC5C;AAED,wBAAgB,kBAAkB,CAAC,EACjC,oBAAoB,GACrB,EAAE,wBAAwB;;;;;;
|
|
1
|
+
{"version":3,"file":"useEveryAppSession.d.ts","sourceRoot":"","sources":["../../../src/client/_internal/useEveryAppSession.tsx"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,oBAAoB,EAAE,MAAM,oBAAoB,CAAC;AAE1E,UAAU,wBAAwB;IAChC,oBAAoB,EAAE,oBAAoB,CAAC;CAC5C;AAED,wBAAgB,kBAAkB,CAAC,EACjC,oBAAoB,GACrB,EAAE,wBAAwB;;;;;;EAyC1B"}
|
|
@@ -1,31 +1,36 @@
|
|
|
1
1
|
import { useEffect, useRef, useState } from "react";
|
|
2
2
|
import { SessionManager } from "../session-manager";
|
|
3
|
-
export function useEveryAppSession({ sessionManagerConfig }) {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
});
|
|
9
|
-
if (!sessionManagerRef.current && typeof document !== "undefined") {
|
|
10
|
-
sessionManagerRef.current = new SessionManager(sessionManagerConfig);
|
|
11
|
-
}
|
|
12
|
-
const sessionManager = sessionManagerRef.current;
|
|
13
|
-
useEffect(() => {
|
|
14
|
-
if (!sessionManager) return;
|
|
15
|
-
const interval = setInterval(() => {
|
|
16
|
-
setSessionTokenState(sessionManager.getTokenState());
|
|
17
|
-
}, 5000);
|
|
18
|
-
sessionManager.getToken().catch((err) => {
|
|
19
|
-
console.error("[EmbeddedProvider] Initial token request failed:", err);
|
|
3
|
+
export function useEveryAppSession({ sessionManagerConfig, }) {
|
|
4
|
+
const sessionManagerRef = useRef(null);
|
|
5
|
+
const [sessionTokenState, setSessionTokenState] = useState({
|
|
6
|
+
status: "NO_TOKEN",
|
|
7
|
+
token: null,
|
|
20
8
|
});
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
9
|
+
if (!sessionManagerRef.current && typeof document !== "undefined") {
|
|
10
|
+
sessionManagerRef.current = new SessionManager(sessionManagerConfig);
|
|
11
|
+
}
|
|
12
|
+
const sessionManager = sessionManagerRef.current;
|
|
13
|
+
useEffect(() => {
|
|
14
|
+
if (!sessionManager)
|
|
15
|
+
return;
|
|
16
|
+
// Skip token requests when not in iframe - the app will show GatewayRequiredError instead
|
|
17
|
+
if (!sessionManager.isInIframe)
|
|
18
|
+
return;
|
|
19
|
+
const interval = setInterval(() => {
|
|
20
|
+
setSessionTokenState(sessionManager.getTokenState());
|
|
21
|
+
}, 5000);
|
|
22
|
+
sessionManager.getToken().catch((err) => {
|
|
23
|
+
console.error("[EmbeddedProvider] Initial token request failed:", err);
|
|
24
|
+
});
|
|
25
|
+
return () => {
|
|
26
|
+
clearInterval(interval);
|
|
27
|
+
};
|
|
28
|
+
}, [sessionManager]);
|
|
29
|
+
useEffect(() => {
|
|
30
|
+
if (!sessionManager)
|
|
31
|
+
return;
|
|
32
|
+
// Make sessionManager globally accessible for middleware
|
|
33
|
+
window.__embeddedSessionManager = sessionManager;
|
|
34
|
+
}, [sessionManager]);
|
|
35
|
+
return { sessionManager, sessionTokenState };
|
|
31
36
|
}
|
|
@@ -5,8 +5,5 @@ export declare function getSessionToken(): Promise<string>;
|
|
|
5
5
|
/**
|
|
6
6
|
* Performs a fetch request with the authorization header automatically added
|
|
7
7
|
*/
|
|
8
|
-
export declare function authenticatedFetch(
|
|
9
|
-
|
|
10
|
-
init?: RequestInit,
|
|
11
|
-
): Promise<Response>;
|
|
12
|
-
//# sourceMappingURL=authenticatedFetch.d.ts.map
|
|
8
|
+
export declare function authenticatedFetch(input: RequestInfo | URL, init?: RequestInit): Promise<Response>;
|
|
9
|
+
//# sourceMappingURL=authenticatedFetch.d.ts.map
|
|
@@ -2,26 +2,26 @@
|
|
|
2
2
|
* Gets the current session token from the embedded session manager
|
|
3
3
|
*/
|
|
4
4
|
export async function getSessionToken() {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
5
|
+
const windowWithSession = window;
|
|
6
|
+
const sessionManager = windowWithSession.__embeddedSessionManager;
|
|
7
|
+
if (!sessionManager) {
|
|
8
|
+
throw new Error("Session manager not available");
|
|
9
|
+
}
|
|
10
|
+
const token = await sessionManager.getToken();
|
|
11
|
+
if (!token) {
|
|
12
|
+
throw new Error("No token available");
|
|
13
|
+
}
|
|
14
|
+
return token;
|
|
15
15
|
}
|
|
16
16
|
/**
|
|
17
17
|
* Performs a fetch request with the authorization header automatically added
|
|
18
18
|
*/
|
|
19
19
|
export async function authenticatedFetch(input, init) {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
20
|
+
const token = await getSessionToken();
|
|
21
|
+
const headers = new Headers(init?.headers);
|
|
22
|
+
headers.set("Authorization", `Bearer ${token}`);
|
|
23
|
+
return fetch(input, {
|
|
24
|
+
...init,
|
|
25
|
+
headers,
|
|
26
|
+
});
|
|
27
27
|
}
|
package/dist/client/index.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
export { SessionManager } from "./session-manager";
|
|
1
|
+
export { SessionManager, isRunningInIframe } from "./session-manager";
|
|
2
2
|
export type { SessionManagerConfig } from "./session-manager";
|
|
3
3
|
export { useSessionTokenClientMiddleware } from "./useSessionTokenClientMiddleware";
|
|
4
4
|
export { EmbeddedAppProvider, useCurrentUser } from "./EmbeddedAppProvider";
|
|
5
|
+
export { GatewayRequiredError } from "./GatewayRequiredError";
|
|
5
6
|
export { lazyInitForWorkers } from "./lazyInitForWorkers";
|
|
6
7
|
export { authenticatedFetch, getSessionToken } from "./authenticatedFetch";
|
|
7
|
-
//# sourceMappingURL=index.d.ts.map
|
|
8
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/client/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/client/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AACtE,YAAY,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AAC9D,OAAO,EAAE,+BAA+B,EAAE,MAAM,mCAAmC,CAAC;AAEpF,OAAO,EAAE,mBAAmB,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAC5E,OAAO,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAC;AAE9D,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAE1D,OAAO,EAAE,kBAAkB,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC"}
|
package/dist/client/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
export { SessionManager } from "./session-manager";
|
|
1
|
+
export { SessionManager, isRunningInIframe } from "./session-manager";
|
|
2
2
|
export { useSessionTokenClientMiddleware } from "./useSessionTokenClientMiddleware";
|
|
3
3
|
export { EmbeddedAppProvider, useCurrentUser } from "./EmbeddedAppProvider";
|
|
4
|
+
export { GatewayRequiredError } from "./GatewayRequiredError";
|
|
4
5
|
export { lazyInitForWorkers } from "./lazyInitForWorkers";
|
|
5
6
|
export { authenticatedFetch, getSessionToken } from "./authenticatedFetch";
|
|
@@ -18,7 +18,5 @@
|
|
|
18
18
|
* );
|
|
19
19
|
* ```
|
|
20
20
|
*/
|
|
21
|
-
export declare function lazyInitForWorkers<T extends object>(
|
|
22
|
-
|
|
23
|
-
): T;
|
|
24
|
-
//# sourceMappingURL=lazyInitForWorkers.d.ts.map
|
|
21
|
+
export declare function lazyInitForWorkers<T extends object>(factory: () => T): T;
|
|
22
|
+
//# sourceMappingURL=lazyInitForWorkers.d.ts.map
|
|
@@ -19,50 +19,47 @@
|
|
|
19
19
|
* ```
|
|
20
20
|
*/
|
|
21
21
|
export function lazyInitForWorkers(factory) {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
22
|
+
// Closure: This variable is captured by getInstance() and the Proxy traps below.
|
|
23
|
+
// It remains in memory as long as the returned Proxy is referenced, enabling singleton behavior.
|
|
24
|
+
let instance = null;
|
|
25
|
+
function getInstance() {
|
|
26
|
+
if (!instance) {
|
|
27
|
+
instance = factory();
|
|
28
|
+
}
|
|
29
|
+
return instance;
|
|
28
30
|
}
|
|
29
|
-
return
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
const inst = getInstance();
|
|
64
|
-
return Reflect.getPrototypeOf(inst);
|
|
65
|
-
},
|
|
66
|
-
},
|
|
67
|
-
);
|
|
31
|
+
return new Proxy({}, {
|
|
32
|
+
get(_, prop) {
|
|
33
|
+
const inst = getInstance();
|
|
34
|
+
const value = inst[prop];
|
|
35
|
+
// Bind methods to the instance to preserve `this` context
|
|
36
|
+
return typeof value === "function" ? value.bind(inst) : value;
|
|
37
|
+
},
|
|
38
|
+
set(_, prop, value) {
|
|
39
|
+
const inst = getInstance();
|
|
40
|
+
inst[prop] = value;
|
|
41
|
+
return true;
|
|
42
|
+
},
|
|
43
|
+
deleteProperty(_, prop) {
|
|
44
|
+
const inst = getInstance();
|
|
45
|
+
delete inst[prop];
|
|
46
|
+
return true;
|
|
47
|
+
},
|
|
48
|
+
has(_, prop) {
|
|
49
|
+
const inst = getInstance();
|
|
50
|
+
return prop in inst;
|
|
51
|
+
},
|
|
52
|
+
ownKeys(_) {
|
|
53
|
+
const inst = getInstance();
|
|
54
|
+
return Reflect.ownKeys(inst);
|
|
55
|
+
},
|
|
56
|
+
getOwnPropertyDescriptor(_, prop) {
|
|
57
|
+
const inst = getInstance();
|
|
58
|
+
return Reflect.getOwnPropertyDescriptor(inst, prop);
|
|
59
|
+
},
|
|
60
|
+
getPrototypeOf(_) {
|
|
61
|
+
const inst = getInstance();
|
|
62
|
+
return Reflect.getPrototypeOf(inst);
|
|
63
|
+
},
|
|
64
|
+
});
|
|
68
65
|
}
|