@payez/next-mvp 4.0.0 → 4.0.2

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.
Files changed (81) hide show
  1. package/dist/api-handlers/account/change-password.js +110 -110
  2. package/dist/api-handlers/admin/analytics.d.ts +19 -19
  3. package/dist/api-handlers/admin/analytics.js +378 -378
  4. package/dist/api-handlers/admin/audit.d.ts +19 -19
  5. package/dist/api-handlers/admin/audit.js +213 -213
  6. package/dist/api-handlers/admin/index.d.ts +21 -21
  7. package/dist/api-handlers/admin/index.js +42 -42
  8. package/dist/api-handlers/admin/redis-sessions.d.ts +35 -35
  9. package/dist/api-handlers/admin/redis-sessions.js +203 -203
  10. package/dist/api-handlers/admin/sessions.d.ts +20 -20
  11. package/dist/api-handlers/admin/sessions.js +283 -283
  12. package/dist/api-handlers/admin/site-logs.d.ts +45 -45
  13. package/dist/api-handlers/admin/site-logs.js +317 -317
  14. package/dist/api-handlers/admin/stats.d.ts +20 -20
  15. package/dist/api-handlers/admin/stats.js +239 -239
  16. package/dist/api-handlers/admin/users.d.ts +19 -19
  17. package/dist/api-handlers/admin/users.js +221 -221
  18. package/dist/api-handlers/admin/vibe-data.d.ts +79 -79
  19. package/dist/api-handlers/admin/vibe-data.js +267 -267
  20. package/dist/api-handlers/auth/refresh.js +633 -633
  21. package/dist/api-handlers/auth/signout.js +186 -186
  22. package/dist/api-handlers/auth/verify-code.d.ts +43 -43
  23. package/dist/api-handlers/auth/verify-code.js +90 -90
  24. package/dist/api-handlers/session/viability.js +114 -114
  25. package/dist/api-handlers/test/force-expire.js +59 -59
  26. package/dist/auth/auth-decision.js +182 -182
  27. package/dist/auth/utils/token-utils.d.ts +83 -83
  28. package/dist/auth/utils/token-utils.js +218 -218
  29. package/dist/client/AuthContext.js +115 -115
  30. package/dist/client/better-auth-client.d.ts +1020 -1020
  31. package/dist/components/SessionSync.js +121 -121
  32. package/dist/components/account/MobileNavDrawer.js +64 -64
  33. package/dist/components/account/UserAvatarMenu.js +91 -91
  34. package/dist/components/admin/VibeAdminLayout.js +71 -71
  35. package/dist/hooks/useAuthSettings.js +93 -93
  36. package/dist/hooks/useAvailableProviders.d.ts +43 -43
  37. package/dist/hooks/useAvailableProviders.js +112 -112
  38. package/dist/lib/app-slug.d.ts +95 -95
  39. package/dist/lib/app-slug.js +172 -172
  40. package/dist/lib/test-aware-get-token.js +86 -86
  41. package/dist/lib/token-lifecycle.d.ts +78 -78
  42. package/dist/lib/token-lifecycle.js +360 -360
  43. package/dist/pages/admin-login/page.js +73 -73
  44. package/dist/pages/client-admin/ClientSiteAdminPage.js +179 -179
  45. package/dist/pages/login/page.js +202 -202
  46. package/dist/pages/showcase/ShowcasePage.js +142 -142
  47. package/dist/pages/test-env/EmergencyLogoutPage.js +99 -99
  48. package/dist/pages/test-env/JwtInspectPage.js +116 -116
  49. package/dist/pages/test-env/TestEnvPage.js +51 -51
  50. package/dist/pages/verify-code/page.js +412 -412
  51. package/dist/routes/auth/logout.d.ts +31 -31
  52. package/dist/routes/auth/logout.js +98 -98
  53. package/dist/routes/auth/session.js +157 -157
  54. package/dist/routes/auth/viability.js +190 -190
  55. package/package.json +6 -16
  56. package/dist/auth/auth-options.d.ts +0 -57
  57. package/dist/auth/auth-options.js +0 -213
  58. package/dist/auth/callbacks/index.d.ts +0 -6
  59. package/dist/auth/callbacks/index.js +0 -12
  60. package/dist/auth/callbacks/jwt.d.ts +0 -45
  61. package/dist/auth/callbacks/jwt.js +0 -305
  62. package/dist/auth/callbacks/session.d.ts +0 -60
  63. package/dist/auth/callbacks/session.js +0 -170
  64. package/dist/auth/callbacks/signin.d.ts +0 -23
  65. package/dist/auth/callbacks/signin.js +0 -44
  66. package/dist/auth/events/index.d.ts +0 -4
  67. package/dist/auth/events/index.js +0 -8
  68. package/dist/auth/events/signout.d.ts +0 -17
  69. package/dist/auth/events/signout.js +0 -32
  70. package/dist/auth/providers/credentials.d.ts +0 -32
  71. package/dist/auth/providers/credentials.js +0 -223
  72. package/dist/auth/providers/index.d.ts +0 -5
  73. package/dist/auth/providers/index.js +0 -21
  74. package/dist/auth/providers/oauth.d.ts +0 -26
  75. package/dist/auth/providers/oauth.js +0 -105
  76. package/dist/lib/nextauth-secret.d.ts +0 -10
  77. package/dist/lib/nextauth-secret.js +0 -100
  78. package/dist/pages/profile/profile-patch.d.ts +0 -1
  79. package/dist/pages/profile/profile-patch.js +0 -281
  80. package/dist/pages/security/security-patch.d.ts +0 -1
  81. package/dist/pages/security/security-patch.js +0 -302
@@ -1,114 +1,114 @@
1
- "use strict";
2
- /**
3
- * Session Viability Check API Handler for `@payez/next-mvp`
4
- *
5
- * This API route is called by the middleware to securely check if a session is valid.
6
- */
7
- Object.defineProperty(exports, "__esModule", { value: true });
8
- exports.GET = GET;
9
- const server_1 = require("next/server");
10
- const session_store_1 = require("../../lib/session-store");
11
- const auth_1 = require("../../server/auth");
12
- const startup_init_1 = require("../../lib/startup-init");
13
- const idp_client_config_1 = require("../../lib/idp-client-config");
14
- async function GET(req) {
15
- try {
16
- // Ensure initialization is complete
17
- if (!process.env.NEXTAUTH_SECRET) {
18
- try {
19
- await (0, startup_init_1.ensureInitialized)();
20
- }
21
- catch (error) {
22
- // Initialization failed - return 503
23
- console.error('[API Viability] Initialization failed - returning 503');
24
- return server_1.NextResponse.json({
25
- error: 'Service Unavailable',
26
- message: 'Authentication service is not properly configured',
27
- code: 'AUTH_NOT_INITIALIZED'
28
- }, { status: 503 });
29
- }
30
- }
31
- // Double-check after initialization attempt
32
- if ((0, startup_init_1.isInitializationFailed)()) {
33
- console.error('[API Viability] Initialization failed - returning 503');
34
- return server_1.NextResponse.json({
35
- error: 'Service Unavailable',
36
- message: 'Authentication service is not properly configured',
37
- code: 'AUTH_NOT_INITIALIZED'
38
- }, { status: 503 });
39
- }
40
- // Get session from Better Auth
41
- const betterAuthSession = await (0, auth_1.getSession)(req);
42
- // Debug logging
43
- if (!betterAuthSession) {
44
- console.warn('[VIABILITY] getSession returned null');
45
- }
46
- const sessionToken = betterAuthSession?.session?.token;
47
- if (betterAuthSession && sessionToken) {
48
- const sessionData = await (0, session_store_1.getSession)(sessionToken);
49
- if (sessionData) {
50
- // The session exists in Redis
51
- // Check if access token is expired (for middleware decision-making)
52
- const accessTokenExpires = sessionData.idpAccessTokenExpires || 0;
53
- const accessTokenExpired = accessTokenExpires < Date.now();
54
- // Get requires2FA from cached client config (not session)
55
- // This is a client-wide setting from the broker handshake
56
- let requires2FA = true; // Default to true for security
57
- try {
58
- const cachedConfig = await (0, idp_client_config_1.getIDPClientConfig)();
59
- requires2FA = cachedConfig.authSettings?.require2FA ?? true;
60
- }
61
- catch (e) {
62
- console.warn('[API Viability] Could not get client config, defaulting requires2FA to true');
63
- }
64
- // CRITICAL: Check if MFA has expired (2FA TTL enforcement)
65
- // The session may have mfaVerified=true from days ago, but if mfaExpiresAt
66
- // has passed, we must treat 2FA as incomplete to force re-verification.
67
- const mfaExpiresAt = sessionData.mfaExpiresAt || 0;
68
- const mfaExpired = mfaExpiresAt > 0 && mfaExpiresAt < Date.now();
69
- // Check both field names for compatibility (mfaVerified is the normalized name)
70
- const sessionMfaComplete = sessionData.mfaVerified ?? sessionData.twoFactorComplete ?? false;
71
- const effectiveTwoFactorComplete = sessionMfaComplete && !mfaExpired;
72
- console.log('[VIABILITY] Session 2FA check:', {
73
- sessionToken: sessionToken.substring(0, 8) + '...',
74
- mfaVerified: sessionData.mfaVerified,
75
- twoFactorComplete: sessionData.twoFactorComplete,
76
- sessionMfaComplete,
77
- mfaExpired,
78
- effectiveTwoFactorComplete,
79
- });
80
- if (mfaExpired && sessionMfaComplete) {
81
- console.warn('[API Viability] MFA expired - forcing 2FA re-verification', {
82
- mfaExpiresAt: new Date(mfaExpiresAt).toISOString(),
83
- now: new Date().toISOString(),
84
- hoursExpiredAgo: ((Date.now() - mfaExpiresAt) / (1000 * 60 * 60)).toFixed(1)
85
- });
86
- }
87
- const response = {
88
- authenticated: true,
89
- sessionToken, // Include token for middleware tracking
90
- // 2FA fields - critical for middleware redirect logic
91
- requires2FA, // From cached client config (client-wide setting)
92
- twoFactorComplete: effectiveTwoFactorComplete, // From session, BUT respects MFA TTL
93
- // Token status for refresh decisions
94
- accessTokenExpired,
95
- hasRefreshToken: !!sessionData.idpRefreshToken
96
- };
97
- return server_1.NextResponse.json(response);
98
- }
99
- // CRITICAL: Cookie exists but Redis session is missing (stale cookie state)
100
- // Return sessionToken so middleware can detect this and clear the stale cookie
101
- console.warn('[VIABILITY] Stale cookie detected - session not in Redis');
102
- return server_1.NextResponse.json({
103
- authenticated: false,
104
- sessionToken // Include token to enable stale cookie detection
105
- });
106
- }
107
- // If there's no token at all, it's not authenticated
108
- return server_1.NextResponse.json({ authenticated: false });
109
- }
110
- catch (error) {
111
- console.error('[API Viability] Error checking session viability:', error);
112
- return server_1.NextResponse.json({ authenticated: false }, { status: 500 });
113
- }
114
- }
1
+ "use strict";
2
+ /**
3
+ * Session Viability Check API Handler for `@payez/next-mvp`
4
+ *
5
+ * This API route is called by the middleware to securely check if a session is valid.
6
+ */
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.GET = GET;
9
+ const server_1 = require("next/server");
10
+ const session_store_1 = require("../../lib/session-store");
11
+ const auth_1 = require("../../server/auth");
12
+ const startup_init_1 = require("../../lib/startup-init");
13
+ const idp_client_config_1 = require("../../lib/idp-client-config");
14
+ async function GET(req) {
15
+ try {
16
+ // Ensure initialization is complete
17
+ if (!process.env.NEXTAUTH_SECRET) {
18
+ try {
19
+ await (0, startup_init_1.ensureInitialized)();
20
+ }
21
+ catch (error) {
22
+ // Initialization failed - return 503
23
+ console.error('[API Viability] Initialization failed - returning 503');
24
+ return server_1.NextResponse.json({
25
+ error: 'Service Unavailable',
26
+ message: 'Authentication service is not properly configured',
27
+ code: 'AUTH_NOT_INITIALIZED'
28
+ }, { status: 503 });
29
+ }
30
+ }
31
+ // Double-check after initialization attempt
32
+ if ((0, startup_init_1.isInitializationFailed)()) {
33
+ console.error('[API Viability] Initialization failed - returning 503');
34
+ return server_1.NextResponse.json({
35
+ error: 'Service Unavailable',
36
+ message: 'Authentication service is not properly configured',
37
+ code: 'AUTH_NOT_INITIALIZED'
38
+ }, { status: 503 });
39
+ }
40
+ // Get session from Better Auth
41
+ const betterAuthSession = await (0, auth_1.getSession)(req);
42
+ // Debug logging
43
+ if (!betterAuthSession) {
44
+ console.warn('[VIABILITY] getSession returned null');
45
+ }
46
+ const sessionToken = betterAuthSession?.session?.token;
47
+ if (betterAuthSession && sessionToken) {
48
+ const sessionData = await (0, session_store_1.getSession)(sessionToken);
49
+ if (sessionData) {
50
+ // The session exists in Redis
51
+ // Check if access token is expired (for middleware decision-making)
52
+ const accessTokenExpires = sessionData.idpAccessTokenExpires || 0;
53
+ const accessTokenExpired = accessTokenExpires < Date.now();
54
+ // Get requires2FA from cached client config (not session)
55
+ // This is a client-wide setting from the broker handshake
56
+ let requires2FA = true; // Default to true for security
57
+ try {
58
+ const cachedConfig = await (0, idp_client_config_1.getIDPClientConfig)();
59
+ requires2FA = cachedConfig.authSettings?.require2FA ?? true;
60
+ }
61
+ catch (e) {
62
+ console.warn('[API Viability] Could not get client config, defaulting requires2FA to true');
63
+ }
64
+ // CRITICAL: Check if MFA has expired (2FA TTL enforcement)
65
+ // The session may have mfaVerified=true from days ago, but if mfaExpiresAt
66
+ // has passed, we must treat 2FA as incomplete to force re-verification.
67
+ const mfaExpiresAt = sessionData.mfaExpiresAt || 0;
68
+ const mfaExpired = mfaExpiresAt > 0 && mfaExpiresAt < Date.now();
69
+ // Check both field names for compatibility (mfaVerified is the normalized name)
70
+ const sessionMfaComplete = sessionData.mfaVerified ?? sessionData.twoFactorComplete ?? false;
71
+ const effectiveTwoFactorComplete = sessionMfaComplete && !mfaExpired;
72
+ console.log('[VIABILITY] Session 2FA check:', {
73
+ sessionToken: sessionToken.substring(0, 8) + '...',
74
+ mfaVerified: sessionData.mfaVerified,
75
+ twoFactorComplete: sessionData.twoFactorComplete,
76
+ sessionMfaComplete,
77
+ mfaExpired,
78
+ effectiveTwoFactorComplete,
79
+ });
80
+ if (mfaExpired && sessionMfaComplete) {
81
+ console.warn('[API Viability] MFA expired - forcing 2FA re-verification', {
82
+ mfaExpiresAt: new Date(mfaExpiresAt).toISOString(),
83
+ now: new Date().toISOString(),
84
+ hoursExpiredAgo: ((Date.now() - mfaExpiresAt) / (1000 * 60 * 60)).toFixed(1)
85
+ });
86
+ }
87
+ const response = {
88
+ authenticated: true,
89
+ sessionToken, // Include token for middleware tracking
90
+ // 2FA fields - critical for middleware redirect logic
91
+ requires2FA, // From cached client config (client-wide setting)
92
+ twoFactorComplete: effectiveTwoFactorComplete, // From session, BUT respects MFA TTL
93
+ // Token status for refresh decisions
94
+ accessTokenExpired,
95
+ hasRefreshToken: !!sessionData.idpRefreshToken
96
+ };
97
+ return server_1.NextResponse.json(response);
98
+ }
99
+ // CRITICAL: Cookie exists but Redis session is missing (stale cookie state)
100
+ // Return sessionToken so middleware can detect this and clear the stale cookie
101
+ console.warn('[VIABILITY] Stale cookie detected - session not in Redis');
102
+ return server_1.NextResponse.json({
103
+ authenticated: false,
104
+ sessionToken // Include token to enable stale cookie detection
105
+ });
106
+ }
107
+ // If there's no token at all, it's not authenticated
108
+ return server_1.NextResponse.json({ authenticated: false });
109
+ }
110
+ catch (error) {
111
+ console.error('[API Viability] Error checking session viability:', error);
112
+ return server_1.NextResponse.json({ authenticated: false }, { status: 500 });
113
+ }
114
+ }
@@ -1,59 +1,59 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.POST = void 0;
4
- const server_1 = require("next/server");
5
- const auth_1 = require("../../server/auth");
6
- const session_store_1 = require("../../lib/session-store");
7
- /**
8
- * Force-expire access token for testing refresh flow.
9
- *
10
- * Sets the access token expiry to 2 minutes in the past,
11
- * which will trigger a refresh on the next API call.
12
- *
13
- * Usage in consuming app:
14
- * ```typescript
15
- * // app/api/test/force-expire/route.ts
16
- * export { POST } from '@payez/next-mvp/api-handlers/test/force-expire';
17
- * ```
18
- */
19
- const POST = async (req) => {
20
- try {
21
- const betterAuthSession = await (0, auth_1.getSession)(req);
22
- let sessionToken = betterAuthSession?.session?.token;
23
- if (!sessionToken) {
24
- const headerSessionToken = req.headers.get('x-session-token') || req.headers.get('X-Session-Token');
25
- if (headerSessionToken) {
26
- sessionToken = headerSessionToken;
27
- }
28
- else {
29
- console.warn('[TEST_EXPIRE] No session token or X-Session-Token header');
30
- return server_1.NextResponse.json({ success: false, error: 'No session token' }, { status: 401 });
31
- }
32
- }
33
- const session = await (0, session_store_1.getSession)(sessionToken);
34
- if (!session) {
35
- return server_1.NextResponse.json({ success: false, error: 'Session not found' }, { status: 404 });
36
- }
37
- const now = Date.now();
38
- const forced = now - (2 * 60 * 1000); // two minutes ago
39
- const prev = session.idpAccessTokenExpires || null;
40
- await (0, session_store_1.updateSession)(sessionToken, { idpAccessTokenExpires: forced });
41
- console.log('[TEST_EXPIRE] Forced access token expiry for session', {
42
- sessionToken: sessionToken.substring(0, 8) + '...',
43
- previous: prev ? new Date(prev).toISOString() : null,
44
- newExpiry: new Date(forced).toISOString()
45
- });
46
- return server_1.NextResponse.json({
47
- success: true,
48
- previous: prev,
49
- previousIso: prev ? new Date(prev).toISOString() : null,
50
- newExpiry: forced,
51
- newExpiryIso: new Date(forced).toISOString()
52
- });
53
- }
54
- catch (e) {
55
- console.error('[TEST_EXPIRE] Error:', e);
56
- return server_1.NextResponse.json({ success: false, error: e instanceof Error ? e.message : String(e) }, { status: 500 });
57
- }
58
- };
59
- exports.POST = POST;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.POST = void 0;
4
+ const server_1 = require("next/server");
5
+ const auth_1 = require("../../server/auth");
6
+ const session_store_1 = require("../../lib/session-store");
7
+ /**
8
+ * Force-expire access token for testing refresh flow.
9
+ *
10
+ * Sets the access token expiry to 2 minutes in the past,
11
+ * which will trigger a refresh on the next API call.
12
+ *
13
+ * Usage in consuming app:
14
+ * ```typescript
15
+ * // app/api/test/force-expire/route.ts
16
+ * export { POST } from '@payez/next-mvp/api-handlers/test/force-expire';
17
+ * ```
18
+ */
19
+ const POST = async (req) => {
20
+ try {
21
+ const betterAuthSession = await (0, auth_1.getSession)(req);
22
+ let sessionToken = betterAuthSession?.session?.token;
23
+ if (!sessionToken) {
24
+ const headerSessionToken = req.headers.get('x-session-token') || req.headers.get('X-Session-Token');
25
+ if (headerSessionToken) {
26
+ sessionToken = headerSessionToken;
27
+ }
28
+ else {
29
+ console.warn('[TEST_EXPIRE] No session token or X-Session-Token header');
30
+ return server_1.NextResponse.json({ success: false, error: 'No session token' }, { status: 401 });
31
+ }
32
+ }
33
+ const session = await (0, session_store_1.getSession)(sessionToken);
34
+ if (!session) {
35
+ return server_1.NextResponse.json({ success: false, error: 'Session not found' }, { status: 404 });
36
+ }
37
+ const now = Date.now();
38
+ const forced = now - (2 * 60 * 1000); // two minutes ago
39
+ const prev = session.idpAccessTokenExpires || null;
40
+ await (0, session_store_1.updateSession)(sessionToken, { idpAccessTokenExpires: forced });
41
+ console.log('[TEST_EXPIRE] Forced access token expiry for session', {
42
+ sessionToken: sessionToken.substring(0, 8) + '...',
43
+ previous: prev ? new Date(prev).toISOString() : null,
44
+ newExpiry: new Date(forced).toISOString()
45
+ });
46
+ return server_1.NextResponse.json({
47
+ success: true,
48
+ previous: prev,
49
+ previousIso: prev ? new Date(prev).toISOString() : null,
50
+ newExpiry: forced,
51
+ newExpiryIso: new Date(forced).toISOString()
52
+ });
53
+ }
54
+ catch (e) {
55
+ console.error('[TEST_EXPIRE] Error:', e);
56
+ return server_1.NextResponse.json({ success: false, error: e instanceof Error ? e.message : String(e) }, { status: 500 });
57
+ }
58
+ };
59
+ exports.POST = POST;