@eventuras/fides-auth-next 0.1.5
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 +71 -0
- package/dist/cookies.d.ts +143 -0
- package/dist/cookies.d.ts.map +1 -0
- package/dist/cookies.js +81 -0
- package/dist/fides-auth/dist/build-CNL3v39v.js +977 -0
- package/dist/fides-auth/dist/decode_jwt-1J26fl4I.js +25 -0
- package/dist/fides-auth/dist/decrypt-Cahlu_6Y.js +88 -0
- package/dist/fides-auth/dist/deflate-koSuX7FB.js +969 -0
- package/dist/fides-auth/dist/logger.js +46 -0
- package/dist/fides-auth/dist/oauth.js +165 -0
- package/dist/fides-auth/dist/rate-limit.js +26 -0
- package/dist/fides-auth/dist/session-refresh.js +27 -0
- package/dist/fides-auth/dist/session-validation-BxObT3wC.js +64 -0
- package/dist/fides-auth/dist/silent-login.js +50 -0
- package/dist/fides-auth/dist/utils-ByMRF7b2.js +379 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +18 -0
- package/dist/oidc-callback.d.ts +46 -0
- package/dist/oidc-callback.d.ts.map +1 -0
- package/dist/oidc-callback.js +33 -0
- package/dist/oidc-login.d.ts +26 -0
- package/dist/oidc-login.d.ts.map +1 -0
- package/dist/oidc-login.js +20 -0
- package/dist/request.d.ts +3 -0
- package/dist/request.d.ts.map +1 -0
- package/dist/request.js +26 -0
- package/dist/session.d.ts +111 -0
- package/dist/session.d.ts.map +1 -0
- package/dist/session.js +61 -0
- package/dist/store/configure-logger.d.ts +15 -0
- package/dist/store/configure-logger.d.ts.map +1 -0
- package/dist/store/configure-logger.js +10 -0
- package/dist/store/hooks.d.ts +64 -0
- package/dist/store/hooks.d.ts.map +1 -0
- package/dist/store/hooks.js +36 -0
- package/dist/store/index.d.ts +107 -0
- package/dist/store/index.d.ts.map +1 -0
- package/dist/store/index.js +6 -0
- package/dist/store/session-monitor.d.ts +51 -0
- package/dist/store/session-monitor.d.ts.map +1 -0
- package/dist/store/session-monitor.js +49 -0
- package/dist/store/store.d.ts +83 -0
- package/dist/store/store.d.ts.map +1 -0
- package/dist/store/store.js +134 -0
- package/dist/store/use-session-monitor.d.ts +28 -0
- package/dist/store/use-session-monitor.d.ts.map +1 -0
- package/dist/store/use-session-monitor.js +13 -0
- package/package.json +60 -0
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { Session, CreateSessionOptions } from '@eventuras/fides-auth/types';
|
|
2
|
+
import { OAuthConfig } from '@eventuras/fides-auth/oauth';
|
|
3
|
+
/**
|
|
4
|
+
* Creates an encrypted JWT containing session data.
|
|
5
|
+
*
|
|
6
|
+
* @param session - Session data (tokens, user, etc.)
|
|
7
|
+
* @param options - Configuration options (e.g., sessionDurationDays)
|
|
8
|
+
* @returns Encrypted JWT string
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* const jwt = await createSession({
|
|
13
|
+
* tokens: {
|
|
14
|
+
* accessToken: 'token',
|
|
15
|
+
* refreshToken: 'refresh',
|
|
16
|
+
* },
|
|
17
|
+
* user: {
|
|
18
|
+
* name: 'John Doe',
|
|
19
|
+
* email: 'john@example.com',
|
|
20
|
+
* }
|
|
21
|
+
* });
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export declare function createSession<TData = Record<string, unknown>>(session: Session<TData>, options?: CreateSessionOptions): Promise<string>;
|
|
25
|
+
/**
|
|
26
|
+
* Retrieves the current session from the "session" cookie, if any.
|
|
27
|
+
* Uses React server components' cache for performance.
|
|
28
|
+
*
|
|
29
|
+
* This function:
|
|
30
|
+
* 1. Gets the session cookie
|
|
31
|
+
* 2. Validates the JWT
|
|
32
|
+
* 3. Returns the session if valid, null otherwise
|
|
33
|
+
*
|
|
34
|
+
* @param config - Optional OAuth config (currently unused, kept for compatibility)
|
|
35
|
+
* @returns Session object or null if no valid session exists
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```ts
|
|
39
|
+
* // In a server component or server action
|
|
40
|
+
* const session = await getCurrentSession();
|
|
41
|
+
* if (session) {
|
|
42
|
+
* console.log('User:', session.user);
|
|
43
|
+
* } else {
|
|
44
|
+
* // User not logged in
|
|
45
|
+
* }
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
export declare const getCurrentSession: (config?: OAuthConfig) => Promise<Session<any> | null>;
|
|
49
|
+
/**
|
|
50
|
+
* Refreshes the current session using the refresh token.
|
|
51
|
+
*
|
|
52
|
+
* This function:
|
|
53
|
+
* 1. Gets the current session
|
|
54
|
+
* 2. Uses the refresh token to get new access tokens
|
|
55
|
+
* 3. Updates the session cookie with new tokens
|
|
56
|
+
* 4. Returns the updated session
|
|
57
|
+
*
|
|
58
|
+
* @param config - OAuth configuration
|
|
59
|
+
* @param options - Session creation options
|
|
60
|
+
* @returns Updated session or null if refresh failed
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```ts
|
|
64
|
+
* const updatedSession = await refreshCurrentSession(oauthConfig);
|
|
65
|
+
* if (updatedSession) {
|
|
66
|
+
* // Session refreshed successfully
|
|
67
|
+
* } else {
|
|
68
|
+
* // Refresh failed, user needs to log in again
|
|
69
|
+
* redirect('/login');
|
|
70
|
+
* }
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
73
|
+
export declare function refreshCurrentSession(config: OAuthConfig, options?: CreateSessionOptions): Promise<Session | null>;
|
|
74
|
+
/**
|
|
75
|
+
* Creates and persists a new session.
|
|
76
|
+
*
|
|
77
|
+
* This is a convenience function that combines createSession and setSessionCookie.
|
|
78
|
+
* Use this after successful authentication to establish a user session.
|
|
79
|
+
*
|
|
80
|
+
* @param session - Session data
|
|
81
|
+
* @param options - Session creation options
|
|
82
|
+
* @returns The encrypted JWT that was stored in the cookie
|
|
83
|
+
*
|
|
84
|
+
* @example
|
|
85
|
+
* ```ts
|
|
86
|
+
* // After successful OAuth callback
|
|
87
|
+
* await createAndPersistSession({
|
|
88
|
+
* tokens: {
|
|
89
|
+
* accessToken: tokens.access_token,
|
|
90
|
+
* refreshToken: tokens.refresh_token,
|
|
91
|
+
* },
|
|
92
|
+
* user: {
|
|
93
|
+
* name: 'John Doe',
|
|
94
|
+
* email: 'john@example.com',
|
|
95
|
+
* }
|
|
96
|
+
* }, { sessionDurationDays: 30 });
|
|
97
|
+
* ```
|
|
98
|
+
*/
|
|
99
|
+
export declare function createAndPersistSession(session: Session, options?: CreateSessionOptions): Promise<string>;
|
|
100
|
+
/**
|
|
101
|
+
* Clears the current session by deleting the session cookie.
|
|
102
|
+
* Use this when logging out or when session validation fails.
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* ```ts
|
|
106
|
+
* await clearCurrentSession();
|
|
107
|
+
* redirect('/');
|
|
108
|
+
* ```
|
|
109
|
+
*/
|
|
110
|
+
export declare function clearCurrentSession(): Promise<void>;
|
|
111
|
+
//# sourceMappingURL=session.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session.d.ts","sourceRoot":"","sources":["../src/session.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,OAAO,EAAE,oBAAoB,EAAE,MAAM,6BAA6B,CAAC;AACjF,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,6BAA6B,CAAC;AAO/D;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAsB,aAAa,CAAC,KAAK,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACjE,OAAO,EAAE,OAAO,CAAC,KAAK,CAAC,EACvB,OAAO,GAAE,oBAAyB,GACjC,OAAO,CAAC,MAAM,CAAC,CAejB;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,eAAO,MAAM,iBAAiB,YAAyB,WAAW,KAAG,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,IAAI,CAkC9F,CAAC;AAEH;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAsB,qBAAqB,CACzC,MAAM,EAAE,WAAW,EACnB,OAAO,GAAE,oBAAyB,GACjC,OAAO,CAAC,OAAO,GAAG,IAAI,CAAC,CA+CzB;AAED;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAsB,uBAAuB,CAC3C,OAAO,EAAE,OAAO,EAChB,OAAO,GAAE,oBAAyB,GACjC,OAAO,CAAC,MAAM,CAAC,CAajB;AAED;;;;;;;;;GASG;AACH,wBAAsB,mBAAmB,IAAI,OAAO,CAAC,IAAI,CAAC,CAUzD"}
|
package/dist/session.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { V as e, q as t } from "./fides-auth/dist/utils-ByMRF7b2.js";
|
|
2
|
+
import { c as n } from "./fides-auth/dist/session-validation-BxObT3wC.js";
|
|
3
|
+
import { r } from "./fides-auth/dist/session-refresh.js";
|
|
4
|
+
import { deleteSessionCookie as i, getSessionCookie as a, setSessionCookie as o } from "./cookies.js";
|
|
5
|
+
import { Logger as s } from "@eventuras/logger";
|
|
6
|
+
import { cache as c } from "react";
|
|
7
|
+
//#region src/session.ts
|
|
8
|
+
var l = s.create({ namespace: "fides-auth-next:session" });
|
|
9
|
+
async function u(n, r = {}) {
|
|
10
|
+
l.debug("Creating new session");
|
|
11
|
+
let { sessionDurationDays: i = 7 } = r;
|
|
12
|
+
try {
|
|
13
|
+
let r = await e({ ...n }, t());
|
|
14
|
+
return l.info({ sessionDurationDays: i }, "Session created successfully"), r;
|
|
15
|
+
} catch (e) {
|
|
16
|
+
throw l.error({ error: e }, "Failed to create session"), e;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
var d = c(async (e) => {
|
|
20
|
+
l.debug("Retrieving current session");
|
|
21
|
+
try {
|
|
22
|
+
let e = await a();
|
|
23
|
+
if (!e) return l.debug("No session cookie found"), null;
|
|
24
|
+
let { status: r, session: i } = await n(e, t());
|
|
25
|
+
return r === "VALID" ? i ? (l.debug("Current session retrieved successfully"), i) : (l.warn("Session validation succeeded but no session object returned"), null) : (l.warn({ status: r }, "Session validation failed"), null);
|
|
26
|
+
} catch (e) {
|
|
27
|
+
return e instanceof Error && e.message.includes("worker") ? l.error({ error: e }, "Worker thread error in getCurrentSession") : l.error({ error: e }, "Unexpected error in getCurrentSession"), null;
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
async function f(e, t = {}) {
|
|
31
|
+
l.info("Starting session refresh");
|
|
32
|
+
try {
|
|
33
|
+
let n = await d();
|
|
34
|
+
if (!n) return l.warn("No current session to refresh"), null;
|
|
35
|
+
if (!n.tokens?.refreshToken) return l.error("Current session has no refresh token"), null;
|
|
36
|
+
let i = await r(n, e, t);
|
|
37
|
+
return i ? (await o(await u(i, t)), l.info("Session refreshed and cookie updated successfully"), i) : (l.error("Session refresh returned null"), null);
|
|
38
|
+
} catch (e) {
|
|
39
|
+
let t = e;
|
|
40
|
+
return t?.code === "OAUTH_RESPONSE_BODY_ERROR" && t?.error === "invalid_grant" ? l.info("Session refresh failed - refresh token expired or invalid") : l.error({ error: e }, "Failed to refresh current session"), null;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
async function p(e, t = {}) {
|
|
44
|
+
l.info("Creating and persisting new session");
|
|
45
|
+
try {
|
|
46
|
+
let n = await u(e, t);
|
|
47
|
+
return await o(n), l.info("Session created and persisted successfully"), n;
|
|
48
|
+
} catch (e) {
|
|
49
|
+
throw l.error({ error: e }, "Failed to create and persist session"), e;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
async function m() {
|
|
53
|
+
l.info("Clearing current session");
|
|
54
|
+
try {
|
|
55
|
+
await i(), l.info("Session cleared successfully");
|
|
56
|
+
} catch (e) {
|
|
57
|
+
throw l.error({ error: e }, "Failed to clear session"), e;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
//#endregion
|
|
61
|
+
export { m as clearCurrentSession, p as createAndPersistSession, u as createSession, d as getCurrentSession, f as refreshCurrentSession };
|
|
@@ -0,0 +1,15 @@
|
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
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"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { r as e } from "../fides-auth/dist/logger.js";
|
|
2
|
+
import { Logger as t } from "@eventuras/logger";
|
|
3
|
+
//#region src/store/configure-logger.ts
|
|
4
|
+
function n() {
|
|
5
|
+
e({ create(e) {
|
|
6
|
+
return t.create(e);
|
|
7
|
+
} });
|
|
8
|
+
}
|
|
9
|
+
//#endregion
|
|
10
|
+
export { n as configureAuthLogger };
|
|
@@ -0,0 +1,64 @@
|
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
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"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { useSelector as e } from "@xstate/store/react";
|
|
2
|
+
//#region src/store/hooks.ts
|
|
3
|
+
function t(t) {
|
|
4
|
+
function n() {
|
|
5
|
+
return e(t, (e) => e).context;
|
|
6
|
+
}
|
|
7
|
+
function r() {
|
|
8
|
+
return {
|
|
9
|
+
loginSuccess: (e) => t.send({
|
|
10
|
+
type: "loginSuccess",
|
|
11
|
+
user: e
|
|
12
|
+
}),
|
|
13
|
+
logout: () => t.send({ type: "logout" }),
|
|
14
|
+
sessionExpired: () => t.send({ type: "sessionExpired" }),
|
|
15
|
+
authSuccess: (e) => t.send({
|
|
16
|
+
type: "authSuccess",
|
|
17
|
+
user: e
|
|
18
|
+
}),
|
|
19
|
+
authFailed: (e) => t.send({
|
|
20
|
+
type: "authFailed",
|
|
21
|
+
error: e
|
|
22
|
+
}),
|
|
23
|
+
setError: (e) => t.send({
|
|
24
|
+
type: "setError",
|
|
25
|
+
error: e
|
|
26
|
+
}),
|
|
27
|
+
clearError: () => t.send({ type: "clearError" })
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
return {
|
|
31
|
+
useAuthStore: n,
|
|
32
|
+
useAuthActions: r
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
//#endregion
|
|
36
|
+
export { t as createAuthStoreHooks };
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Authentication Store for Next.js
|
|
3
|
+
*
|
|
4
|
+
* This package provides XState Store-based authentication state management:
|
|
5
|
+
*
|
|
6
|
+
* - Simpler data-focused state management
|
|
7
|
+
* - Easier to understand and maintain
|
|
8
|
+
* - Perfect for authentication data and status
|
|
9
|
+
* - Apps can build complex workflows on top if needed
|
|
10
|
+
*
|
|
11
|
+
* ## Usage
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* import { createAuthStore, createAuthStoreHooks, initializeAuth, startSessionMonitor } from '@eventuras/fides-auth-next/store';
|
|
16
|
+
* import { getAuthStatus } from './auth/getAuthStatus';
|
|
17
|
+
*
|
|
18
|
+
* // Create the store
|
|
19
|
+
* export const authStore = createAuthStore({
|
|
20
|
+
* checkAuthStatus: getAuthStatus,
|
|
21
|
+
* config: {
|
|
22
|
+
* adminRole: 'Admin',
|
|
23
|
+
* loggerNamespace: 'myapp:auth',
|
|
24
|
+
* }
|
|
25
|
+
* });
|
|
26
|
+
*
|
|
27
|
+
* // Create hooks
|
|
28
|
+
* export const { useAuthStore, useAuthActions } = createAuthStoreHooks(authStore);
|
|
29
|
+
*
|
|
30
|
+
* // In your root component
|
|
31
|
+
* function App() {
|
|
32
|
+
* useEffect(() => {
|
|
33
|
+
* // Initialize auth on mount
|
|
34
|
+
* initializeAuth(authStore, getAuthStatus);
|
|
35
|
+
*
|
|
36
|
+
* // Start session monitoring
|
|
37
|
+
* return startSessionMonitor(authStore, getAuthStatus, {
|
|
38
|
+
* interval: 30_000,
|
|
39
|
+
* onSessionExpired: () => {
|
|
40
|
+
* toast.warn('Your session has expired');
|
|
41
|
+
* }
|
|
42
|
+
* });
|
|
43
|
+
* }, []);
|
|
44
|
+
*
|
|
45
|
+
* return <div>...</div>;
|
|
46
|
+
* }
|
|
47
|
+
*
|
|
48
|
+
* // In any component
|
|
49
|
+
* function MyComponent() {
|
|
50
|
+
* const auth = useAuthStore();
|
|
51
|
+
* const { logout } = useAuthActions();
|
|
52
|
+
*
|
|
53
|
+
* if (auth.isInitializing) return <Spinner />;
|
|
54
|
+
* if (!auth.isAuthenticated) return <LoginButton />;
|
|
55
|
+
*
|
|
56
|
+
* return (
|
|
57
|
+
* <div>
|
|
58
|
+
* Welcome, {auth.user?.name}!
|
|
59
|
+
* <button onClick={logout}>Logout</button>
|
|
60
|
+
* </div>
|
|
61
|
+
* );
|
|
62
|
+
* }
|
|
63
|
+
* ```
|
|
64
|
+
*
|
|
65
|
+
* ## Building Complex Workflows
|
|
66
|
+
*
|
|
67
|
+
* You can use the Store for data management and build app-specific
|
|
68
|
+
* state machines that consume the auth store for complex workflows:
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* ```typescript
|
|
72
|
+
* import { createMachine } from 'xstate';
|
|
73
|
+
* import { authStore } from './auth-store';
|
|
74
|
+
*
|
|
75
|
+
* const checkoutMachine = createMachine({
|
|
76
|
+
* context: ({ input }) => ({
|
|
77
|
+
* authStore: input.authStore,
|
|
78
|
+
* cart: [],
|
|
79
|
+
* }),
|
|
80
|
+
* states: {
|
|
81
|
+
* idle: {
|
|
82
|
+
* on: {
|
|
83
|
+
* START_CHECKOUT: {
|
|
84
|
+
* target: 'checkingAuth',
|
|
85
|
+
* }
|
|
86
|
+
* }
|
|
87
|
+
* },
|
|
88
|
+
* checkingAuth: {
|
|
89
|
+
* entry: ({ context }) => {
|
|
90
|
+
* const auth = context.authStore.getSnapshot().context;
|
|
91
|
+
* if (!auth.isAuthenticated) {
|
|
92
|
+
* // Trigger login flow
|
|
93
|
+
* }
|
|
94
|
+
* },
|
|
95
|
+
* // ... rest of workflow
|
|
96
|
+
* }
|
|
97
|
+
* }
|
|
98
|
+
* });
|
|
99
|
+
* ```
|
|
100
|
+
*/
|
|
101
|
+
export { createAuthStore, initializeAuth, checkAuth, type AuthStoreContext, type AuthStoreConfig, } from './store';
|
|
102
|
+
export { createAuthStoreHooks, type AuthStoreSelector, type AuthStoreActions, } from './hooks';
|
|
103
|
+
export { startSessionMonitor, type SessionMonitorConfig, } from './session-monitor';
|
|
104
|
+
export { useSessionMonitor } from './use-session-monitor';
|
|
105
|
+
export { configureAuthLogger } from './configure-logger';
|
|
106
|
+
export type { SessionUser, AuthStatus } from './types';
|
|
107
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/store/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmGG;AAGH,OAAO,EACL,eAAe,EACf,cAAc,EACd,SAAS,EACT,KAAK,gBAAgB,EACrB,KAAK,eAAe,GACrB,MAAM,SAAS,CAAC;AAEjB,OAAO,EACL,oBAAoB,EACpB,KAAK,iBAAiB,EACtB,KAAK,gBAAgB,GACtB,MAAM,SAAS,CAAC;AAEjB,OAAO,EACL,mBAAmB,EACnB,KAAK,oBAAoB,GAC1B,MAAM,mBAAmB,CAAC;AAE3B,OAAO,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAC;AAE1D,OAAO,EAAE,mBAAmB,EAAE,MAAM,oBAAoB,CAAC;AAGzD,YAAY,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { checkAuth as e, createAuthStore as t, initializeAuth as n } from "./store.js";
|
|
2
|
+
import { createAuthStoreHooks as r } from "./hooks.js";
|
|
3
|
+
import { startSessionMonitor as i } from "./session-monitor.js";
|
|
4
|
+
import { useSessionMonitor as a } from "./use-session-monitor.js";
|
|
5
|
+
import { configureAuthLogger as o } from "./configure-logger.js";
|
|
6
|
+
export { e as checkAuth, o as configureAuthLogger, t as createAuthStore, r as createAuthStoreHooks, n as initializeAuth, i as startSessionMonitor, a as useSessionMonitor };
|
|
@@ -0,0 +1,51 @@
|
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
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"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { Logger as e } from "@eventuras/logger";
|
|
2
|
+
//#region src/store/session-monitor.ts
|
|
3
|
+
var t = {
|
|
4
|
+
interval: 3e4,
|
|
5
|
+
loggerNamespace: "fides:session-monitor"
|
|
6
|
+
};
|
|
7
|
+
function n(n, r, i = {}) {
|
|
8
|
+
let a = {
|
|
9
|
+
...t,
|
|
10
|
+
...i
|
|
11
|
+
}, o = e.create({
|
|
12
|
+
namespace: a.loggerNamespace,
|
|
13
|
+
context: { component: "SessionMonitor" }
|
|
14
|
+
});
|
|
15
|
+
o.debug({ interval: a.interval }, "Starting session monitor");
|
|
16
|
+
let s = !0, c = null, l = async () => {
|
|
17
|
+
if (s) {
|
|
18
|
+
try {
|
|
19
|
+
let e = await r();
|
|
20
|
+
if (o.debug({ authenticated: e.authenticated }, "Session monitor check"), !e.authenticated) {
|
|
21
|
+
o.info("Session monitor detected unauthenticated state - session ended"), n.send({ type: "sessionExpired" }), a.onSessionExpired && a.onSessionExpired();
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
e.user && n.send({
|
|
25
|
+
type: "authSuccess",
|
|
26
|
+
user: e.user
|
|
27
|
+
});
|
|
28
|
+
} catch (e) {
|
|
29
|
+
let t = e;
|
|
30
|
+
if (t?.code === "OAUTH_RESPONSE_BODY_ERROR" && t?.error === "invalid_grant") {
|
|
31
|
+
o.info("Session monitor detected expired refresh token - user session ended"), n.send({ type: "sessionExpired" }), a.onSessionExpired && a.onSessionExpired();
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
let r = { message: e instanceof Error ? e.message : "Unknown error" };
|
|
35
|
+
if (e && typeof e == "object") {
|
|
36
|
+
let t = e;
|
|
37
|
+
t.code && (r.code = t.code), t.error && (r.error = t.error), t.error_description && (r.error_description = t.error_description), t.status && (r.status = t.status);
|
|
38
|
+
}
|
|
39
|
+
o.error(r, "Session monitor error"), a.onError && e instanceof Error && a.onError(e);
|
|
40
|
+
}
|
|
41
|
+
s && (c = setTimeout(l, a.interval));
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
return c = setTimeout(l, a.interval), () => {
|
|
45
|
+
o.debug("Stopping session monitor"), s = !1, c &&= (clearTimeout(c), null);
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
//#endregion
|
|
49
|
+
export { n as startSessionMonitor };
|
|
@@ -0,0 +1,83 @@
|
|
|
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
|
+
}, {
|
|
73
|
+
type: string;
|
|
74
|
+
}>;
|
|
75
|
+
/**
|
|
76
|
+
* Helper function to initialize auth by checking status
|
|
77
|
+
*/
|
|
78
|
+
export declare function initializeAuth(store: ReturnType<typeof createAuthStore>, checkAuthStatus: () => Promise<AuthStatus>): Promise<void>;
|
|
79
|
+
/**
|
|
80
|
+
* Helper function to check auth status and update store
|
|
81
|
+
*/
|
|
82
|
+
export declare function checkAuth(store: ReturnType<typeof createAuthStore>, checkAuthStatus: () => Promise<AuthStatus>): Promise<void>;
|
|
83
|
+
//# sourceMappingURL=store.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
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;;;;;GAiBnE;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"}
|