@payez/next-mvp 3.4.0 → 3.6.0

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.
@@ -0,0 +1,240 @@
1
+ /**
2
+ * Admin Stats API Handler
3
+ *
4
+ * Aggregates dashboard statistics from users, Redis sessions, and audit logs.
5
+ * Uses service account HMAC auth for Vibe API requests.
6
+ *
7
+ * @version 1.0
8
+ * @requires Admin role (vibe_app_admin or payez_admin)
9
+ */
10
+
11
+ import { NextRequest, NextResponse } from 'next/server';
12
+ import { getServerSession } from 'next-auth';
13
+ import { getStartupIDPConfig } from '../../lib/startup-init';
14
+ import { getRedis } from '../../lib/redis';
15
+ import { ADMIN_ROLES } from '../../lib/roles';
16
+
17
+ interface VibeRequestOptions {
18
+ method: 'GET' | 'POST' | 'PUT' | 'DELETE';
19
+ body?: unknown;
20
+ }
21
+
22
+ async function checkAdminRole(getAuthOptions: () => Promise<any>): Promise<{ isAdmin: boolean; error?: NextResponse }> {
23
+ const authOptions = await getAuthOptions();
24
+ const session = await getServerSession(authOptions) as any;
25
+
26
+ if (!session?.user) {
27
+ return {
28
+ isAdmin: false,
29
+ error: NextResponse.json({ success: false, error: 'Please sign in' }, { status: 401 }),
30
+ };
31
+ }
32
+
33
+ const userRoles = (session.user?.roles as string[]) || [];
34
+ const hasAdminRole = ADMIN_ROLES.some(role => userRoles.includes(role));
35
+
36
+ if (!hasAdminRole) {
37
+ return {
38
+ isAdmin: false,
39
+ error: NextResponse.json({ success: false, error: 'Admin access required' }, { status: 403 }),
40
+ };
41
+ }
42
+
43
+ return { isAdmin: true };
44
+ }
45
+
46
+ async function vibeServiceRequest<T = unknown>(
47
+ endpoint: string,
48
+ options: VibeRequestOptions
49
+ ): Promise<{ ok: boolean; status: number; data: T | null; error?: string }> {
50
+ const idpUrl = process.env.NEXT_PUBLIC_IDP_URL || process.env.IDP_URL;
51
+ const clientId = process.env.VIBE_CLIENT_ID;
52
+ const signingKey = process.env.VIBE_HMAC_KEY;
53
+
54
+ if (!idpUrl || !clientId || !signingKey) {
55
+ return { ok: false, status: 500, data: null, error: 'Vibe not configured' };
56
+ }
57
+
58
+ const timestamp = Math.floor(Date.now() / 1000);
59
+ const stringToSign = `${timestamp}|${options.method}|${endpoint}`;
60
+
61
+ const crypto = await import('crypto');
62
+ const signature = crypto
63
+ .createHmac('sha256', Buffer.from(signingKey, 'base64'))
64
+ .update(stringToSign)
65
+ .digest('base64');
66
+
67
+ const proxyUrl = `${idpUrl}/api/vibe/proxy`;
68
+
69
+ const idpConfig = getStartupIDPConfig();
70
+ const idpClientId = idpConfig?.clientSlug || idpConfig?.clientId;
71
+
72
+ try {
73
+ const res = await fetch(proxyUrl, {
74
+ method: 'POST',
75
+ headers: {
76
+ 'Content-Type': 'application/json',
77
+ 'X-Vibe-Client-Id': clientId,
78
+ 'X-Vibe-Timestamp': String(timestamp),
79
+ 'X-Vibe-Signature': signature,
80
+ ...(idpClientId && { 'X-Client-Id': idpClientId }),
81
+ },
82
+ body: JSON.stringify({
83
+ endpoint,
84
+ method: options.method,
85
+ data: options.body ?? null,
86
+ }),
87
+ cache: 'no-store',
88
+ });
89
+
90
+ if (res.status === 204) return { ok: true, status: 204, data: null };
91
+ if (!res.ok) {
92
+ const errorText = await res.text();
93
+ return { ok: false, status: res.status, data: null, error: errorText };
94
+ }
95
+
96
+ const body = await res.json();
97
+ return { ok: true, status: res.status, data: body };
98
+ } catch (error) {
99
+ return { ok: false, status: 0, data: null, error: String(error) };
100
+ }
101
+ }
102
+
103
+ export interface AdminStatsHandlerConfig {
104
+ getAuthOptions: () => Promise<any>;
105
+ appSlug?: string;
106
+ }
107
+
108
+ /**
109
+ * GET /api/admin/stats - Dashboard statistics
110
+ * Aggregates users + tier breakdown, active Redis sessions, and recent audit activity.
111
+ */
112
+ export function createStatsHandler(config: AdminStatsHandlerConfig) {
113
+ const getSessionPrefix = () => {
114
+ const appSlug = config.appSlug || process.env.APP_SLUG || process.env.CLIENT_ID || 'app';
115
+ return `${appSlug}:sess:`;
116
+ };
117
+
118
+ return {
119
+ async GET(_request: NextRequest) {
120
+ const adminCheck = await checkAdminRole(config.getAuthOptions);
121
+ if (adminCheck.error) return adminCheck.error;
122
+
123
+ try {
124
+ // Fetch from 3 sources in parallel
125
+ const [usersResult, sessionCount, auditResult] = await Promise.allSettled([
126
+ // 1. Users + tier breakdown via HMAC proxy (Vibe collection query)
127
+ vibeServiceRequest<any>('/v1/collections/vibe_app/tables/users/query', {
128
+ method: 'POST',
129
+ body: { page: 1, pageSize: 500, orderBy: 'created_at', orderDirection: 'desc' },
130
+ }),
131
+
132
+ // 2. Active sessions from Redis
133
+ (async () => {
134
+ const redis = getRedis();
135
+ const sessionPrefix = getSessionPrefix();
136
+ const sessionKeys: string[] = [];
137
+ let cursor = '0';
138
+ do {
139
+ const [newCursor, keys] = await redis.scan(cursor, 'MATCH', `${sessionPrefix}*`, 'COUNT', 100);
140
+ cursor = newCursor;
141
+ sessionKeys.push(...keys.filter((k: string) => !k.includes(':ver:')));
142
+ } while (cursor !== '0');
143
+ return sessionKeys.length;
144
+ })(),
145
+
146
+ // 3. Recent audit activity via HMAC proxy
147
+ vibeServiceRequest<any>('/v1/audit?pageSize=10&sortDir=desc', { method: 'GET' }),
148
+ ]);
149
+
150
+ // Parse users — deduplicate by user_id
151
+ let totalUsers = 0;
152
+ let tierBreakdown: Record<string, number> = {};
153
+ if (usersResult.status === 'fulfilled' && usersResult.value.ok && usersResult.value.data) {
154
+ const data = usersResult.value.data;
155
+ const rawUsers = data.data || data.documents || data.users || [];
156
+
157
+ // Deduplicate by user_id (keeps latest document_id)
158
+ const userMap = new Map();
159
+ for (const u of rawUsers) {
160
+ const uid = u.user_id || u.id || u.document_id;
161
+ const existing = userMap.get(uid);
162
+ if (!existing || (u.document_id || '') > (existing.document_id || '')) {
163
+ userMap.set(uid, u);
164
+ }
165
+ }
166
+ const uniqueUsers = Array.from(userMap.values());
167
+ totalUsers = uniqueUsers.length;
168
+
169
+ // Build tier breakdown from deduplicated users (unless API provides one)
170
+ tierBreakdown = data.tierBreakdown || data.tiers || {};
171
+ if (Object.keys(tierBreakdown).length === 0) {
172
+ for (const user of uniqueUsers) {
173
+ const tier = user.tier || 'free';
174
+ tierBreakdown[tier] = (tierBreakdown[tier] || 0) + 1;
175
+ }
176
+ }
177
+ }
178
+
179
+ // Parse active sessions count
180
+ let activeSessions = 0;
181
+ if (sessionCount.status === 'fulfilled') {
182
+ activeSessions = sessionCount.value;
183
+ }
184
+
185
+ // Parse audit events for recent activity
186
+ let recentActivity: any[] = [];
187
+ if (auditResult.status === 'fulfilled' && auditResult.value.ok && auditResult.value.data) {
188
+ const data = auditResult.value.data;
189
+
190
+ // Handle multiple possible response shapes
191
+ let events: any[] = [];
192
+ if (Array.isArray(data)) {
193
+ events = data;
194
+ } else if (Array.isArray(data.data)) {
195
+ events = data.data;
196
+ } else if (Array.isArray(data.entries)) {
197
+ events = data.entries;
198
+ } else if (Array.isArray(data.items)) {
199
+ events = data.items;
200
+ } else if (Array.isArray(data.documents)) {
201
+ events = data.documents;
202
+ } else if (data.success && Array.isArray(data.results)) {
203
+ events = data.results;
204
+ }
205
+
206
+ recentActivity = events.slice(0, 5).map((e: any) => ({
207
+ id: e.audit_log_id || e.id || e.document_id,
208
+ type: e.category || e.type || 'admin',
209
+ action: e.action || e.event || e.message || 'Unknown action',
210
+ actor: e.admin_email || e.actor || e.user || e.actor_email || 'System',
211
+ target: e.target_type ? `${e.target_type}:${e.target_id}` : (e.target || e.target_user),
212
+ details: e.description || e.details,
213
+ timestamp: e.created_at || e.timestamp || e.date,
214
+ success: e.is_success ?? e.success ?? true,
215
+ }));
216
+ }
217
+
218
+ // Calculate tier percentages
219
+ const tiers = Object.entries(tierBreakdown).map(([name, count]) => ({
220
+ name,
221
+ count: count as number,
222
+ pct: totalUsers > 0 ? Math.round(((count as number) / totalUsers) * 100) : 0,
223
+ }));
224
+
225
+ return NextResponse.json({
226
+ totalUsers,
227
+ activeSessions,
228
+ tiers,
229
+ recentActivity,
230
+ });
231
+ } catch (error: any) {
232
+ console.error('[admin/stats] Error:', error);
233
+ return NextResponse.json(
234
+ { error: error.message || 'Internal error' },
235
+ { status: 500 }
236
+ );
237
+ }
238
+ },
239
+ };
240
+ }
@@ -11,15 +11,29 @@
11
11
  * - Graceful degradation on failure
12
12
  */
13
13
 
14
- import Transport from 'winston-transport';
15
14
  import { getRedis } from '../lib/redis';
16
15
 
16
+ // Dynamic import — winston is a peerDependency and may not be installed.
17
+ // We resolve the base class lazily so builds don't break without it.
18
+ let TransportBase: any;
19
+ try {
20
+ TransportBase = require('winston-transport');
21
+ } catch {
22
+ // Fallback: minimal base class for when winston is not installed
23
+ TransportBase = class {
24
+ constructor(_opts?: any) {}
25
+ emit(_event: string, _info: any) {}
26
+ };
27
+ }
28
+
17
29
  /** Redis key for pending log entries */
18
30
  const REDIS_LOG_KEY = 'vibe:logs:pending';
19
31
  /** TTL in seconds: 1 week */
20
32
  const REDIS_LOG_TTL = 7 * 24 * 60 * 60;
21
33
 
22
- export interface VibeLogTransportOptions extends Transport.TransportStreamOptions {
34
+ export interface VibeLogTransportOptions {
35
+ /** Winston transport level */
36
+ level?: string;
23
37
  /** Redis URL (optional, uses REDIS_URL env var by default) */
24
38
  redisUrl?: string;
25
39
  /** Vibe client ID (for log metadata) */
@@ -55,7 +69,7 @@ const LEVEL_ORDER: Record<string, number> = {
55
69
  /**
56
70
  * Winston transport that buffers logs to Redis for Vibe drain processing
57
71
  */
58
- export class VibeLogTransport extends Transport {
72
+ export class VibeLogTransport extends TransportBase {
59
73
  private vibeClientId: string;
60
74
  private appSlug: string;
61
75
  private minLevelNum: number;
@@ -0,0 +1,170 @@
1
+ /**
2
+ * Server-Side Auth Guard for Layouts
3
+ *
4
+ * Replaces middleware's self-fetch auth checks with direct Redis/function calls.
5
+ * Call from server-component layouts to protect routes.
6
+ *
7
+ * Zero HTTP self-fetches. ~8ms total (Redis + in-memory checks).
8
+ */
9
+
10
+ import 'server-only';
11
+ import { redirect } from 'next/navigation';
12
+ import { headers } from 'next/headers';
13
+ import { decodeSession } from './decode-session';
14
+ import { getIDPClientConfig } from '../lib/idp-client-config';
15
+ import { getSessionCookieName, getSecureSessionCookieName } from '../lib/app-slug';
16
+ import type { SessionData } from '../lib/session-store';
17
+
18
+ // =============================================================================
19
+ // TYPES
20
+ // =============================================================================
21
+
22
+ export interface AuthGuardOptions {
23
+ /** Custom checks to run after standard auth validation */
24
+ checks?: AuthCheck[];
25
+ /** Override login redirect URL (default: /account-auth/login) */
26
+ loginUrl?: string;
27
+ /** Override 2FA redirect URL (default: /account-auth/verify-code) */
28
+ verifyCodeUrl?: string;
29
+ /** Override service unavailable URL (default: /service-unavailable) */
30
+ serviceUnavailableUrl?: string;
31
+ }
32
+
33
+ export interface AuthCheck {
34
+ /** Name for logging */
35
+ name: string;
36
+ /** Returns redirect URL if check fails, null if passes */
37
+ check: (session: SessionData, pathname: string) => Promise<string | null>;
38
+ }
39
+
40
+ export interface AuthGuardResult {
41
+ userId: string;
42
+ email: string;
43
+ roles: string[];
44
+ sessionData: SessionData;
45
+ accessToken?: string;
46
+ }
47
+
48
+ // =============================================================================
49
+ // CONSTANTS
50
+ // =============================================================================
51
+
52
+ const LOGIN_PAGE = '/account-auth/login';
53
+ const VERIFY_CODE_PAGE = '/account-auth/verify-code';
54
+ const SERVICE_UNAVAILABLE_PAGE = '/service-unavailable';
55
+
56
+ // =============================================================================
57
+ // MAIN
58
+ // =============================================================================
59
+
60
+ /**
61
+ * Server-side auth guard. Call from async server layouts.
62
+ *
63
+ * Redirects (via next/navigation redirect()) if:
64
+ * - No session cookie / invalid JWT
65
+ * - Session not in Redis (stale)
66
+ * - Session force-invalidated
67
+ * - 2FA required but not completed / expired
68
+ * - Any custom check fails
69
+ *
70
+ * Returns the authenticated user's session data on success.
71
+ */
72
+ export async function authGuard(options?: AuthGuardOptions): Promise<AuthGuardResult> {
73
+ const loginUrl = options?.loginUrl || LOGIN_PAGE;
74
+ const verifyCodeUrl = options?.verifyCodeUrl || VERIFY_CODE_PAGE;
75
+ const serviceUnavailableUrl = options?.serviceUnavailableUrl || SERVICE_UNAVAILABLE_PAGE;
76
+
77
+ // Get current pathname from headers (set by Next.js)
78
+ const headerStore = await headers();
79
+ const pathname = headerStore.get('x-next-pathname') ||
80
+ headerStore.get('x-invoke-path') ||
81
+ headerStore.get('x-matched-path') ||
82
+ '/';
83
+
84
+ const callbackUrl = encodeURIComponent(pathname);
85
+
86
+ // --- Decode session (cookie → JWT → Redis) ---
87
+ let decoded: Awaited<ReturnType<typeof decodeSession>>;
88
+ try {
89
+ decoded = await decodeSession();
90
+ } catch (error) {
91
+ // Redis unreachable or startup failure → fail closed
92
+ console.error('[AUTH-GUARD] Session decode failed (service error):', error instanceof Error ? error.message : String(error));
93
+ redirect(serviceUnavailableUrl);
94
+ }
95
+
96
+ // No session at all → redirect to login
97
+ if (!decoded) {
98
+ redirect(`${loginUrl}?callbackUrl=${callbackUrl}`);
99
+ }
100
+
101
+ const { sessionData } = decoded;
102
+
103
+ // --- Force-invalidated session (admin action, password change) ---
104
+ if (sessionData.forceInvalidated) {
105
+ console.warn('[AUTH-GUARD] Session force-invalidated', {
106
+ userId: sessionData.userId,
107
+ pathname,
108
+ });
109
+ redirect(`${loginUrl}?callbackUrl=${callbackUrl}&reason=invalidated`);
110
+ }
111
+
112
+ // --- 2FA check ---
113
+ try {
114
+ const config = await getIDPClientConfig();
115
+ const requires2FA = config.authSettings?.require2FA ?? true;
116
+
117
+ if (requires2FA) {
118
+ const mfaVerified = sessionData.mfaVerified ?? (sessionData as any).twoFactorComplete ?? false;
119
+ const mfaExpiresAt = sessionData.mfaExpiresAt || 0;
120
+ const mfaExpired = mfaExpiresAt > 0 && mfaExpiresAt < Date.now();
121
+
122
+ if (!mfaVerified || mfaExpired) {
123
+ console.log('[AUTH-GUARD] 2FA required', {
124
+ mfaVerified,
125
+ mfaExpired,
126
+ userId: sessionData.userId,
127
+ pathname,
128
+ });
129
+ redirect(`${verifyCodeUrl}?callbackUrl=${callbackUrl}`);
130
+ }
131
+ }
132
+ } catch (error) {
133
+ // If we can't check 2FA config, fail closed
134
+ console.error('[AUTH-GUARD] 2FA config check failed:', error instanceof Error ? error.message : String(error));
135
+ redirect(serviceUnavailableUrl);
136
+ }
137
+
138
+ // --- Custom checks (beta, admin, etc.) ---
139
+ if (options?.checks) {
140
+ for (const check of options.checks) {
141
+ try {
142
+ const redirectUrl = await check.check(sessionData, pathname);
143
+ if (redirectUrl) {
144
+ console.log(`[AUTH-GUARD] Custom check "${check.name}" failed`, {
145
+ userId: sessionData.userId,
146
+ pathname,
147
+ redirectUrl,
148
+ });
149
+ redirect(redirectUrl);
150
+ }
151
+ } catch (error) {
152
+ // If the error is a redirect (from next/navigation), re-throw it
153
+ if (error && typeof error === 'object' && 'digest' in error) {
154
+ throw error;
155
+ }
156
+ console.error(`[AUTH-GUARD] Custom check "${check.name}" error:`, error instanceof Error ? error.message : String(error));
157
+ redirect(serviceUnavailableUrl);
158
+ }
159
+ }
160
+ }
161
+
162
+ // --- All checks passed ---
163
+ return {
164
+ userId: sessionData.userId,
165
+ email: sessionData.email,
166
+ roles: sessionData.roles || [],
167
+ sessionData,
168
+ accessToken: sessionData.idpAccessToken,
169
+ };
170
+ }
@@ -0,0 +1,91 @@
1
+ /**
2
+ * Server-Side Session Decoder
3
+ *
4
+ * Reads the JWT session cookie, decodes it with jose, and fetches the
5
+ * full session from Redis. Used by authGuard (layouts) and withAuth (API routes).
6
+ *
7
+ * Zero HTTP self-fetches. Direct Redis reads only.
8
+ */
9
+
10
+ import 'server-only';
11
+ import { cookies } from 'next/headers';
12
+ import { jwtVerify, type JWTPayload } from 'jose';
13
+ import { getSession, type SessionData } from '../lib/session-store';
14
+ import { getIDPClientConfig } from '../lib/idp-client-config';
15
+ import { getSessionCookieName, getSecureSessionCookieName } from '../lib/app-slug';
16
+ import { ensureInitialized } from '../lib/startup-init';
17
+
18
+ export interface DecodedSession {
19
+ sessionData: SessionData;
20
+ jwtPayload: JWTPayload & { sessionToken?: string; redisSessionId?: string };
21
+ }
22
+
23
+ /**
24
+ * Decode the session from cookies and Redis.
25
+ * Returns null if no valid session exists.
26
+ *
27
+ * @param requestCookies Optional cookie getter for API route context (NextRequest.cookies).
28
+ * If omitted, uses next/headers cookies() for server components.
29
+ */
30
+ export async function decodeSession(
31
+ requestCookies?: { get: (name: string) => { value: string } | undefined }
32
+ ): Promise<DecodedSession | null> {
33
+ try {
34
+ // Ensure startup initialization is complete (Redis, IDP config, etc.)
35
+ await ensureInitialized();
36
+
37
+ // Get the JWT cookie value
38
+ const cookieStore = requestCookies || (await cookies());
39
+ const sessionCookieName = getSessionCookieName();
40
+ const secureCookieName = getSecureSessionCookieName();
41
+
42
+ const cookieValue =
43
+ cookieStore.get(secureCookieName)?.value ||
44
+ cookieStore.get(sessionCookieName)?.value;
45
+
46
+ if (!cookieValue) {
47
+ return null;
48
+ }
49
+
50
+ // Get the NextAuth secret from IDP config
51
+ const config = await getIDPClientConfig();
52
+ const secret = config.nextAuthSecret;
53
+ if (!secret) {
54
+ console.error('[DECODE-SESSION] No nextAuthSecret available from IDP config');
55
+ return null;
56
+ }
57
+
58
+ // Decode the JWT (same pattern as test-aware-get-token.ts)
59
+ const secretKey = new TextEncoder().encode(secret);
60
+ let payload: JWTPayload;
61
+ try {
62
+ const result = await jwtVerify(cookieValue, secretKey);
63
+ payload = result.payload;
64
+ } catch (jwtError) {
65
+ // JWT decode failed - cookie may be corrupted or secret rotated
66
+ console.warn('[DECODE-SESSION] JWT verification failed:', jwtError instanceof Error ? jwtError.message : String(jwtError));
67
+ return null;
68
+ }
69
+
70
+ // Extract the Redis session ID from JWT payload
71
+ const sessionToken = (payload as any).sessionToken || (payload as any).redisSessionId;
72
+ if (!sessionToken) {
73
+ console.warn('[DECODE-SESSION] JWT payload missing sessionToken/redisSessionId');
74
+ return null;
75
+ }
76
+
77
+ // Fetch session from Redis (direct, no HTTP)
78
+ const sessionData = await getSession(sessionToken);
79
+ if (!sessionData) {
80
+ return null;
81
+ }
82
+
83
+ return {
84
+ sessionData,
85
+ jwtPayload: payload as DecodedSession['jwtPayload'],
86
+ };
87
+ } catch (error) {
88
+ console.error('[DECODE-SESSION] Error:', error instanceof Error ? error.message : String(error));
89
+ return null;
90
+ }
91
+ }
@@ -0,0 +1,117 @@
1
+ /**
2
+ * Slim Middleware — Cookie-Only Auth Check
3
+ *
4
+ * Replaces the self-fetching middleware with a cookie existence check.
5
+ * All real auth validation happens in server-side layouts (authGuard).
6
+ *
7
+ * Zero self-fetches. Zero Redis calls. Zero JWT decoding.
8
+ * Just: does the session cookie exist? Yes → pass through. No → redirect to login.
9
+ */
10
+
11
+ import { NextRequest, NextResponse } from 'next/server';
12
+ import { getSessionCookieName, getSecureSessionCookieName } from '../lib/app-slug';
13
+
14
+ // =============================================================================
15
+ // TYPES
16
+ // =============================================================================
17
+
18
+ export interface SlimMiddlewareOptions {
19
+ /** Routes that don't require authentication (glob-style patterns) */
20
+ publicRoutes?: string[];
21
+ /** Login page URL (default: /account-auth/login) */
22
+ loginUrl?: string;
23
+ /** Additional paths to always bypass (e.g., /api/auth/, /api/session/) */
24
+ bypassPrefixes?: string[];
25
+ }
26
+
27
+ // =============================================================================
28
+ // DEFAULT BYPASS PATHS
29
+ // =============================================================================
30
+
31
+ /** Routes that must always bypass middleware (prevent infinite loops) */
32
+ const DEFAULT_BYPASS_PREFIXES = [
33
+ '/api/auth/',
34
+ '/api/session/',
35
+ '/_next/',
36
+ '/favicon.ico',
37
+ ];
38
+
39
+ /** Static file extensions to bypass */
40
+ const STATIC_EXTENSIONS = /\.(svg|png|jpg|jpeg|gif|webp|ico|css|js|woff|woff2|ttf|eot|map)$/i;
41
+
42
+ // =============================================================================
43
+ // MAIN
44
+ // =============================================================================
45
+
46
+ /**
47
+ * Create a slim middleware that only checks cookie existence.
48
+ * Auth validation is deferred to server-side layouts (authGuard).
49
+ */
50
+ export function createSlimMiddleware(options?: SlimMiddlewareOptions) {
51
+ const publicRoutes = options?.publicRoutes || [];
52
+ const loginUrl = options?.loginUrl || '/account-auth/login';
53
+ const extraBypass = options?.bypassPrefixes || [];
54
+ const allBypass = [...DEFAULT_BYPASS_PREFIXES, ...extraBypass];
55
+
56
+ // Pre-compile public route patterns for fast matching
57
+ const publicMatchers = publicRoutes.map(pattern => {
58
+ if (pattern.endsWith('/*')) {
59
+ const prefix = pattern.slice(0, -2);
60
+ return (p: string) => p === prefix || p.startsWith(prefix + '/');
61
+ }
62
+ if (pattern.endsWith('*')) {
63
+ const prefix = pattern.slice(0, -1);
64
+ return (p: string) => p.startsWith(prefix);
65
+ }
66
+ if (pattern.startsWith('/*.')) {
67
+ const ext = pattern.slice(2);
68
+ return (p: string) => p.endsWith(ext);
69
+ }
70
+ return (p: string) => p === pattern;
71
+ });
72
+
73
+ return function middleware(request: NextRequest): NextResponse {
74
+ const { pathname } = request.nextUrl;
75
+
76
+ // 1. Always bypass static/internal routes
77
+ if (STATIC_EXTENSIONS.test(pathname)) {
78
+ return NextResponse.next();
79
+ }
80
+ for (const prefix of allBypass) {
81
+ if (pathname.startsWith(prefix)) {
82
+ return NextResponse.next();
83
+ }
84
+ }
85
+
86
+ // 2. Check if it's a public route → pass through
87
+ for (const matcher of publicMatchers) {
88
+ if (matcher(pathname)) {
89
+ return NextResponse.next();
90
+ }
91
+ }
92
+
93
+ // 3. THE ONLY AUTH CHECK: Does a session cookie exist?
94
+ const sessionCookieName = getSessionCookieName();
95
+ const secureCookieName = getSecureSessionCookieName();
96
+ const hasCookie =
97
+ request.cookies.has(sessionCookieName) ||
98
+ request.cookies.has(secureCookieName);
99
+
100
+ if (!hasCookie) {
101
+ // No cookie on a protected route → redirect to login
102
+ // API routes get 401 instead of redirect
103
+ if (pathname.startsWith('/api/')) {
104
+ return NextResponse.json(
105
+ { error: 'Unauthorized', message: 'No session' },
106
+ { status: 401 }
107
+ );
108
+ }
109
+
110
+ const callbackUrl = encodeURIComponent(pathname);
111
+ return NextResponse.redirect(new URL(`${loginUrl}?callbackUrl=${callbackUrl}`, request.url));
112
+ }
113
+
114
+ // Cookie exists → pass through, layout authGuard does the real validation
115
+ return NextResponse.next();
116
+ };
117
+ }