mulguard 1.1.6 → 1.1.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/README.md +210 -706
- package/dist/actions-CMtg7FGv.js +1 -0
- package/dist/{actions-DeCfLtHA.mjs → actions-CjQUKaXF.mjs} +54 -38
- package/dist/client/index.js +1 -1
- package/dist/client/index.mjs +84 -78
- package/dist/core/auth/email-password.d.ts +145 -0
- package/dist/core/auth/oauth/index.d.ts +14 -0
- package/dist/core/auth/oauth/oauth-handler.d.ts +172 -0
- package/dist/core/auth/oauth/pkce.d.ts +168 -0
- package/dist/core/auth/{oauth-providers.d.ts → oauth/providers.d.ts} +8 -7
- package/dist/core/auth/{oauth-state-store-cookie.d.ts → oauth/state-store-cookie.d.ts} +4 -4
- package/dist/core/auth/{oauth-state-store-redis.d.ts → oauth/state-store-redis.d.ts} +1 -1
- package/dist/core/auth/{oauth-state-store.d.ts → oauth/state-store.d.ts} +4 -1
- package/dist/core/auth/otp.d.ts +184 -0
- package/dist/core/errors/index.d.ts +269 -0
- package/dist/core/index.d.ts +1 -3
- package/dist/core/logger/index.d.ts +147 -0
- package/dist/core/mulguard/integration.d.ts +104 -0
- package/dist/core/mulguard/oauth-handler.d.ts +1 -1
- package/dist/core/security/security-manager.d.ts +236 -0
- package/dist/core/session/session-manager.d.ts +235 -0
- package/dist/core/types/index.d.ts +27 -5
- package/dist/index/index.js +1 -1
- package/dist/index/index.mjs +1388 -881
- package/dist/index.d.ts +3 -6
- package/dist/{client → nextjs/client}/hooks.d.ts +2 -2
- package/dist/nextjs/client/index.d.ts +13 -0
- package/dist/{client → nextjs/client}/provider.d.ts +1 -1
- package/dist/{client → nextjs/client}/server-actions-helper.d.ts +2 -2
- package/dist/{handlers → nextjs/handlers}/api.d.ts +1 -1
- package/dist/nextjs/handlers/index.d.ts +9 -0
- package/dist/{handlers → nextjs/handlers}/route.d.ts +1 -1
- package/dist/nextjs/index.d.ts +15 -0
- package/dist/nextjs/proxy/index.d.ts +149 -0
- package/dist/nextjs/server/actions.d.ts +30 -0
- package/dist/{server → nextjs/server}/auth.d.ts +6 -6
- package/dist/{server → nextjs/server}/cookies.d.ts +5 -6
- package/dist/nextjs/server/index.d.ts +18 -0
- package/dist/{server → nextjs/server}/oauth-state.d.ts +5 -3
- package/dist/{server → nextjs/server}/session-helpers.d.ts +1 -3
- package/dist/nextjs/server/session.d.ts +144 -0
- package/dist/oauth-state-Drwz6fES.js +1 -0
- package/dist/oauth-state-pdypStuS.mjs +210 -0
- package/dist/server/index.js +1 -1
- package/dist/server/index.mjs +27 -29
- package/package.json +64 -11
- package/dist/actions-CExpv_dD.js +0 -1
- package/dist/client/index.d.ts +0 -5
- package/dist/core/auth/index.d.ts +0 -40
- package/dist/core/auth/oauth.d.ts +0 -20
- package/dist/middleware/index.d.ts +0 -28
- package/dist/middleware/proxy.d.ts +0 -53
- package/dist/oauth-state-DKle8eCr.mjs +0 -289
- package/dist/oauth-state-DlvrCV11.js +0 -1
- package/dist/server/actions.d.ts +0 -86
- package/dist/server/helpers.d.ts +0 -10
- package/dist/server/index.d.ts +0 -14
- package/dist/server/middleware.d.ts +0 -39
- package/dist/server/session.d.ts +0 -28
- package/dist/server/utils.d.ts +0 -10
- /package/dist/{middleware → nextjs/proxy}/security.d.ts +0 -0
package/dist/index.d.ts
CHANGED
|
@@ -13,9 +13,6 @@
|
|
|
13
13
|
*/
|
|
14
14
|
export * from './core';
|
|
15
15
|
export * from './mulguard';
|
|
16
|
-
export * from './server';
|
|
17
|
-
export * from './handlers
|
|
18
|
-
export * from './
|
|
19
|
-
export * from './middleware';
|
|
20
|
-
export { createProxyMiddleware, checkRole as checkRoleProxy, } from './middleware/proxy';
|
|
21
|
-
export * from './middleware/security';
|
|
16
|
+
export * from './nextjs/server';
|
|
17
|
+
export * from './nextjs/handlers';
|
|
18
|
+
export * from './nextjs/proxy';
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Session, AuthResult, EmailCredentials, RegisterData, RememberedUser, Verify2FAData } from '
|
|
2
|
-
import { MulguardInstance } from '
|
|
1
|
+
import { Session, AuthResult, EmailCredentials, RegisterData, RememberedUser, Verify2FAData } from '../../core/types';
|
|
2
|
+
import { MulguardInstance } from '../../mulguard';
|
|
3
3
|
export interface UseAuthReturn {
|
|
4
4
|
session: Session | null;
|
|
5
5
|
isLoading: boolean;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Next.js Client-Side Integration for Mulguard Authentication Library.
|
|
3
|
+
*
|
|
4
|
+
* Provides client-side utilities for Next.js including:
|
|
5
|
+
* - React hooks (useAuth, useSession)
|
|
6
|
+
* - Provider component
|
|
7
|
+
* - Client-side session management
|
|
8
|
+
*
|
|
9
|
+
* @module @mulguard/nextjs/client
|
|
10
|
+
*/
|
|
11
|
+
export * from './hooks';
|
|
12
|
+
export * from './provider';
|
|
13
|
+
export type { Session, User, AuthResult } from '../../core/types';
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { MulguardInstance } from '
|
|
2
|
-
import { Verify2FAData, AuthResult, EmailCredentials, RegisterData } from '
|
|
1
|
+
import { MulguardInstance } from '../../mulguard';
|
|
2
|
+
import { Verify2FAData, AuthResult, EmailCredentials, RegisterData } from '../../core/types';
|
|
3
3
|
/**
|
|
4
4
|
* Verify 2FA with automatic fallback
|
|
5
5
|
* Tries Server Action first, then falls back to Route Handler
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Next.js Route Handlers for Mulguard Authentication Library.
|
|
3
|
+
*
|
|
4
|
+
* Provides route handlers for Next.js App Router API routes.
|
|
5
|
+
*
|
|
6
|
+
* @module @mulguard/nextjs/handlers
|
|
7
|
+
*/
|
|
8
|
+
export { toNextJsHandler } from './route';
|
|
9
|
+
export { createApiHandler } from './api';
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Next.js Integration for Mulguard Authentication Library.
|
|
3
|
+
*
|
|
4
|
+
* Provides comprehensive Next.js integration including:
|
|
5
|
+
* - Server-side utilities (getServerSession, Server Actions)
|
|
6
|
+
* - Client-side hooks (useAuth, useSession, Provider)
|
|
7
|
+
* - Proxy middleware (Next.js 16+)
|
|
8
|
+
* - Route handlers (App Router)
|
|
9
|
+
*
|
|
10
|
+
* @module @mulguard/nextjs
|
|
11
|
+
*/
|
|
12
|
+
export * from './server';
|
|
13
|
+
export * from './client';
|
|
14
|
+
export * from './proxy';
|
|
15
|
+
export * from './handlers';
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { NextResponse, NextRequest } from 'next/server';
|
|
2
|
+
import { MulguardInstance } from '../../mulguard';
|
|
3
|
+
/**
|
|
4
|
+
* Proxy middleware configuration.
|
|
5
|
+
*/
|
|
6
|
+
export interface ProxyMiddlewareConfig {
|
|
7
|
+
/**
|
|
8
|
+
* Mulguard auth instance.
|
|
9
|
+
*/
|
|
10
|
+
readonly auth: MulguardInstance;
|
|
11
|
+
/**
|
|
12
|
+
* Protected routes that require authentication.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ['/dashboard', '/profile', '/settings']
|
|
16
|
+
*/
|
|
17
|
+
readonly protectedRoutes?: readonly string[];
|
|
18
|
+
/**
|
|
19
|
+
* Public routes accessible without authentication.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ['/login', '/signup', '/about']
|
|
23
|
+
*/
|
|
24
|
+
readonly publicRoutes?: readonly string[];
|
|
25
|
+
/**
|
|
26
|
+
* Redirect to this URL if not authenticated.
|
|
27
|
+
*
|
|
28
|
+
* @default '/login'
|
|
29
|
+
*/
|
|
30
|
+
readonly redirectTo?: string;
|
|
31
|
+
/**
|
|
32
|
+
* Redirect to this URL if already authenticated (for login/register pages).
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* '/dashboard'
|
|
36
|
+
*/
|
|
37
|
+
readonly redirectIfAuthenticated?: string;
|
|
38
|
+
/**
|
|
39
|
+
* API routes prefix (default: '/api/auth').
|
|
40
|
+
*/
|
|
41
|
+
readonly apiPrefix?: string;
|
|
42
|
+
/**
|
|
43
|
+
* Enable security headers.
|
|
44
|
+
*
|
|
45
|
+
* @default true
|
|
46
|
+
*/
|
|
47
|
+
readonly enableSecurityHeaders?: boolean;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Creates proxy middleware for Next.js 16+.
|
|
51
|
+
*
|
|
52
|
+
* Replaces the old middleware.ts pattern with proxy-based approach.
|
|
53
|
+
*
|
|
54
|
+
* @param config - Proxy middleware configuration
|
|
55
|
+
* @returns Proxy middleware function
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```typescript
|
|
59
|
+
* // proxy.ts (Next.js 16+)
|
|
60
|
+
* import { createProxyMiddleware } from 'mulguard/nextjs/proxy'
|
|
61
|
+
* import { auth } from '@/lib/auth'
|
|
62
|
+
*
|
|
63
|
+
* export default createProxyMiddleware({
|
|
64
|
+
* auth,
|
|
65
|
+
* protectedRoutes: ['/dashboard', '/profile'],
|
|
66
|
+
* redirectTo: '/login',
|
|
67
|
+
* redirectIfAuthenticated: '/dashboard',
|
|
68
|
+
* })
|
|
69
|
+
*
|
|
70
|
+
* export const config = {
|
|
71
|
+
* matcher: ['/((?!api|_next/static|_next/image|favicon.ico).*)'],
|
|
72
|
+
* }
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
75
|
+
export declare function createProxyMiddleware(config: ProxyMiddlewareConfig): (request: NextRequest) => Promise<NextResponse>;
|
|
76
|
+
/**
|
|
77
|
+
* Checks if user has required role (for role-based access control).
|
|
78
|
+
*
|
|
79
|
+
* @param auth - Mulguard auth instance
|
|
80
|
+
* @param requiredRole - Required role
|
|
81
|
+
* @returns True if user has required role
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* ```typescript
|
|
85
|
+
* const hasAdminRole = await checkRole(auth, 'admin')
|
|
86
|
+
* if (!hasAdminRole) {
|
|
87
|
+
* return NextResponse.json({ error: 'Forbidden' }, { status: 403 })
|
|
88
|
+
* }
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
91
|
+
export declare function checkRole(auth: MulguardInstance, requiredRole: string): Promise<boolean>;
|
|
92
|
+
/**
|
|
93
|
+
* Creates a role-based proxy middleware.
|
|
94
|
+
*
|
|
95
|
+
* @param config - Proxy middleware configuration
|
|
96
|
+
* @param requiredRole - Required role
|
|
97
|
+
* @returns Proxy middleware function with role check
|
|
98
|
+
*
|
|
99
|
+
* @example
|
|
100
|
+
* ```typescript
|
|
101
|
+
* export default createRoleBasedProxy(
|
|
102
|
+
* { auth, protectedRoutes: ['/admin'] },
|
|
103
|
+
* 'admin'
|
|
104
|
+
* )
|
|
105
|
+
* ```
|
|
106
|
+
*/
|
|
107
|
+
export declare function createRoleBasedProxy(config: ProxyMiddlewareConfig, requiredRole: string): (request: NextRequest) => Promise<NextResponse>;
|
|
108
|
+
export { withSecurityHeaders } from './security';
|
|
109
|
+
/**
|
|
110
|
+
* TODO: Performance
|
|
111
|
+
* - [ ] Add session caching for proxy middleware
|
|
112
|
+
* - [ ] Optimize route matching with compiled regex
|
|
113
|
+
* - [ ] Add request-level session cache
|
|
114
|
+
* - [ ] Implement route preloading
|
|
115
|
+
*
|
|
116
|
+
* TODO: Features
|
|
117
|
+
* - [ ] Add IP-based rate limiting in proxy
|
|
118
|
+
* - [ ] Implement request logging
|
|
119
|
+
* - [ ] Add custom redirect logic support
|
|
120
|
+
* - [ ] Create route permission system
|
|
121
|
+
* - [ ] Add middleware chaining support
|
|
122
|
+
*
|
|
123
|
+
* TODO: Type Safety
|
|
124
|
+
* - [ ] Add branded types for routes
|
|
125
|
+
* - [ ] Create type-safe route matching
|
|
126
|
+
* - [ ] Implement compile-time route validation
|
|
127
|
+
*
|
|
128
|
+
* TODO: Security
|
|
129
|
+
* - [ ] Add request fingerprinting
|
|
130
|
+
* - [ ] Implement bot detection
|
|
131
|
+
* - [ ] Add DDoS protection
|
|
132
|
+
* - [ ] Create security event logging
|
|
133
|
+
*
|
|
134
|
+
* TODO: Testing
|
|
135
|
+
* - [ ] Add comprehensive unit tests
|
|
136
|
+
* - [ ] Test route matching logic
|
|
137
|
+
* - [ ] Test redirect behavior
|
|
138
|
+
* - [ ] Add Next.js integration tests
|
|
139
|
+
*
|
|
140
|
+
* TODO: Documentation
|
|
141
|
+
* - [ ] Document proxy middleware usage
|
|
142
|
+
* - [ ] Add route protection guide
|
|
143
|
+
* - [ ] Create security best practices guide
|
|
144
|
+
*
|
|
145
|
+
* TODO: Limitations
|
|
146
|
+
* - [ ] Proxy middleware runs on every request (consider caching)
|
|
147
|
+
* - [ ] Session check is synchronous (consider async optimization)
|
|
148
|
+
* - [ ] Route matching is simple (consider regex support)
|
|
149
|
+
*/
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { MulguardInstance } from '../../mulguard';
|
|
2
|
+
import { Verify2FAData, AuthResult, EmailCredentials, RegisterData } from '../../core/types';
|
|
3
|
+
/**
|
|
4
|
+
* Verify 2FA code - Server Action
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```typescript
|
|
8
|
+
* 'use client'
|
|
9
|
+
* import { verify2FAAction } from 'mulguard/nextjs/server'
|
|
10
|
+
* import { auth } from '@/lib/auth'
|
|
11
|
+
*
|
|
12
|
+
* const result = await verify2FAAction(auth, { email, userId, code })
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
export declare function verify2FAAction(auth: MulguardInstance, data: Verify2FAData): Promise<AuthResult>;
|
|
16
|
+
/**
|
|
17
|
+
* Sign out - Server Action
|
|
18
|
+
*/
|
|
19
|
+
export declare function signOutAction(auth: MulguardInstance): Promise<{
|
|
20
|
+
success: boolean;
|
|
21
|
+
error?: string;
|
|
22
|
+
}>;
|
|
23
|
+
/**
|
|
24
|
+
* Sign in with email - Server Action
|
|
25
|
+
*/
|
|
26
|
+
export declare function signInEmailAction(auth: MulguardInstance, credentials: EmailCredentials): Promise<AuthResult>;
|
|
27
|
+
/**
|
|
28
|
+
* Sign up - Server Action
|
|
29
|
+
*/
|
|
30
|
+
export declare function signUpAction(auth: MulguardInstance, data: RegisterData): Promise<AuthResult>;
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import { Session, User } from '
|
|
2
|
-
import { MulguardInstance } from '
|
|
1
|
+
import { Session, User } from '../../core/types';
|
|
2
|
+
import { MulguardInstance } from '../../mulguard';
|
|
3
3
|
/**
|
|
4
4
|
* Get server session using auth instance
|
|
5
5
|
*
|
|
6
6
|
* @example
|
|
7
7
|
* ```typescript
|
|
8
8
|
* import { auth } from '@/auth'
|
|
9
|
-
* import { getServerSession } from 'mulguard/server'
|
|
9
|
+
* import { getServerSession } from 'mulguard/nextjs/server'
|
|
10
10
|
*
|
|
11
11
|
* export default async function Page() {
|
|
12
12
|
* const session = await getServerSession(auth)
|
|
@@ -24,7 +24,7 @@ export declare function getServerSession(auth: MulguardInstance): Promise<Sessio
|
|
|
24
24
|
* @example
|
|
25
25
|
* ```typescript
|
|
26
26
|
* import { auth } from '@/auth'
|
|
27
|
-
* import { requireAuth } from 'mulguard/server'
|
|
27
|
+
* import { requireAuth } from 'mulguard/nextjs/server'
|
|
28
28
|
*
|
|
29
29
|
* export default async function ProtectedPage() {
|
|
30
30
|
* const session = await requireAuth(auth, '/login')
|
|
@@ -39,7 +39,7 @@ export declare function requireAuth(auth: MulguardInstance, redirectTo?: string)
|
|
|
39
39
|
* @example
|
|
40
40
|
* ```typescript
|
|
41
41
|
* import { auth } from '@/auth'
|
|
42
|
-
* import { requireRole } from 'mulguard/server'
|
|
42
|
+
* import { requireRole } from 'mulguard/nextjs/server'
|
|
43
43
|
*
|
|
44
44
|
* export default async function AdminPage() {
|
|
45
45
|
* const session = await requireRole(auth, 'admin', '/unauthorized')
|
|
@@ -54,7 +54,7 @@ export declare function requireRole(auth: MulguardInstance, role: string, redire
|
|
|
54
54
|
* @example
|
|
55
55
|
* ```typescript
|
|
56
56
|
* import { auth } from '@/auth'
|
|
57
|
-
* import { getCurrentUser } from 'mulguard/server'
|
|
57
|
+
* import { getCurrentUser } from 'mulguard/nextjs/server'
|
|
58
58
|
*
|
|
59
59
|
* export default async function Page() {
|
|
60
60
|
* const user = await getCurrentUser(auth)
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { SessionConfig } from '
|
|
1
|
+
import { SessionConfig } from '../../core/types';
|
|
2
2
|
export interface CookieOptions {
|
|
3
3
|
name: string;
|
|
4
4
|
value: string;
|
|
@@ -10,10 +10,6 @@ export interface CookieOptions {
|
|
|
10
10
|
path?: string;
|
|
11
11
|
domain?: string;
|
|
12
12
|
}
|
|
13
|
-
/**
|
|
14
|
-
* Get cookie value from Next.js cookies
|
|
15
|
-
*/
|
|
16
|
-
export declare function getCookie(name: string): Promise<string | undefined>;
|
|
17
13
|
/**
|
|
18
14
|
* Result of setting a cookie
|
|
19
15
|
*/
|
|
@@ -22,9 +18,12 @@ export interface SetCookieResult {
|
|
|
22
18
|
error?: string;
|
|
23
19
|
warning?: string;
|
|
24
20
|
}
|
|
21
|
+
/**
|
|
22
|
+
* Get cookie value from Next.js cookies
|
|
23
|
+
*/
|
|
24
|
+
export declare function getCookie(name: string): Promise<string | undefined>;
|
|
25
25
|
/**
|
|
26
26
|
* Set cookie in Next.js response
|
|
27
|
-
* Note: This requires using Next.js 15+ with async cookies() or response cookies
|
|
28
27
|
*
|
|
29
28
|
* @returns Result indicating success or failure with error message
|
|
30
29
|
*/
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Next.js Server-Side Integration for Mulguard Authentication Library.
|
|
3
|
+
*
|
|
4
|
+
* Provides server-side utilities for Next.js App Router including:
|
|
5
|
+
* - Session management (getServerSession, getServerUser)
|
|
6
|
+
* - Server Actions helpers
|
|
7
|
+
* - Cookie utilities
|
|
8
|
+
* - Authentication helpers
|
|
9
|
+
*
|
|
10
|
+
* @module @mulguard/nextjs/server
|
|
11
|
+
*/
|
|
12
|
+
export * from './session';
|
|
13
|
+
export * from './actions';
|
|
14
|
+
export { requireAuth, requireRole, getCurrentUser } from './auth';
|
|
15
|
+
export * from './cookies';
|
|
16
|
+
export * from './session-helpers';
|
|
17
|
+
export * from './oauth-state';
|
|
18
|
+
export type { Session, User } from '../../core/types';
|
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Server-side OAuth state management
|
|
3
|
-
*
|
|
2
|
+
* Server-side OAuth state management for Next.js.
|
|
3
|
+
*
|
|
4
|
+
* Stores OAuth state in httpOnly cookies for security.
|
|
4
5
|
*
|
|
5
6
|
* ✅ SECURE: Uses httpOnly cookies to prevent XSS attacks
|
|
6
7
|
* ✅ PRODUCTION-READY: Works with Next.js Server Actions
|
|
7
8
|
*
|
|
8
9
|
* ⚠️ NOTE: For production with multiple server instances, use Redis or Database store instead.
|
|
9
|
-
*
|
|
10
|
+
*
|
|
11
|
+
* @module @mulguard/nextjs/server/oauth-state
|
|
10
12
|
*/
|
|
11
13
|
/**
|
|
12
14
|
* Store OAuth state in httpOnly cookie
|
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
import { Session } from '
|
|
1
|
+
import { Session } from '../../core/types';
|
|
2
2
|
/**
|
|
3
3
|
* Check if session is expired (helper version that accepts null)
|
|
4
|
-
* Note: The main isSessionExpired is exported from session.ts
|
|
5
|
-
* This is a convenience helper for nullable sessions
|
|
6
4
|
*/
|
|
7
5
|
export declare function isSessionExpiredNullable(session: Session | null): boolean;
|
|
8
6
|
/**
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { Session, User } from '../../core/types';
|
|
2
|
+
import { MulguardInstance } from '../../mulguard';
|
|
3
|
+
/**
|
|
4
|
+
* Gets the current session on the server side.
|
|
5
|
+
*
|
|
6
|
+
* Reads session from cookie and validates expiration.
|
|
7
|
+
* Works in Server Components, Server Actions, and API Routes.
|
|
8
|
+
*
|
|
9
|
+
* @template TUser - User type
|
|
10
|
+
* @template TSession - Session type
|
|
11
|
+
* @param auth - Mulguard instance
|
|
12
|
+
* @returns Current session or null if not authenticated
|
|
13
|
+
* @throws SessionExpiredError if session is expired
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* // In Server Component
|
|
18
|
+
* import { getServerSession } from 'mulguard/nextjs/server'
|
|
19
|
+
* import { auth } from '@/lib/auth'
|
|
20
|
+
*
|
|
21
|
+
* export default async function DashboardPage() {
|
|
22
|
+
* const session = await getServerSession(auth)
|
|
23
|
+
* if (!session) {
|
|
24
|
+
* redirect('/login')
|
|
25
|
+
* }
|
|
26
|
+
* return <div>Welcome, {session.user.name}!</div>
|
|
27
|
+
* }
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export declare function getServerSession<TUser extends User = User, TSession extends Session<TUser> = Session<TUser>>(auth: MulguardInstance): Promise<TSession | null>;
|
|
31
|
+
/**
|
|
32
|
+
* Checks if user is authenticated on the server side.
|
|
33
|
+
*
|
|
34
|
+
* @param auth - Mulguard instance
|
|
35
|
+
* @returns True if user is authenticated
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```typescript
|
|
39
|
+
* const isAuthenticated = await isAuthenticated(auth)
|
|
40
|
+
* if (!isAuthenticated) {
|
|
41
|
+
* redirect('/login')
|
|
42
|
+
* }
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
export declare function isAuthenticated(auth: MulguardInstance): Promise<boolean>;
|
|
46
|
+
/**
|
|
47
|
+
* Gets the current user from session on the server side.
|
|
48
|
+
*
|
|
49
|
+
* @template TUser - User type
|
|
50
|
+
* @param auth - Mulguard instance
|
|
51
|
+
* @returns Current user or null if not authenticated
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```typescript
|
|
55
|
+
* const user = await getServerUser(auth)
|
|
56
|
+
* if (user) {
|
|
57
|
+
* console.log('User:', user.email)
|
|
58
|
+
* }
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
export declare function getServerUser<TUser extends User = User>(auth: MulguardInstance): Promise<TUser | null>;
|
|
62
|
+
/**
|
|
63
|
+
* Server Action wrapper for authentication operations.
|
|
64
|
+
*
|
|
65
|
+
* Provides type-safe server actions with automatic error handling.
|
|
66
|
+
*
|
|
67
|
+
* @template TResult - Result type
|
|
68
|
+
* @param action - Server action function
|
|
69
|
+
* @returns Server action result
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* ```typescript
|
|
73
|
+
* 'use server'
|
|
74
|
+
*
|
|
75
|
+
* import { createServerAction } from 'mulguard/nextjs/server'
|
|
76
|
+
* import { auth } from '@/lib/auth'
|
|
77
|
+
*
|
|
78
|
+
* export const signInAction = createServerAction(async (email: string, password: string) => {
|
|
79
|
+
* return await auth.signIn('credentials', { email, password })
|
|
80
|
+
* })
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
export declare function createServerAction<TResult>(action: () => Promise<TResult>): () => Promise<TResult>;
|
|
84
|
+
/**
|
|
85
|
+
* Server Action with authentication check.
|
|
86
|
+
*
|
|
87
|
+
* Ensures user is authenticated before executing action.
|
|
88
|
+
*
|
|
89
|
+
* @template TResult - Result type
|
|
90
|
+
* @template TUser - User type
|
|
91
|
+
* @param auth - Mulguard instance
|
|
92
|
+
* @param action - Server action function with user parameter
|
|
93
|
+
* @returns Server action result
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* ```typescript
|
|
97
|
+
* 'use server'
|
|
98
|
+
*
|
|
99
|
+
* import { createAuthenticatedAction } from 'mulguard/nextjs/server'
|
|
100
|
+
* import { auth } from '@/lib/auth'
|
|
101
|
+
*
|
|
102
|
+
* export const updateProfileAction = createAuthenticatedAction(auth, async (user, data) => {
|
|
103
|
+
* // User is guaranteed to be authenticated here
|
|
104
|
+
* return await updateUserProfile(user.id, data)
|
|
105
|
+
* })
|
|
106
|
+
* ```
|
|
107
|
+
*/
|
|
108
|
+
export declare function createAuthenticatedAction<TResult, TUser extends User = User>(auth: MulguardInstance, action: (user: TUser) => Promise<TResult>): () => Promise<TResult>;
|
|
109
|
+
export type { Session, User } from '../../core/types';
|
|
110
|
+
export { SessionExpiredError } from '../../core/errors';
|
|
111
|
+
/**
|
|
112
|
+
* TODO: Performance
|
|
113
|
+
* - [ ] Add session caching for Server Components
|
|
114
|
+
* - [ ] Implement request-level session cache
|
|
115
|
+
* - [ ] Optimize cookie reading
|
|
116
|
+
* - [ ] Add session preloading
|
|
117
|
+
*
|
|
118
|
+
* TODO: Features
|
|
119
|
+
* - [ ] Add session refresh helpers
|
|
120
|
+
* - [ ] Implement session invalidation
|
|
121
|
+
* - [ ] Add session middleware support
|
|
122
|
+
* - [ ] Create session debugging utilities
|
|
123
|
+
*
|
|
124
|
+
* TODO: Type Safety
|
|
125
|
+
* - [ ] Add branded types for session IDs
|
|
126
|
+
* - [ ] Create type-safe cookie handling
|
|
127
|
+
* - [ ] Implement compile-time session validation
|
|
128
|
+
*
|
|
129
|
+
* TODO: Testing
|
|
130
|
+
* - [ ] Add comprehensive unit tests
|
|
131
|
+
* - [ ] Test Server Actions integration
|
|
132
|
+
* - [ ] Test cookie handling
|
|
133
|
+
* - [ ] Add Next.js integration tests
|
|
134
|
+
*
|
|
135
|
+
* TODO: Documentation
|
|
136
|
+
* - [ ] Document Server Actions usage
|
|
137
|
+
* - [ ] Add Server Components guide
|
|
138
|
+
* - [ ] Create API Routes guide
|
|
139
|
+
*
|
|
140
|
+
* TODO: Limitations
|
|
141
|
+
* - [ ] Cookie handling requires Next.js cookies() API
|
|
142
|
+
* - [ ] Server Actions require 'use server' directive
|
|
143
|
+
* - [ ] Session caching is per-request (consider global cache)
|
|
144
|
+
*/
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var y=Object.defineProperty;var m=(t,e,r)=>e in t?y(t,e,{enumerable:!0,configurable:!0,writable:!0,value:r}):t[e]=r;var a=(t,e,r)=>m(t,typeof e!="symbol"?e+"":e,r);const o=require("./actions-CMtg7FGv.js"),S=require("next/navigation");class l extends Error{constructor(r,s,i,E){super(s);a(this,"code");a(this,"statusCode");a(this,"details");this.name="AuthError",this.code=r,this.statusCode=i??o.getErrorStatusCode(r),this.details=E,Error.captureStackTrace&&Error.captureStackTrace(this,l)}toJSON(){return{code:this.code,message:this.message,statusCode:this.statusCode,details:this.details}}toErrorResult(){return{success:!1,error:this.message,errorCode:this.code,details:this.details}}}class c extends l{constructor(e="Session has expired",r){super(o.AuthErrorCode.SESSION_EXPIRED,e,void 0,r),this.name="SessionExpiredError"}}function f(t){return!t||!t.expiresAt?!1:new Date(t.expiresAt)<new Date}function O(t,e=5){if(!t||!t.expiresAt)return!1;const r=new Date(t.expiresAt),s=new Date,i=(r.getTime()-s.getTime())/(1e3*60);return i>0&&i<e}function T(t){if(!t||!t.expiresAt)return null;const e=new Date(t.expiresAt),r=new Date,s=(e.getTime()-r.getTime())/(1e3*60);return s>0?Math.floor(s):0}function v(t){return!(!t||!t.user||!t.user.id||!t.user.email||!t.user.name||f(t))}function h(t){if(!t||typeof t!="object")return!1;const e=t;if(!e.user||typeof e.user!="object")return!1;const r=e.user;if(typeof r.id!="string"||r.id.length===0||typeof r.email!="string"||r.email.length===0||typeof r.name!="string"||r.name.length===0||!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(r.email))return!1;if(e.expiresAt)if(e.expiresAt instanceof Date){if(isNaN(e.expiresAt.getTime()))return!1}else if(typeof e.expiresAt=="string"){const i=new Date(e.expiresAt);if(isNaN(i.getTime()))return!1}else return!1;return!0}async function u(t){try{const e=await t.getSession();if(!e)return null;if((typeof e.expiresAt=="string"?new Date(e.expiresAt):e.expiresAt).getTime()<Date.now())throw new c("Session has expired");return e}catch(e){if(e instanceof c)throw e;return null}}async function C(t){try{return await u(t)!==null}catch{return!1}}async function D(t){const e=await u(t);return(e==null?void 0:e.user)||null}function N(t){return async()=>{try{return await t()}catch(e){throw e}}}function _(t,e){return async()=>{const r=await u(t);if(!r)throw new Error("Authentication required");return await e(r.user)}}async function A(t){try{const e=await t.getSession();return!e||!h(e)||f(e)?null:e}catch(e){return process.env.NODE_ENV==="development"&&console.error("Failed to get server session:",e),null}}async function g(t,e="/login"){const r=await A(t);return r||S.redirect(e),r}async function k(t,e,r="/unauthorized"){const s=await g(t);return(!s.user.roles||!s.user.roles.includes(e))&&S.redirect(r),s}async function U(t){const e=await A(t);return(e==null?void 0:e.user)??null}const d="__mulguard_oauth_state",p=10*60;async function x(t,e){try{const r=JSON.stringify({state:t,provider:e,expiresAt:Date.now()+p*1e3}),s=process.env.NODE_ENV==="production";return await o.setCookie({name:d,value:r,httpOnly:!0,secure:s,sameSite:"lax",maxAge:p,path:"/"})}catch(r){return{success:!1,error:r instanceof Error?r.message:"Failed to store OAuth state"}}}async function w(){try{const t=await o.getCookie(d);if(!t)return null;const e=JSON.parse(t);return e.expiresAt<Date.now()?(await n(),null):(await n(),{state:e.state,provider:e.provider})}catch{return await n(),null}}async function n(){await o.deleteCookie(d,{path:"/"})}const b=Object.freeze(Object.defineProperty({__proto__:null,deleteOAuthStateCookie:n,getOAuthStateCookie:w,storeOAuthStateCookie:x},Symbol.toStringTag,{value:"Module"}));exports.SessionExpiredError=c;exports.createAuthenticatedAction=_;exports.createServerAction=N;exports.deleteOAuthStateCookie=n;exports.getCurrentUser=U;exports.getOAuthStateCookie=w;exports.getServerSession=u;exports.getServerUser=D;exports.getSessionTimeUntilExpiry=T;exports.isAuthenticated=C;exports.isSessionExpiredNullable=f;exports.isSessionExpiringSoon=O;exports.isSessionValid=v;exports.oauthState=b;exports.requireAuth=g;exports.requireRole=k;exports.storeOAuthStateCookie=x;exports.validateSessionStructure=h;
|