@payez/next-mvp 3.3.0 → 3.5.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,83 @@
1
+ 'use client';
2
+
3
+ import React, { Suspense } from 'react';
4
+ import Link from 'next/link';
5
+ import { useBranding, useColors } from '../../theme/useTheme';
6
+
7
+ interface ComingSoonPageProps {
8
+ homeUrl?: string;
9
+ /** Override logo — pass a React node (e.g. inline SVG or themed <img>) */
10
+ logo?: React.ReactNode;
11
+ }
12
+
13
+ function ComingSoonContent({ homeUrl = '/', logo }: ComingSoonPageProps) {
14
+ const branding = useBranding();
15
+ const colors = useColors();
16
+
17
+ const fallbackLogo = branding.logo?.dark || branding.logo?.light;
18
+ const logoAlt = branding.logo?.alt || branding.appName || 'App Logo';
19
+ const logoHeight = branding.logo?.height || 48;
20
+
21
+ return (
22
+ <div className="min-h-screen flex items-center justify-center p-4">
23
+ <div
24
+ className="max-w-md w-full text-center rounded-xl p-8 shadow-lg border"
25
+ style={{
26
+ backgroundColor: 'var(--bg-card, #ffffff)',
27
+ borderColor: 'var(--border-default, #e5e7eb)',
28
+ }}
29
+ >
30
+ <div className="mb-6 flex justify-center">
31
+ {logo || (fallbackLogo && (
32
+ <img
33
+ src={fallbackLogo}
34
+ alt={logoAlt}
35
+ style={{ height: logoHeight }}
36
+ />
37
+ ))}
38
+ </div>
39
+
40
+ <h1
41
+ className="text-2xl font-bold mb-2"
42
+ style={{ color: 'var(--text-primary, #111827)' }}
43
+ >
44
+ {branding.appName || 'Our App'}
45
+ </h1>
46
+
47
+ <span
48
+ className="inline-flex items-center px-3 py-1 text-xs font-medium rounded-full lowercase tracking-wide border mb-4"
49
+ style={{
50
+ borderColor: colors.primary || '#3b82f6',
51
+ color: colors.primary || '#3b82f6',
52
+ }}
53
+ >
54
+ coming soon
55
+ </span>
56
+
57
+ <p
58
+ className="mb-6"
59
+ style={{ color: 'var(--text-secondary, #6b7280)' }}
60
+ >
61
+ We&apos;re currently in beta and access is limited to approved users.
62
+ Check back soon &mdash; we&apos;re working hard to open the doors!
63
+ </p>
64
+
65
+ <Link
66
+ href={homeUrl}
67
+ className="inline-block w-full font-medium py-3 px-4 rounded-lg transition-colors text-white"
68
+ style={{ backgroundColor: colors.primary || '#3b82f6' }}
69
+ >
70
+ Go to Home
71
+ </Link>
72
+ </div>
73
+ </div>
74
+ );
75
+ }
76
+
77
+ export default function ComingSoonPage(props: ComingSoonPageProps) {
78
+ return (
79
+ <Suspense>
80
+ <ComingSoonContent {...props} />
81
+ </Suspense>
82
+ );
83
+ }
@@ -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
+ }
@@ -0,0 +1,93 @@
1
+ /**
2
+ * Server-Side Auth Wrapper for API Routes & Server Actions
3
+ *
4
+ * Wraps route handlers with session validation. Uses direct Redis reads.
5
+ * Zero HTTP self-fetches.
6
+ *
7
+ * Usage:
8
+ * export const GET = withAuth(async (req, auth) => {
9
+ * return NextResponse.json({ userId: auth.userId });
10
+ * });
11
+ *
12
+ * // With role requirement:
13
+ * export const POST = withAuth(async (req, auth) => { ... }, { requiredRoles: ['admin'] });
14
+ */
15
+
16
+ import 'server-only';
17
+ import { NextRequest, NextResponse } from 'next/server';
18
+ import { decodeSession } from './decode-session';
19
+ import type { SessionData } from '../lib/session-store';
20
+
21
+ // =============================================================================
22
+ // TYPES
23
+ // =============================================================================
24
+
25
+ export interface ApiAuthResult {
26
+ userId: string;
27
+ email: string;
28
+ roles: string[];
29
+ sessionData: SessionData;
30
+ accessToken?: string;
31
+ }
32
+
33
+ export interface WithAuthOptions {
34
+ /** Roles required to access the route (any match = allowed) */
35
+ requiredRoles?: string[];
36
+ }
37
+
38
+ // =============================================================================
39
+ // MAIN
40
+ // =============================================================================
41
+
42
+ /**
43
+ * Wrap an API route handler with auth validation.
44
+ * Returns 401 if not authenticated, 403 if missing required roles.
45
+ */
46
+ export function withAuth(
47
+ handler: (req: NextRequest, auth: ApiAuthResult) => Promise<NextResponse>,
48
+ options?: WithAuthOptions
49
+ ): (req: NextRequest) => Promise<NextResponse> {
50
+ return async (req: NextRequest): Promise<NextResponse> => {
51
+ try {
52
+ // Decode session from request cookies (direct Redis, no self-fetch)
53
+ const decoded = await decodeSession(req.cookies);
54
+
55
+ if (!decoded) {
56
+ return NextResponse.json(
57
+ { error: 'Unauthorized', message: 'No valid session' },
58
+ { status: 401 }
59
+ );
60
+ }
61
+
62
+ const { sessionData } = decoded;
63
+
64
+ // Check required roles
65
+ if (options?.requiredRoles && options.requiredRoles.length > 0) {
66
+ const userRoles = sessionData.roles || [];
67
+ const hasRole = options.requiredRoles.some(r => userRoles.includes(r));
68
+ if (!hasRole) {
69
+ return NextResponse.json(
70
+ { error: 'Forbidden', message: 'Insufficient permissions' },
71
+ { status: 403 }
72
+ );
73
+ }
74
+ }
75
+
76
+ const auth: ApiAuthResult = {
77
+ userId: sessionData.userId,
78
+ email: sessionData.email,
79
+ roles: sessionData.roles || [],
80
+ sessionData,
81
+ accessToken: sessionData.idpAccessToken,
82
+ };
83
+
84
+ return handler(req, auth);
85
+ } catch (error) {
86
+ console.error('[WITH-AUTH] Error:', error instanceof Error ? error.message : String(error));
87
+ return NextResponse.json(
88
+ { error: 'Internal Server Error', message: 'Auth check failed' },
89
+ { status: 500 }
90
+ );
91
+ }
92
+ };
93
+ }