@payez/next-mvp 4.0.20 → 4.0.22
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/dist/api-handlers/session/viability.js +9 -1
- package/dist/auth/better-auth.js +1 -1
- package/dist/client/AuthContext.d.ts +1 -2
- package/dist/client/AuthContext.js +2 -2
- package/dist/client/better-auth-client.d.ts +8 -0
- package/dist/client/better-auth-client.js +15 -0
- package/dist/components/account/MobileNavDrawer.js +1 -1
- package/dist/hooks/useAvailableProviders.d.ts +4 -5
- package/dist/hooks/useAvailableProviders.js +7 -8
- package/dist/hooks/usePublicAuthSettings.d.ts +4 -4
- package/dist/hooks/usePublicAuthSettings.js +6 -6
- package/dist/lib/idp-client-config.d.ts +4 -0
- package/dist/lib/idp-client-config.js +14 -0
- package/dist/lib/session-store.d.ts +9 -0
- package/dist/lib/session-store.js +65 -21
- package/dist/lib/startup-init.js +21 -19
- package/dist/routes/auth/settings.d.ts +1 -1
- package/dist/routes/auth/settings.js +2 -2
- package/dist/server/auth.js +1 -1
- package/package.json +1 -1
- package/src/api-handlers/session/viability.ts +136 -128
- package/src/auth/better-auth.ts +271 -271
- package/src/client/AuthContext.tsx +3 -4
- package/src/client/better-auth-client.ts +14 -0
- package/src/components/account/MobileNavDrawer.tsx +2 -2
- package/src/hooks/useAvailableProviders.ts +5 -7
- package/src/hooks/usePublicAuthSettings.ts +6 -6
- package/src/lib/idp-client-config.ts +539 -526
- package/src/lib/session-store.ts +689 -645
- package/src/lib/startup-init.ts +246 -243
- package/src/routes/auth/settings.ts +3 -3
- package/src/server/auth.ts +81 -81
|
@@ -1,129 +1,137 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Session Viability Check API Handler for `@payez/next-mvp`
|
|
3
|
-
*
|
|
4
|
-
* This API route is called by the middleware to securely check if a session is valid.
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import { NextRequest, NextResponse } from 'next/server';
|
|
8
|
-
import { getSession as getRedisSession } from '../../lib/session-store';
|
|
9
|
-
import { getSession } from '../../server/auth';
|
|
10
|
-
import { isInitializationFailed, ensureInitialized } from '../../lib/startup-init';
|
|
11
|
-
import { getIDPClientConfig } from '../../lib/idp-client-config';
|
|
12
|
-
|
|
13
|
-
export async function GET(req: NextRequest) {
|
|
14
|
-
try {
|
|
15
|
-
// Ensure initialization is complete
|
|
16
|
-
if (!process.env.NEXTAUTH_SECRET) {
|
|
17
|
-
try {
|
|
18
|
-
await ensureInitialized();
|
|
19
|
-
} catch (error) {
|
|
20
|
-
// Initialization failed - return 503
|
|
21
|
-
console.error('[API Viability] Initialization failed - returning 503');
|
|
22
|
-
return NextResponse.json(
|
|
23
|
-
{
|
|
24
|
-
error: 'Service Unavailable',
|
|
25
|
-
message: 'Authentication service is not properly configured',
|
|
26
|
-
code: 'AUTH_NOT_INITIALIZED'
|
|
27
|
-
},
|
|
28
|
-
{ status: 503 }
|
|
29
|
-
);
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
// Double-check after initialization attempt
|
|
34
|
-
if (isInitializationFailed()) {
|
|
35
|
-
console.error('[API Viability] Initialization failed - returning 503');
|
|
36
|
-
return NextResponse.json(
|
|
37
|
-
{
|
|
38
|
-
error: 'Service Unavailable',
|
|
39
|
-
message: 'Authentication service is not properly configured',
|
|
40
|
-
code: 'AUTH_NOT_INITIALIZED'
|
|
41
|
-
},
|
|
42
|
-
{ status: 503 }
|
|
43
|
-
);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
// Get session from Better Auth
|
|
47
|
-
const betterAuthSession = await getSession(req);
|
|
48
|
-
|
|
49
|
-
// Debug logging
|
|
50
|
-
if (!betterAuthSession) {
|
|
51
|
-
console.warn('[VIABILITY] getSession returned null');
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
const sessionToken = betterAuthSession?.session?.token as string | undefined;
|
|
55
|
-
if (betterAuthSession && sessionToken) {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Session Viability Check API Handler for `@payez/next-mvp`
|
|
3
|
+
*
|
|
4
|
+
* This API route is called by the middleware to securely check if a session is valid.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { NextRequest, NextResponse } from 'next/server';
|
|
8
|
+
import { getSession as getRedisSession, getBetterAuthSession } from '../../lib/session-store';
|
|
9
|
+
import { getSession } from '../../server/auth';
|
|
10
|
+
import { isInitializationFailed, ensureInitialized } from '../../lib/startup-init';
|
|
11
|
+
import { getIDPClientConfig } from '../../lib/idp-client-config';
|
|
12
|
+
|
|
13
|
+
export async function GET(req: NextRequest) {
|
|
14
|
+
try {
|
|
15
|
+
// Ensure initialization is complete
|
|
16
|
+
if (!process.env.NEXTAUTH_SECRET) {
|
|
17
|
+
try {
|
|
18
|
+
await ensureInitialized();
|
|
19
|
+
} catch (error) {
|
|
20
|
+
// Initialization failed - return 503
|
|
21
|
+
console.error('[API Viability] Initialization failed - returning 503');
|
|
22
|
+
return NextResponse.json(
|
|
23
|
+
{
|
|
24
|
+
error: 'Service Unavailable',
|
|
25
|
+
message: 'Authentication service is not properly configured',
|
|
26
|
+
code: 'AUTH_NOT_INITIALIZED'
|
|
27
|
+
},
|
|
28
|
+
{ status: 503 }
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Double-check after initialization attempt
|
|
34
|
+
if (isInitializationFailed()) {
|
|
35
|
+
console.error('[API Viability] Initialization failed - returning 503');
|
|
36
|
+
return NextResponse.json(
|
|
37
|
+
{
|
|
38
|
+
error: 'Service Unavailable',
|
|
39
|
+
message: 'Authentication service is not properly configured',
|
|
40
|
+
code: 'AUTH_NOT_INITIALIZED'
|
|
41
|
+
},
|
|
42
|
+
{ status: 503 }
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// Get session from Better Auth
|
|
47
|
+
const betterAuthSession = await getSession(req);
|
|
48
|
+
|
|
49
|
+
// Debug logging
|
|
50
|
+
if (!betterAuthSession) {
|
|
51
|
+
console.warn('[VIABILITY] getSession returned null');
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const sessionToken = betterAuthSession?.session?.token as string | undefined;
|
|
55
|
+
if (betterAuthSession && sessionToken) {
|
|
56
|
+
// Try legacy session store first, then Better Auth format
|
|
57
|
+
let sessionData = await getRedisSession(sessionToken);
|
|
58
|
+
if (!sessionData) {
|
|
59
|
+
// Better Auth stores sessions with ba:{appSlug}:{token} prefix
|
|
60
|
+
sessionData = await getBetterAuthSession(sessionToken);
|
|
61
|
+
if (sessionData) {
|
|
62
|
+
console.log('[VIABILITY] Found session in Better Auth store');
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
if (sessionData) {
|
|
66
|
+
// The session exists in Redis
|
|
67
|
+
|
|
68
|
+
// Check if access token is expired (for middleware decision-making)
|
|
69
|
+
const accessTokenExpires = sessionData.idpAccessTokenExpires || 0;
|
|
70
|
+
const accessTokenExpired = accessTokenExpires < Date.now();
|
|
71
|
+
|
|
72
|
+
// Get requires2FA from cached client config (not session)
|
|
73
|
+
// This is a client-wide setting from the broker handshake
|
|
74
|
+
let requires2FA = true; // Default to true for security
|
|
75
|
+
try {
|
|
76
|
+
const cachedConfig = await getIDPClientConfig();
|
|
77
|
+
requires2FA = cachedConfig.authSettings?.require2FA ?? true;
|
|
78
|
+
} catch (e) {
|
|
79
|
+
console.warn('[API Viability] Could not get client config, defaulting requires2FA to true');
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// CRITICAL: Check if MFA has expired (2FA TTL enforcement)
|
|
83
|
+
// The session may have mfaVerified=true from days ago, but if mfaExpiresAt
|
|
84
|
+
// has passed, we must treat 2FA as incomplete to force re-verification.
|
|
85
|
+
const mfaExpiresAt = sessionData.mfaExpiresAt || 0;
|
|
86
|
+
const mfaExpired = mfaExpiresAt > 0 && mfaExpiresAt < Date.now();
|
|
87
|
+
// Check both field names for compatibility (mfaVerified is the normalized name)
|
|
88
|
+
const sessionMfaComplete = sessionData.mfaVerified ?? (sessionData as any).twoFactorComplete ?? false;
|
|
89
|
+
const effectiveTwoFactorComplete = sessionMfaComplete && !mfaExpired;
|
|
90
|
+
|
|
91
|
+
console.log('[VIABILITY] Session 2FA check:', {
|
|
92
|
+
sessionToken: sessionToken.substring(0, 8) + '...',
|
|
93
|
+
mfaVerified: sessionData.mfaVerified,
|
|
94
|
+
twoFactorComplete: (sessionData as any).twoFactorComplete,
|
|
95
|
+
sessionMfaComplete,
|
|
96
|
+
mfaExpired,
|
|
97
|
+
effectiveTwoFactorComplete,
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
if (mfaExpired && sessionMfaComplete) {
|
|
101
|
+
console.warn('[API Viability] MFA expired - forcing 2FA re-verification', {
|
|
102
|
+
mfaExpiresAt: new Date(mfaExpiresAt).toISOString(),
|
|
103
|
+
now: new Date().toISOString(),
|
|
104
|
+
hoursExpiredAgo: ((Date.now() - mfaExpiresAt) / (1000 * 60 * 60)).toFixed(1)
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const response = {
|
|
109
|
+
authenticated: true,
|
|
110
|
+
sessionToken, // Include token for middleware tracking
|
|
111
|
+
// 2FA fields - critical for middleware redirect logic
|
|
112
|
+
requires2FA, // From cached client config (client-wide setting)
|
|
113
|
+
twoFactorComplete: effectiveTwoFactorComplete, // From session, BUT respects MFA TTL
|
|
114
|
+
// Token status for refresh decisions
|
|
115
|
+
accessTokenExpired,
|
|
116
|
+
hasRefreshToken: !!sessionData.idpRefreshToken
|
|
117
|
+
};
|
|
118
|
+
return NextResponse.json(response);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// CRITICAL: Cookie exists but Redis session is missing (stale cookie state)
|
|
122
|
+
// Return sessionToken so middleware can detect this and clear the stale cookie
|
|
123
|
+
console.warn('[VIABILITY] Stale cookie detected - session not in Redis');
|
|
124
|
+
return NextResponse.json({
|
|
125
|
+
authenticated: false,
|
|
126
|
+
sessionToken // Include token to enable stale cookie detection
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// If there's no token at all, it's not authenticated
|
|
131
|
+
return NextResponse.json({ authenticated: false });
|
|
132
|
+
|
|
133
|
+
} catch (error) {
|
|
134
|
+
console.error('[API Viability] Error checking session viability:', error);
|
|
135
|
+
return NextResponse.json({ authenticated: false }, { status: 500 });
|
|
136
|
+
}
|
|
129
137
|
}
|