@eventuras/fides-auth-next 0.3.0 → 0.6.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 (61) hide show
  1. package/LICENSE +21 -674
  2. package/README.md +6 -1
  3. package/dist/cookie-store.d.ts +10 -0
  4. package/dist/cookie-store.d.ts.map +1 -0
  5. package/dist/cookie-store.js +25 -0
  6. package/dist/cookie-store.js.map +1 -0
  7. package/dist/cookies.d.ts +18 -41
  8. package/dist/cookies.d.ts.map +1 -1
  9. package/dist/cookies.js +31 -29
  10. package/dist/cookies.js.map +1 -1
  11. package/dist/heartbeat-handler.d.ts +1 -1
  12. package/dist/heartbeat-handler.d.ts.map +1 -1
  13. package/dist/heartbeat-handler.js +12 -36
  14. package/dist/heartbeat-handler.js.map +1 -1
  15. package/dist/index.d.ts +1 -0
  16. package/dist/index.d.ts.map +1 -1
  17. package/dist/index.js +6 -11
  18. package/dist/oidc-callback.d.ts +5 -5
  19. package/dist/oidc-callback.d.ts.map +1 -1
  20. package/dist/oidc-callback.js +16 -39
  21. package/dist/oidc-callback.js.map +1 -1
  22. package/dist/oidc-login.d.ts.map +1 -1
  23. package/dist/oidc-login.js +12 -24
  24. package/dist/oidc-login.js.map +1 -1
  25. package/dist/oidc-logout.d.ts +44 -0
  26. package/dist/oidc-logout.d.ts.map +1 -0
  27. package/dist/oidc-logout.js +44 -0
  28. package/dist/oidc-logout.js.map +1 -0
  29. package/dist/request.js.map +1 -1
  30. package/dist/session.d.ts +24 -62
  31. package/dist/session.d.ts.map +1 -1
  32. package/dist/session.js +30 -132
  33. package/dist/session.js.map +1 -1
  34. package/dist/store/index.d.ts +2 -7
  35. package/dist/store/index.d.ts.map +1 -1
  36. package/dist/store/index.js +2 -6
  37. package/package.json +24 -23
  38. package/dist/store/configure-logger.d.ts +0 -15
  39. package/dist/store/configure-logger.d.ts.map +0 -1
  40. package/dist/store/configure-logger.js +0 -25
  41. package/dist/store/configure-logger.js.map +0 -1
  42. package/dist/store/hooks.d.ts +0 -64
  43. package/dist/store/hooks.d.ts.map +0 -1
  44. package/dist/store/hooks.js +0 -78
  45. package/dist/store/hooks.js.map +0 -1
  46. package/dist/store/session-monitor.d.ts +0 -51
  47. package/dist/store/session-monitor.d.ts.map +0 -1
  48. package/dist/store/session-monitor.js +0 -92
  49. package/dist/store/session-monitor.js.map +0 -1
  50. package/dist/store/store.d.ts +0 -81
  51. package/dist/store/store.d.ts.map +0 -1
  52. package/dist/store/store.js +0 -218
  53. package/dist/store/store.js.map +0 -1
  54. package/dist/store/use-heartbeat.d.ts +0 -98
  55. package/dist/store/use-heartbeat.d.ts.map +0 -1
  56. package/dist/store/use-heartbeat.js +0 -205
  57. package/dist/store/use-heartbeat.js.map +0 -1
  58. package/dist/store/use-session-monitor.d.ts +0 -28
  59. package/dist/store/use-session-monitor.d.ts.map +0 -1
  60. package/dist/store/use-session-monitor.js +0 -40
  61. package/dist/store/use-session-monitor.js.map +0 -1
package/package.json CHANGED
@@ -1,16 +1,18 @@
1
1
  {
2
2
  "name": "@eventuras/fides-auth-next",
3
- "version": "0.3.0",
3
+ "version": "0.6.1",
4
4
  "description": "Next.js bindings for @eventuras/fides-auth",
5
- "homepage": "https://github.com/losol/eventuras/tree/main/libs/fides-auth-next#readme",
5
+ "homepage": "https://github.com/losol/fides-auth/tree/main/packages/fides-auth-next#readme",
6
6
  "bugs": {
7
- "url": "https://github.com/losol/eventuras/issues"
7
+ "url": "https://github.com/losol/fides-auth/issues"
8
8
  },
9
9
  "repository": {
10
10
  "type": "git",
11
- "url": "https://github.com/losol/eventuras.git",
12
- "directory": "libs/fides-auth-next"
11
+ "url": "https://github.com/losol/fides-auth.git",
12
+ "directory": "packages/fides-auth-next"
13
13
  },
14
+ "license": "MIT",
15
+ "author": "losol <losol@users.noreply.github.com>",
14
16
  "exports": {
15
17
  ".": {
16
18
  "types": "./dist/index.d.ts",
@@ -36,6 +38,10 @@
36
38
  "types": "./dist/oidc-login.d.ts",
37
39
  "import": "./dist/oidc-login.js"
38
40
  },
41
+ "./oidc-logout": {
42
+ "types": "./dist/oidc-logout.d.ts",
43
+ "import": "./dist/oidc-logout.js"
44
+ },
39
45
  "./heartbeat-handler": {
40
46
  "types": "./dist/heartbeat-handler.d.ts",
41
47
  "import": "./dist/heartbeat-handler.js"
@@ -44,38 +50,33 @@
44
50
  "main": "./dist/index.js",
45
51
  "types": "./dist/index.d.ts",
46
52
  "files": [
47
- "dist"
53
+ "dist",
54
+ "LICENSE"
48
55
  ],
49
56
  "dependencies": {
50
- "@xstate/store": "^4.0.0",
51
- "@xstate/store-react": "^2.0.0",
52
- "@eventuras/fides-auth": "0.9.0",
53
- "@eventuras/logger": "0.8.1"
57
+ "@eventuras/logger": "^0.8.1",
58
+ "@eventuras/fides-auth": "0.13.0",
59
+ "@eventuras/fides-auth-react": "0.1.4",
60
+ "@eventuras/fides-auth-store": "0.2.0"
54
61
  },
55
62
  "devDependencies": {
56
63
  "@testing-library/jest-dom": "6.9.1",
57
- "@testing-library/react": "^16.3.2",
58
- "@types/react": "^19.2.14",
59
- "@types/react-dom": "^19.2.3",
64
+ "@types/react": "^19.2.18",
60
65
  "jsdom": "29.1.1",
61
- "react": "^19.2.0",
62
- "react-dom": "^19.2.0",
63
- "vite": "^8.0.8",
64
- "vitest": "^4.1.6",
65
- "xstate": "^5.30.0",
66
+ "react": "^19.2.8",
67
+ "typescript": "^6.0.2",
68
+ "vite": "^8.2.0",
69
+ "vitest": "^4.1.10",
66
70
  "@eventuras/typescript-config": "1.0.0",
67
71
  "@eventuras/vite-config": "0.2.2"
68
72
  },
69
73
  "peerDependencies": {
70
- "@xstate/react": "^5.0.0 || ^6.0.0",
71
- "@xstate/store": "^4.0.0",
72
- "@xstate/store-react": "^2.0.0",
73
74
  "next": "^16.0.0",
74
- "react": "^19.0.0",
75
- "xstate": "^5.0.0"
75
+ "react": "^19.0.0"
76
76
  },
77
77
  "scripts": {
78
78
  "build": "vite build",
79
+ "typecheck": "tsc --noEmit",
79
80
  "dev": "vite build --watch",
80
81
  "test": "vitest run",
81
82
  "test:watch": "vitest"
@@ -1,15 +0,0 @@
1
- /**
2
- * Configures fides-auth to use @eventuras/logger for all internal logging.
3
- *
4
- * Call this once at application startup, before creating the auth store.
5
- *
6
- * @example
7
- * ```typescript
8
- * import { configureAuthLogger, createAuthStore } from '@eventuras/fides-auth-next/store';
9
- *
10
- * configureAuthLogger();
11
- * const authStore = createAuthStore({ checkAuthStatus });
12
- * ```
13
- */
14
- export declare function configureAuthLogger(): void;
15
- //# sourceMappingURL=configure-logger.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"configure-logger.d.ts","sourceRoot":"","sources":["../../src/store/configure-logger.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAKH,wBAAgB,mBAAmB,IAAI,IAAI,CAM1C"}
@@ -1,25 +0,0 @@
1
- import { Logger } from "@eventuras/logger";
2
- import { configureLogger } from "@eventuras/fides-auth/logger";
3
- //#region src/store/configure-logger.ts
4
- /**
5
- * Configures fides-auth to use @eventuras/logger for all internal logging.
6
- *
7
- * Call this once at application startup, before creating the auth store.
8
- *
9
- * @example
10
- * ```typescript
11
- * import { configureAuthLogger, createAuthStore } from '@eventuras/fides-auth-next/store';
12
- *
13
- * configureAuthLogger();
14
- * const authStore = createAuthStore({ checkAuthStatus });
15
- * ```
16
- */
17
- function configureAuthLogger() {
18
- configureLogger({ create(options) {
19
- return Logger.create(options);
20
- } });
21
- }
22
- //#endregion
23
- export { configureAuthLogger };
24
-
25
- //# sourceMappingURL=configure-logger.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"configure-logger.js","names":[],"sources":["../../src/store/configure-logger.ts"],"sourcesContent":["/**\n * Configures fides-auth to use @eventuras/logger for all internal logging.\n *\n * Call this once at application startup, before creating the auth store.\n *\n * @example\n * ```typescript\n * import { configureAuthLogger, createAuthStore } from '@eventuras/fides-auth-next/store';\n *\n * configureAuthLogger();\n * const authStore = createAuthStore({ checkAuthStatus });\n * ```\n */\n\nimport { configureLogger } from '@eventuras/fides-auth/logger';\nimport { Logger } from '@eventuras/logger';\n\nexport function configureAuthLogger(): void {\n configureLogger({\n create(options) {\n return Logger.create(options);\n },\n });\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAiBA,SAAgB,sBAA4B;CAC1C,gBAAgB,EACd,OAAO,SAAS;EACd,OAAO,OAAO,OAAO,OAAO;CAC9B,EACF,CAAC;AACH"}
@@ -1,64 +0,0 @@
1
- import { SessionUser } from './types';
2
- import { createAuthStore } from './store';
3
- /**
4
- * Return type for store-based auth hooks
5
- */
6
- export type AuthStoreSelector = {
7
- isAuthenticated: boolean;
8
- isAdmin: boolean;
9
- user: SessionUser | null;
10
- error: string | null;
11
- lastChecked: Date | null;
12
- isInitializing: boolean;
13
- };
14
- /**
15
- * Actions for store-based auth
16
- */
17
- export type AuthStoreActions = {
18
- loginSuccess: (user: SessionUser) => void;
19
- logout: () => void;
20
- sessionExpired: () => void;
21
- authSuccess: (user: SessionUser) => void;
22
- authFailed: (error?: string) => void;
23
- setError: (error: string) => void;
24
- clearError: () => void;
25
- };
26
- /**
27
- * Creates React hooks for interacting with the auth store
28
- *
29
- * @param store - The XState store instance
30
- * @returns Object containing useAuthStore hook
31
- *
32
- * @example
33
- * ```typescript
34
- * const authStore = createAuthStore({
35
- * checkAuthStatus: getAuthStatus,
36
- * });
37
- *
38
- * export const { useAuthStore } = createAuthStoreHooks(authStore);
39
- *
40
- * // In component
41
- * function MyComponent() {
42
- * const auth = useAuthStore();
43
- *
44
- * if (auth.error) {
45
- * toast.error(auth.error);
46
- * }
47
- *
48
- * if (auth.isInitializing) {
49
- * return <Spinner />;
50
- * }
51
- *
52
- * return (
53
- * <div>
54
- * {auth.isAuthenticated ? `Hello ${auth.user?.name}` : 'Please login'}
55
- * </div>
56
- * );
57
- * }
58
- * ```
59
- */
60
- export declare function createAuthStoreHooks(store: ReturnType<typeof createAuthStore>): {
61
- useAuthStore: () => AuthStoreSelector;
62
- useAuthActions: () => AuthStoreActions;
63
- };
64
- //# sourceMappingURL=hooks.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"hooks.d.ts","sourceRoot":"","sources":["../../src/store/hooks.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAC3C,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAE/C;;GAEG;AACH,MAAM,MAAM,iBAAiB,GAAG;IAC9B,eAAe,EAAE,OAAO,CAAC;IACzB,OAAO,EAAE,OAAO,CAAC;IACjB,IAAI,EAAE,WAAW,GAAG,IAAI,CAAC;IACzB,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,WAAW,EAAE,IAAI,GAAG,IAAI,CAAC;IACzB,cAAc,EAAE,OAAO,CAAC;CACzB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,gBAAgB,GAAG;IAC7B,YAAY,EAAE,CAAC,IAAI,EAAE,WAAW,KAAK,IAAI,CAAC;IAC1C,MAAM,EAAE,MAAM,IAAI,CAAC;IACnB,cAAc,EAAE,MAAM,IAAI,CAAC;IAC3B,WAAW,EAAE,CAAC,IAAI,EAAE,WAAW,KAAK,IAAI,CAAC;IACzC,UAAU,EAAE,CAAC,KAAK,CAAC,EAAE,MAAM,KAAK,IAAI,CAAC;IACrC,QAAQ,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;IAClC,UAAU,EAAE,MAAM,IAAI,CAAC;CACxB,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,UAAU,CAAC,OAAO,eAAe,CAAC;wBAInD,iBAAiB;0BAQf,gBAAgB;EAgB5C"}
@@ -1,78 +0,0 @@
1
- import { useSelector } from "@xstate/store-react";
2
- //#region src/store/hooks.ts
3
- /**
4
- * Creates React hooks for interacting with the auth store
5
- *
6
- * @param store - The XState store instance
7
- * @returns Object containing useAuthStore hook
8
- *
9
- * @example
10
- * ```typescript
11
- * const authStore = createAuthStore({
12
- * checkAuthStatus: getAuthStatus,
13
- * });
14
- *
15
- * export const { useAuthStore } = createAuthStoreHooks(authStore);
16
- *
17
- * // In component
18
- * function MyComponent() {
19
- * const auth = useAuthStore();
20
- *
21
- * if (auth.error) {
22
- * toast.error(auth.error);
23
- * }
24
- *
25
- * if (auth.isInitializing) {
26
- * return <Spinner />;
27
- * }
28
- *
29
- * return (
30
- * <div>
31
- * {auth.isAuthenticated ? `Hello ${auth.user?.name}` : 'Please login'}
32
- * </div>
33
- * );
34
- * }
35
- * ```
36
- */
37
- function createAuthStoreHooks(store) {
38
- /**
39
- * Hook to access the auth store state
40
- */
41
- function useAuthStore() {
42
- return useSelector(store, (snapshot) => snapshot).context;
43
- }
44
- /**
45
- * Hook to get auth store actions
46
- */
47
- function useAuthActions() {
48
- return {
49
- loginSuccess: (user) => store.send({
50
- type: "loginSuccess",
51
- user
52
- }),
53
- logout: () => store.send({ type: "logout" }),
54
- sessionExpired: () => store.send({ type: "sessionExpired" }),
55
- authSuccess: (user) => store.send({
56
- type: "authSuccess",
57
- user
58
- }),
59
- authFailed: (error) => store.send({
60
- type: "authFailed",
61
- error
62
- }),
63
- setError: (error) => store.send({
64
- type: "setError",
65
- error
66
- }),
67
- clearError: () => store.send({ type: "clearError" })
68
- };
69
- }
70
- return {
71
- useAuthStore,
72
- useAuthActions
73
- };
74
- }
75
- //#endregion
76
- export { createAuthStoreHooks };
77
-
78
- //# sourceMappingURL=hooks.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"hooks.js","names":[],"sources":["../../src/store/hooks.ts"],"sourcesContent":["import { useSelector as useStoreSelector } from '@xstate/store-react';\nimport type { SessionUser } from './types';\nimport type { createAuthStore } from './store';\n\n/**\n * Return type for store-based auth hooks\n */\nexport type AuthStoreSelector = {\n isAuthenticated: boolean;\n isAdmin: boolean;\n user: SessionUser | null;\n error: string | null;\n lastChecked: Date | null;\n isInitializing: boolean;\n};\n\n/**\n * Actions for store-based auth\n */\nexport type AuthStoreActions = {\n loginSuccess: (user: SessionUser) => void;\n logout: () => void;\n sessionExpired: () => void;\n authSuccess: (user: SessionUser) => void;\n authFailed: (error?: string) => void;\n setError: (error: string) => void;\n clearError: () => void;\n};\n\n/**\n * Creates React hooks for interacting with the auth store\n *\n * @param store - The XState store instance\n * @returns Object containing useAuthStore hook\n *\n * @example\n * ```typescript\n * const authStore = createAuthStore({\n * checkAuthStatus: getAuthStatus,\n * });\n *\n * export const { useAuthStore } = createAuthStoreHooks(authStore);\n *\n * // In component\n * function MyComponent() {\n * const auth = useAuthStore();\n *\n * if (auth.error) {\n * toast.error(auth.error);\n * }\n *\n * if (auth.isInitializing) {\n * return <Spinner />;\n * }\n *\n * return (\n * <div>\n * {auth.isAuthenticated ? `Hello ${auth.user?.name}` : 'Please login'}\n * </div>\n * );\n * }\n * ```\n */\nexport function createAuthStoreHooks(store: ReturnType<typeof createAuthStore>) {\n /**\n * Hook to access the auth store state\n */\n function useAuthStore(): AuthStoreSelector {\n const snapshot = useStoreSelector(store, (snapshot) => snapshot);\n return snapshot.context;\n }\n\n /**\n * Hook to get auth store actions\n */\n function useAuthActions(): AuthStoreActions {\n return {\n loginSuccess: (user: SessionUser) => store.send({ type: 'loginSuccess', user }),\n logout: () => store.send({ type: 'logout' }),\n sessionExpired: () => store.send({ type: 'sessionExpired' }),\n authSuccess: (user: SessionUser) => store.send({ type: 'authSuccess', user }),\n authFailed: (error?: string) => store.send({ type: 'authFailed', error }),\n setError: (error: string) => store.send({ type: 'setError', error }),\n clearError: () => store.send({ type: 'clearError' }),\n };\n }\n\n return {\n useAuthStore,\n useAuthActions,\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+DA,SAAgB,qBAAqB,OAA2C;;;;CAI9E,SAAS,eAAkC;EAEzC,OADiB,YAAiB,QAAQ,aAAa,QAChD,EAAS;CAClB;;;;CAKA,SAAS,iBAAmC;EAC1C,OAAO;GACL,eAAe,SAAsB,MAAM,KAAK;IAAE,MAAM;IAAgB;GAAK,CAAC;GAC9E,cAAc,MAAM,KAAK,EAAE,MAAM,SAAS,CAAC;GAC3C,sBAAsB,MAAM,KAAK,EAAE,MAAM,iBAAiB,CAAC;GAC3D,cAAc,SAAsB,MAAM,KAAK;IAAE,MAAM;IAAe;GAAK,CAAC;GAC5E,aAAa,UAAmB,MAAM,KAAK;IAAE,MAAM;IAAc;GAAM,CAAC;GACxE,WAAW,UAAkB,MAAM,KAAK;IAAE,MAAM;IAAY;GAAM,CAAC;GACnE,kBAAkB,MAAM,KAAK,EAAE,MAAM,aAAa,CAAC;EACrD;CACF;CAEA,OAAO;EACL;EACA;CACF;AACF"}
@@ -1,51 +0,0 @@
1
- import { createAuthStore } from './store';
2
- import { AuthStatus } from './types';
3
- /**
4
- * Configuration for session monitoring
5
- */
6
- export type SessionMonitorConfig = {
7
- /**
8
- * Interval in milliseconds for session monitoring polling
9
- * @default 30000 (30 seconds)
10
- */
11
- interval?: number;
12
- /**
13
- * Namespace for logger
14
- * @default 'fides:session-monitor'
15
- */
16
- loggerNamespace?: string;
17
- /**
18
- * Callback when session expires
19
- */
20
- onSessionExpired?: () => void;
21
- /**
22
- * Callback when session check fails
23
- */
24
- onError?: (error: Error) => void;
25
- };
26
- /**
27
- * Creates a session monitor that polls auth status at regular intervals
28
- *
29
- * This is designed to be used with React's useEffect for cleanup.
30
- *
31
- * @param store - The auth store to update
32
- * @param checkAuthStatus - Function to check authentication status
33
- * @param config - Optional configuration
34
- * @returns Cleanup function to stop monitoring
35
- *
36
- * @example
37
- * ```typescript
38
- * useEffect(() => {
39
- * const cleanup = startSessionMonitor(authStore, checkAuthStatus, {
40
- * interval: 30_000,
41
- * onSessionExpired: () => {
42
- * toast.warn('Your session has expired');
43
- * }
44
- * });
45
- *
46
- * return cleanup;
47
- * }, []);
48
- * ```
49
- */
50
- export declare function startSessionMonitor(store: ReturnType<typeof createAuthStore>, checkAuthStatus: () => Promise<AuthStatus>, config?: SessionMonitorConfig): () => void;
51
- //# sourceMappingURL=session-monitor.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"session-monitor.d.ts","sourceRoot":"","sources":["../../src/store/session-monitor.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAC/C,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAE1C;;GAEG;AACH,MAAM,MAAM,oBAAoB,GAAG;IACjC;;;OAGG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;IAElB;;;OAGG;IACH,eAAe,CAAC,EAAE,MAAM,CAAC;IAEzB;;OAEG;IACH,gBAAgB,CAAC,EAAE,MAAM,IAAI,CAAC;IAE9B;;OAEG;IACH,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,CAAC;CAClC,CAAC;AAOF;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,mBAAmB,CACjC,KAAK,EAAE,UAAU,CAAC,OAAO,eAAe,CAAC,EACzC,eAAe,EAAE,MAAM,OAAO,CAAC,UAAU,CAAC,EAC1C,MAAM,GAAE,oBAAyB,GAChC,MAAM,IAAI,CA8FZ"}
@@ -1,92 +0,0 @@
1
- import { Logger } from "@eventuras/logger";
2
- //#region src/store/session-monitor.ts
3
- var DEFAULT_CONFIG = {
4
- interval: 3e4,
5
- loggerNamespace: "fides:session-monitor"
6
- };
7
- /**
8
- * Creates a session monitor that polls auth status at regular intervals
9
- *
10
- * This is designed to be used with React's useEffect for cleanup.
11
- *
12
- * @param store - The auth store to update
13
- * @param checkAuthStatus - Function to check authentication status
14
- * @param config - Optional configuration
15
- * @returns Cleanup function to stop monitoring
16
- *
17
- * @example
18
- * ```typescript
19
- * useEffect(() => {
20
- * const cleanup = startSessionMonitor(authStore, checkAuthStatus, {
21
- * interval: 30_000,
22
- * onSessionExpired: () => {
23
- * toast.warn('Your session has expired');
24
- * }
25
- * });
26
- *
27
- * return cleanup;
28
- * }, []);
29
- * ```
30
- */
31
- function startSessionMonitor(store, checkAuthStatus, config = {}) {
32
- const finalConfig = {
33
- ...DEFAULT_CONFIG,
34
- ...config
35
- };
36
- const logger = Logger.create({
37
- namespace: finalConfig.loggerNamespace,
38
- context: { component: "SessionMonitor" }
39
- });
40
- logger.debug({ interval: finalConfig.interval }, "Starting session monitor");
41
- let isActive = true;
42
- let timeoutId = null;
43
- const poll = async () => {
44
- if (!isActive) return;
45
- try {
46
- const result = await checkAuthStatus();
47
- logger.debug({ authenticated: result.authenticated }, "Session monitor check");
48
- if (!result.authenticated) {
49
- logger.info("Session monitor detected unauthenticated state - session ended");
50
- store.send({ type: "sessionExpired" });
51
- if (finalConfig.onSessionExpired) finalConfig.onSessionExpired();
52
- return;
53
- }
54
- if (result.user) store.send({
55
- type: "authSuccess",
56
- user: result.user
57
- });
58
- } catch (error) {
59
- const err = error;
60
- if (err?.code === "OAUTH_RESPONSE_BODY_ERROR" && err?.error === "invalid_grant") {
61
- logger.info("Session monitor detected expired refresh token - user session ended");
62
- store.send({ type: "sessionExpired" });
63
- if (finalConfig.onSessionExpired) finalConfig.onSessionExpired();
64
- return;
65
- }
66
- const errorDetails = { message: error instanceof Error ? error.message : "Unknown error" };
67
- if (error && typeof error === "object") {
68
- const oauthError = error;
69
- if (oauthError.code) errorDetails.code = oauthError.code;
70
- if (oauthError.error) errorDetails.error = oauthError.error;
71
- if (oauthError.error_description) errorDetails.error_description = oauthError.error_description;
72
- if (oauthError.status) errorDetails.status = oauthError.status;
73
- }
74
- logger.error(errorDetails, "Session monitor error");
75
- if (finalConfig.onError && error instanceof Error) finalConfig.onError(error);
76
- }
77
- if (isActive) timeoutId = setTimeout(poll, finalConfig.interval);
78
- };
79
- timeoutId = setTimeout(poll, finalConfig.interval);
80
- return () => {
81
- logger.debug("Stopping session monitor");
82
- isActive = false;
83
- if (timeoutId) {
84
- clearTimeout(timeoutId);
85
- timeoutId = null;
86
- }
87
- };
88
- }
89
- //#endregion
90
- export { startSessionMonitor };
91
-
92
- //# sourceMappingURL=session-monitor.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"session-monitor.js","names":[],"sources":["../../src/store/session-monitor.ts"],"sourcesContent":["import { Logger } from '@eventuras/logger';\nimport type { createAuthStore } from './store';\nimport type { AuthStatus } from './types';\n\n/**\n * Configuration for session monitoring\n */\nexport type SessionMonitorConfig = {\n /**\n * Interval in milliseconds for session monitoring polling\n * @default 30000 (30 seconds)\n */\n interval?: number;\n\n /**\n * Namespace for logger\n * @default 'fides:session-monitor'\n */\n loggerNamespace?: string;\n\n /**\n * Callback when session expires\n */\n onSessionExpired?: () => void;\n\n /**\n * Callback when session check fails\n */\n onError?: (error: Error) => void;\n};\n\nconst DEFAULT_CONFIG = {\n interval: 30_000, // 30 seconds\n loggerNamespace: 'fides:session-monitor',\n};\n\n/**\n * Creates a session monitor that polls auth status at regular intervals\n *\n * This is designed to be used with React's useEffect for cleanup.\n *\n * @param store - The auth store to update\n * @param checkAuthStatus - Function to check authentication status\n * @param config - Optional configuration\n * @returns Cleanup function to stop monitoring\n *\n * @example\n * ```typescript\n * useEffect(() => {\n * const cleanup = startSessionMonitor(authStore, checkAuthStatus, {\n * interval: 30_000,\n * onSessionExpired: () => {\n * toast.warn('Your session has expired');\n * }\n * });\n *\n * return cleanup;\n * }, []);\n * ```\n */\nexport function startSessionMonitor(\n store: ReturnType<typeof createAuthStore>,\n checkAuthStatus: () => Promise<AuthStatus>,\n config: SessionMonitorConfig = {}\n): () => void {\n const finalConfig = { ...DEFAULT_CONFIG, ...config };\n const logger = Logger.create({\n namespace: finalConfig.loggerNamespace,\n context: { component: 'SessionMonitor' },\n });\n\n logger.debug({ interval: finalConfig.interval }, 'Starting session monitor');\n\n let isActive = true;\n let timeoutId: NodeJS.Timeout | null = null;\n\n const poll = async (): Promise<void> => {\n if (!isActive) return;\n\n try {\n const result = await checkAuthStatus();\n logger.debug({ authenticated: result.authenticated }, 'Session monitor check');\n\n if (!result.authenticated) {\n // User is no longer authenticated - session ended\n logger.info('Session monitor detected unauthenticated state - session ended');\n store.send({ type: 'sessionExpired' });\n\n if (finalConfig.onSessionExpired) {\n finalConfig.onSessionExpired();\n }\n\n // Stop monitoring after session expires\n return;\n }\n\n if (result.user) {\n store.send({ type: 'authSuccess', user: result.user });\n }\n } catch (error) {\n // Check if this is an expected invalid_grant error (expired refresh token)\n const err = error as { code?: string; error?: string; error_description?: string; };\n const isInvalidGrant =\n err?.code === 'OAUTH_RESPONSE_BODY_ERROR' && err?.error === 'invalid_grant';\n\n if (isInvalidGrant) {\n // This is expected during logout or session expiry - log at info level\n logger.info('Session monitor detected expired refresh token - user session ended');\n store.send({ type: 'sessionExpired' });\n\n if (finalConfig.onSessionExpired) {\n finalConfig.onSessionExpired();\n }\n\n // Stop monitoring after session expires\n return;\n }\n\n // Unexpected error during auth check - log with details\n const errorDetails: Record<string, unknown> = {\n message: error instanceof Error ? error.message : 'Unknown error',\n };\n\n // Extract OAuth error details if present\n if (error && typeof error === 'object') {\n const oauthError = error as any;\n if (oauthError.code) errorDetails.code = oauthError.code;\n if (oauthError.error) errorDetails.error = oauthError.error;\n if (oauthError.error_description)\n errorDetails.error_description = oauthError.error_description;\n if (oauthError.status) errorDetails.status = oauthError.status;\n }\n\n logger.error(errorDetails, 'Session monitor error');\n\n if (finalConfig.onError && error instanceof Error) {\n finalConfig.onError(error);\n }\n }\n\n // Schedule next poll if still active\n if (isActive) {\n timeoutId = setTimeout(poll, finalConfig.interval);\n }\n };\n\n // Start first poll\n timeoutId = setTimeout(poll, finalConfig.interval);\n\n // Return cleanup function\n return () => {\n logger.debug('Stopping session monitor');\n isActive = false;\n if (timeoutId) {\n clearTimeout(timeoutId);\n timeoutId = null;\n }\n };\n}\n"],"mappings":";;AA+BA,IAAM,iBAAiB;CACrB,UAAU;CACV,iBAAiB;AACnB;;;;;;;;;;;;;;;;;;;;;;;;;AA0BA,SAAgB,oBACd,OACA,iBACA,SAA+B,CAAC,GACpB;CACZ,MAAM,cAAc;EAAE,GAAG;EAAgB,GAAG;CAAO;CACnD,MAAM,SAAS,OAAO,OAAO;EAC3B,WAAW,YAAY;EACvB,SAAS,EAAE,WAAW,iBAAiB;CACzC,CAAC;CAED,OAAO,MAAM,EAAE,UAAU,YAAY,SAAS,GAAG,0BAA0B;CAE3E,IAAI,WAAW;CACf,IAAI,YAAmC;CAEvC,MAAM,OAAO,YAA2B;EACtC,IAAI,CAAC,UAAU;EAEf,IAAI;GACF,MAAM,SAAS,MAAM,gBAAgB;GACrC,OAAO,MAAM,EAAE,eAAe,OAAO,cAAc,GAAG,uBAAuB;GAE7E,IAAI,CAAC,OAAO,eAAe;IAEzB,OAAO,KAAK,gEAAgE;IAC5E,MAAM,KAAK,EAAE,MAAM,iBAAiB,CAAC;IAErC,IAAI,YAAY,kBACd,YAAY,iBAAiB;IAI/B;GACF;GAEA,IAAI,OAAO,MACT,MAAM,KAAK;IAAE,MAAM;IAAe,MAAM,OAAO;GAAK,CAAC;EAEzD,SAAS,OAAO;GAEd,MAAM,MAAM;GAIZ,IAFE,KAAK,SAAS,+BAA+B,KAAK,UAAU,iBAE1C;IAElB,OAAO,KAAK,qEAAqE;IACjF,MAAM,KAAK,EAAE,MAAM,iBAAiB,CAAC;IAErC,IAAI,YAAY,kBACd,YAAY,iBAAiB;IAI/B;GACF;GAGA,MAAM,eAAwC,EAC5C,SAAS,iBAAiB,QAAQ,MAAM,UAAU,gBACpD;GAGA,IAAI,SAAS,OAAO,UAAU,UAAU;IACtC,MAAM,aAAa;IACnB,IAAI,WAAW,MAAM,aAAa,OAAO,WAAW;IACpD,IAAI,WAAW,OAAO,aAAa,QAAQ,WAAW;IACtD,IAAI,WAAW,mBACb,aAAa,oBAAoB,WAAW;IAC9C,IAAI,WAAW,QAAQ,aAAa,SAAS,WAAW;GAC1D;GAEA,OAAO,MAAM,cAAc,uBAAuB;GAElD,IAAI,YAAY,WAAW,iBAAiB,OAC1C,YAAY,QAAQ,KAAK;EAE7B;EAGA,IAAI,UACF,YAAY,WAAW,MAAM,YAAY,QAAQ;CAErD;CAGA,YAAY,WAAW,MAAM,YAAY,QAAQ;CAGjD,aAAa;EACX,OAAO,MAAM,0BAA0B;EACvC,WAAW;EACX,IAAI,WAAW;GACb,aAAa,SAAS;GACtB,YAAY;EACd;CACF;AACF"}
@@ -1,81 +0,0 @@
1
- import { SessionUser, AuthStatus } from './types';
2
- /**
3
- * Authentication store context
4
- */
5
- export type AuthStoreContext = {
6
- user: SessionUser | null;
7
- isAdmin: boolean;
8
- isAuthenticated: boolean;
9
- error: string | null;
10
- lastChecked: Date | null;
11
- isInitializing: boolean;
12
- };
13
- /**
14
- * Configuration for the auth store
15
- */
16
- export type AuthStoreConfig = {
17
- /**
18
- * Function to check current authentication status
19
- */
20
- checkAuthStatus: () => Promise<AuthStatus>;
21
- /**
22
- * Optional configuration
23
- */
24
- config?: {
25
- /**
26
- * Role name that indicates admin access
27
- * @default 'Admin'
28
- */
29
- adminRole?: string;
30
- /**
31
- * Namespace for logger
32
- * @default 'fides-auth-next:store'
33
- */
34
- loggerNamespace?: string;
35
- };
36
- };
37
- /**
38
- * Creates an authentication store with XState Store
39
- *
40
- * @param options - Configuration for the auth store
41
- * @returns Configured XState store
42
- *
43
- * @example
44
- * ```typescript
45
- * const authStore = createAuthStore({
46
- * checkAuthStatus: async () => getAuthStatus(),
47
- * config: {
48
- * adminRole: 'Admin',
49
- * loggerNamespace: 'myapp:auth',
50
- * }
51
- * });
52
- * ```
53
- */
54
- export declare function createAuthStore(options: AuthStoreConfig): import('@xstate/store').Store<AuthStoreContext, {
55
- initialize: unknown;
56
- authSuccess: {
57
- user: SessionUser;
58
- };
59
- authFailed: {
60
- error?: string;
61
- } | undefined;
62
- sessionExpired: unknown;
63
- logout: unknown;
64
- loginSuccess: {
65
- user: SessionUser;
66
- };
67
- updateLastChecked: unknown;
68
- setError: {
69
- error: string;
70
- };
71
- clearError: unknown;
72
- }, never>;
73
- /**
74
- * Helper function to initialize auth by checking status
75
- */
76
- export declare function initializeAuth(store: ReturnType<typeof createAuthStore>, checkAuthStatus: () => Promise<AuthStatus>): Promise<void>;
77
- /**
78
- * Helper function to check auth status and update store
79
- */
80
- export declare function checkAuth(store: ReturnType<typeof createAuthStore>, checkAuthStatus: () => Promise<AuthStatus>): Promise<void>;
81
- //# sourceMappingURL=store.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"store.d.ts","sourceRoot":"","sources":["../../src/store/store.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAEvD;;GAEG;AACH,MAAM,MAAM,gBAAgB,GAAG;IAC7B,IAAI,EAAE,WAAW,GAAG,IAAI,CAAC;IACzB,OAAO,EAAE,OAAO,CAAC;IACjB,eAAe,EAAE,OAAO,CAAC;IACzB,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,WAAW,EAAE,IAAI,GAAG,IAAI,CAAC;IACzB,cAAc,EAAE,OAAO,CAAC;CACzB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,eAAe,GAAG;IAC5B;;OAEG;IACH,eAAe,EAAE,MAAM,OAAO,CAAC,UAAU,CAAC,CAAC;IAE3C;;OAEG;IACH,MAAM,CAAC,EAAE;QACP;;;WAGG;QACH,SAAS,CAAC,EAAE,MAAM,CAAC;QAEnB;;;WAGG;QACH,eAAe,CAAC,EAAE,MAAM,CAAC;KAC1B,CAAC;CACH,CAAC;AAUF;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,eAAe,CAAC,OAAO,EAAE,eAAe;;;cAyCS,WAAW;;;gBAeV,MAAM;;;;;cA6CN,WAAW;;;;eAuBf,MAAM;;;UAiBnE;AAED;;GAEG;AACH,wBAAsB,cAAc,CAClC,KAAK,EAAE,UAAU,CAAC,OAAO,eAAe,CAAC,EACzC,eAAe,EAAE,MAAM,OAAO,CAAC,UAAU,CAAC,iBAuB3C;AAED;;GAEG;AACH,wBAAsB,SAAS,CAC7B,KAAK,EAAE,UAAU,CAAC,OAAO,eAAe,CAAC,EACzC,eAAe,EAAE,MAAM,OAAO,CAAC,UAAU,CAAC,iBAqD3C"}