@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
package/README.md
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# @eventuras/fides-auth-next
|
|
2
|
+
|
|
3
|
+
Next.js authentication with XState Store state management.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @eventuras/fides-auth-next @xstate/store
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
// 1. Create store
|
|
15
|
+
import {
|
|
16
|
+
createAuthStore,
|
|
17
|
+
createAuthStoreHooks,
|
|
18
|
+
} from "@eventuras/fides-auth-next/store";
|
|
19
|
+
|
|
20
|
+
export const authStore = createAuthStore({
|
|
21
|
+
checkAuthStatus: getAuthStatus,
|
|
22
|
+
config: { adminRole: "Admin" },
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
export const { useAuthStore, useAuthActions } = createAuthStoreHooks(authStore);
|
|
26
|
+
|
|
27
|
+
// 2. Initialize in app
|
|
28
|
+
useEffect(() => {
|
|
29
|
+
initializeAuth(authStore, getAuthStatus);
|
|
30
|
+
return startSessionMonitor(authStore, getAuthStatus, { interval: 30_000 });
|
|
31
|
+
}, []);
|
|
32
|
+
|
|
33
|
+
// 3. Use in components
|
|
34
|
+
const { isAuthenticated, user, isAdmin } = useAuthStore();
|
|
35
|
+
const { logout } = useAuthActions();
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Features
|
|
39
|
+
|
|
40
|
+
- **XState Store**: Simple auth state management with automatic session monitoring
|
|
41
|
+
- **Cookie Helpers**: Server-side cookie operations
|
|
42
|
+
- **Session Management**: High-level session functions
|
|
43
|
+
- **OAuth**: Complete OAuth flow with PKCE
|
|
44
|
+
- **TypeScript**: Full type safety
|
|
45
|
+
|
|
46
|
+
## Core Functions
|
|
47
|
+
|
|
48
|
+
```typescript
|
|
49
|
+
// Server-side
|
|
50
|
+
import {
|
|
51
|
+
getCurrentSession,
|
|
52
|
+
createAndPersistSession,
|
|
53
|
+
clearCurrentSession,
|
|
54
|
+
setSessionCookie,
|
|
55
|
+
getSessionCookie,
|
|
56
|
+
} from "@eventuras/fides-auth-next";
|
|
57
|
+
|
|
58
|
+
// Client-side
|
|
59
|
+
import { useAuthStore, useAuthActions } from "./auth-store";
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## Environment
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
SESSION_SECRET=your-32-byte-hex-secret # openssl rand -hex 32
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## Documentation
|
|
69
|
+
|
|
70
|
+
- [Store API](./src/store/) - XState Store details
|
|
71
|
+
- [Setup Guide](./SETUP_GUIDE.md) - Complete walkthrough
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cookie helpers for Next.js authentication flows.
|
|
3
|
+
*
|
|
4
|
+
* IMPORTANT: These functions can ONLY be used in:
|
|
5
|
+
* - Server Actions (functions marked with 'use server')
|
|
6
|
+
* - Route Handlers (files in app/api directory)
|
|
7
|
+
* - Server Components (async React components)
|
|
8
|
+
*
|
|
9
|
+
* They CANNOT be used in:
|
|
10
|
+
* - Client Components (marked with 'use client')
|
|
11
|
+
* - Middleware (use NextResponse.cookies instead)
|
|
12
|
+
* - Edge Runtime without proper configuration
|
|
13
|
+
*/
|
|
14
|
+
export interface CookieOptions {
|
|
15
|
+
/** Cookie path (default: '/') */
|
|
16
|
+
path?: string;
|
|
17
|
+
/** Max age in seconds */
|
|
18
|
+
maxAge?: number;
|
|
19
|
+
/** SameSite policy (default: 'lax') */
|
|
20
|
+
sameSite?: 'strict' | 'lax' | 'none';
|
|
21
|
+
/** HTTP only flag (default: true) */
|
|
22
|
+
httpOnly?: boolean;
|
|
23
|
+
/** Secure flag (default: true in production) */
|
|
24
|
+
secure?: boolean;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Default cookie options for session cookies.
|
|
28
|
+
* - Secure in production
|
|
29
|
+
* - HTTP only
|
|
30
|
+
* - Lax same-site policy
|
|
31
|
+
* - 30 days max age
|
|
32
|
+
*/
|
|
33
|
+
export declare const defaultSessionCookieOptions: CookieOptions;
|
|
34
|
+
/**
|
|
35
|
+
* Default cookie options for OAuth state/PKCE cookies.
|
|
36
|
+
* - Secure in production
|
|
37
|
+
* - HTTP only
|
|
38
|
+
* - Lax same-site policy
|
|
39
|
+
* - 10 minutes max age (short-lived for security)
|
|
40
|
+
*/
|
|
41
|
+
export declare const defaultOAuthCookieOptions: CookieOptions;
|
|
42
|
+
/**
|
|
43
|
+
* Sets a cookie with the given name and value.
|
|
44
|
+
*
|
|
45
|
+
* @param name - Cookie name
|
|
46
|
+
* @param value - Cookie value
|
|
47
|
+
* @param options - Cookie options (defaults to session cookie options)
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```ts
|
|
51
|
+
* // In a server action or route handler
|
|
52
|
+
* await setAuthCookie('session', encryptedJwt);
|
|
53
|
+
*
|
|
54
|
+
* // With custom options
|
|
55
|
+
* await setAuthCookie('oauth_state', state, {
|
|
56
|
+
* maxAge: 60 * 10, // 10 minutes
|
|
57
|
+
* });
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
export declare function setAuthCookie(name: string, value: string, options?: CookieOptions): Promise<void>;
|
|
61
|
+
/**
|
|
62
|
+
* Gets a cookie value by name.
|
|
63
|
+
*
|
|
64
|
+
* @param name - Cookie name
|
|
65
|
+
* @returns Cookie value or null if not found
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* ```ts
|
|
69
|
+
* // In a server action or route handler
|
|
70
|
+
* const sessionCookie = await getAuthCookie('session');
|
|
71
|
+
* if (sessionCookie) {
|
|
72
|
+
* // Validate and use session
|
|
73
|
+
* }
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
export declare function getAuthCookie(name: string): Promise<string | null>;
|
|
77
|
+
/**
|
|
78
|
+
* Deletes a cookie by name.
|
|
79
|
+
*
|
|
80
|
+
* @param name - Cookie name to delete
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* ```ts
|
|
84
|
+
* // In a server action or route handler
|
|
85
|
+
* await deleteAuthCookie('session');
|
|
86
|
+
* ```
|
|
87
|
+
*/
|
|
88
|
+
export declare function deleteAuthCookie(name: string): Promise<void>;
|
|
89
|
+
/**
|
|
90
|
+
* Deletes multiple cookies at once.
|
|
91
|
+
* Useful for cleaning up OAuth flow cookies.
|
|
92
|
+
*
|
|
93
|
+
* @param names - Array of cookie names to delete
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* ```ts
|
|
97
|
+
* // Clean up OAuth cookies after callback
|
|
98
|
+
* await deleteAuthCookies(['oauth_state', 'oauth_code_verifier', 'returnTo']);
|
|
99
|
+
* ```
|
|
100
|
+
*/
|
|
101
|
+
export declare function deleteAuthCookies(names: string[]): Promise<void>;
|
|
102
|
+
/**
|
|
103
|
+
* Sets the session cookie with proper security settings.
|
|
104
|
+
* This is a convenience wrapper around setAuthCookie.
|
|
105
|
+
*
|
|
106
|
+
* @param encryptedJwt - The encrypted JWT session token
|
|
107
|
+
* @param options - Optional cookie options (merged with defaults)
|
|
108
|
+
*
|
|
109
|
+
* @example
|
|
110
|
+
* ```ts
|
|
111
|
+
* const jwt = await createSession({ ... });
|
|
112
|
+
* await setSessionCookie(jwt);
|
|
113
|
+
* ```
|
|
114
|
+
*/
|
|
115
|
+
export declare function setSessionCookie(encryptedJwt: string, options?: Partial<CookieOptions>): Promise<void>;
|
|
116
|
+
/**
|
|
117
|
+
* Gets the current session cookie value.
|
|
118
|
+
* Returns null if no session cookie exists.
|
|
119
|
+
*
|
|
120
|
+
* @returns Encrypted JWT session token or null
|
|
121
|
+
*
|
|
122
|
+
* @example
|
|
123
|
+
* ```ts
|
|
124
|
+
* const sessionToken = await getSessionCookie();
|
|
125
|
+
* if (sessionToken) {
|
|
126
|
+
* const { status, session } = await validateSessionJwt(sessionToken);
|
|
127
|
+
* // ...
|
|
128
|
+
* }
|
|
129
|
+
* ```
|
|
130
|
+
*/
|
|
131
|
+
export declare function getSessionCookie(): Promise<string | null>;
|
|
132
|
+
/**
|
|
133
|
+
* Deletes the session cookie.
|
|
134
|
+
* Use this when logging out or when session validation fails.
|
|
135
|
+
*
|
|
136
|
+
* @example
|
|
137
|
+
* ```ts
|
|
138
|
+
* await deleteSessionCookie();
|
|
139
|
+
* redirect('/');
|
|
140
|
+
* ```
|
|
141
|
+
*/
|
|
142
|
+
export declare function deleteSessionCookie(): Promise<void>;
|
|
143
|
+
//# sourceMappingURL=cookies.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cookies.d.ts","sourceRoot":"","sources":["../src/cookies.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAOH,MAAM,WAAW,aAAa;IAC5B,iCAAiC;IACjC,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,yBAAyB;IACzB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,uCAAuC;IACvC,QAAQ,CAAC,EAAE,QAAQ,GAAG,KAAK,GAAG,MAAM,CAAC;IACrC,qCAAqC;IACrC,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,gDAAgD;IAChD,MAAM,CAAC,EAAE,OAAO,CAAC;CAClB;AAED;;;;;;GAMG;AACH,eAAO,MAAM,2BAA2B,EAAE,aAMzC,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,yBAAyB,EAAE,aAMvC,CAAC;AAEF;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAsB,aAAa,CACjC,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,MAAM,EACb,OAAO,GAAE,aAA2C,GACnD,OAAO,CAAC,IAAI,CAAC,CAgBf;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAsB,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAgBxE;AAED;;;;;;;;;;GAUG;AACH,wBAAsB,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAUlE;AAED;;;;;;;;;;;GAWG;AACH,wBAAsB,iBAAiB,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAatE;AAED;;;;;;;;;;;;GAYG;AACH,wBAAsB,gBAAgB,CACpC,YAAY,EAAE,MAAM,EACpB,OAAO,GAAE,OAAO,CAAC,aAAa,CAAM,GACnC,OAAO,CAAC,IAAI,CAAC,CAOf;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAsB,gBAAgB,IAAI,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAE/D;AAED;;;;;;;;;GASG;AACH,wBAAsB,mBAAmB,IAAI,OAAO,CAAC,IAAI,CAAC,CAGzD"}
|
package/dist/cookies.js
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { cookies as e } from "next/headers";
|
|
2
|
+
import { Logger as t } from "@eventuras/logger";
|
|
3
|
+
//#region src/cookies.ts
|
|
4
|
+
var n = t.create({ namespace: "fides-auth-next:cookies" }), r = {
|
|
5
|
+
path: "/",
|
|
6
|
+
maxAge: 3600 * 24 * 30,
|
|
7
|
+
sameSite: "lax",
|
|
8
|
+
httpOnly: !0,
|
|
9
|
+
secure: process.env.NODE_ENV === "production"
|
|
10
|
+
}, i = {
|
|
11
|
+
path: "/",
|
|
12
|
+
maxAge: 600,
|
|
13
|
+
sameSite: "lax",
|
|
14
|
+
httpOnly: !0,
|
|
15
|
+
secure: process.env.NODE_ENV === "production"
|
|
16
|
+
};
|
|
17
|
+
async function a(t, i, a = r) {
|
|
18
|
+
try {
|
|
19
|
+
let o = await e(), s = {
|
|
20
|
+
...r,
|
|
21
|
+
...a
|
|
22
|
+
};
|
|
23
|
+
o.set(t, i, s), n.debug({
|
|
24
|
+
cookieName: t,
|
|
25
|
+
maxAge: s.maxAge,
|
|
26
|
+
secure: s.secure
|
|
27
|
+
}, "Cookie set successfully");
|
|
28
|
+
} catch (e) {
|
|
29
|
+
throw n.error({
|
|
30
|
+
error: e,
|
|
31
|
+
cookieName: t
|
|
32
|
+
}, "Failed to set cookie"), e;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
async function o(t) {
|
|
36
|
+
try {
|
|
37
|
+
let r = (await e()).get(t);
|
|
38
|
+
return r?.value ? (n.debug({ cookieName: t }, "Cookie retrieved"), r.value) : (n.debug({ cookieName: t }, "Cookie not found"), null);
|
|
39
|
+
} catch (e) {
|
|
40
|
+
return n.error({
|
|
41
|
+
error: e,
|
|
42
|
+
cookieName: t
|
|
43
|
+
}, "Failed to get cookie"), null;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
async function s(t) {
|
|
47
|
+
try {
|
|
48
|
+
(await e()).delete(t), n.debug({ cookieName: t }, "Cookie deleted successfully");
|
|
49
|
+
} catch (e) {
|
|
50
|
+
throw n.error({
|
|
51
|
+
error: e,
|
|
52
|
+
cookieName: t
|
|
53
|
+
}, "Failed to delete cookie"), e;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
async function c(t) {
|
|
57
|
+
try {
|
|
58
|
+
let r = await e();
|
|
59
|
+
for (let e of t) r.delete(e);
|
|
60
|
+
n.debug({ cookieNames: t }, "Multiple cookies deleted successfully");
|
|
61
|
+
} catch (e) {
|
|
62
|
+
throw n.error({
|
|
63
|
+
error: e,
|
|
64
|
+
cookieNames: t
|
|
65
|
+
}, "Failed to delete cookies"), e;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
async function l(e, t = {}) {
|
|
69
|
+
await a("session", e, {
|
|
70
|
+
...r,
|
|
71
|
+
...t
|
|
72
|
+
}), n.info("Session cookie set");
|
|
73
|
+
}
|
|
74
|
+
async function u() {
|
|
75
|
+
return await o("session");
|
|
76
|
+
}
|
|
77
|
+
async function d() {
|
|
78
|
+
await s("session"), n.info("Session cookie deleted");
|
|
79
|
+
}
|
|
80
|
+
//#endregion
|
|
81
|
+
export { i as defaultOAuthCookieOptions, r as defaultSessionCookieOptions, s as deleteAuthCookie, c as deleteAuthCookies, d as deleteSessionCookie, o as getAuthCookie, u as getSessionCookie, a as setAuthCookie, l as setSessionCookie };
|