@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.
- package/dist/api-handlers/account/change-password.js +110 -110
- package/dist/api-handlers/admin/analytics.d.ts +19 -19
- package/dist/api-handlers/admin/analytics.js +378 -378
- package/dist/api-handlers/admin/audit.d.ts +19 -19
- package/dist/api-handlers/admin/audit.js +213 -213
- package/dist/api-handlers/admin/index.d.ts +21 -21
- package/dist/api-handlers/admin/index.js +42 -42
- package/dist/api-handlers/admin/redis-sessions.d.ts +35 -35
- package/dist/api-handlers/admin/redis-sessions.js +203 -203
- package/dist/api-handlers/admin/sessions.d.ts +20 -20
- package/dist/api-handlers/admin/sessions.js +283 -283
- package/dist/api-handlers/admin/site-logs.d.ts +45 -45
- package/dist/api-handlers/admin/site-logs.js +317 -317
- package/dist/api-handlers/admin/stats.d.ts +20 -20
- package/dist/api-handlers/admin/stats.js +239 -239
- package/dist/api-handlers/admin/users.d.ts +19 -19
- package/dist/api-handlers/admin/users.js +221 -221
- package/dist/api-handlers/admin/vibe-data.d.ts +79 -79
- package/dist/api-handlers/admin/vibe-data.js +267 -267
- package/dist/api-handlers/auth/refresh.js +633 -633
- package/dist/api-handlers/auth/signout.js +186 -186
- package/dist/api-handlers/auth/verify-code.d.ts +43 -43
- package/dist/api-handlers/auth/verify-code.js +90 -90
- package/dist/api-handlers/session/viability.js +114 -114
- package/dist/api-handlers/test/force-expire.js +59 -59
- package/dist/auth/auth-decision.js +182 -182
- package/dist/auth/utils/token-utils.d.ts +83 -83
- package/dist/auth/utils/token-utils.js +218 -218
- package/dist/client/AuthContext.js +115 -115
- package/dist/client/better-auth-client.d.ts +1020 -1020
- package/dist/components/SessionSync.js +121 -121
- package/dist/components/account/MobileNavDrawer.js +64 -64
- package/dist/components/account/UserAvatarMenu.js +91 -91
- package/dist/components/admin/VibeAdminLayout.js +71 -71
- package/dist/hooks/useAuthSettings.js +93 -93
- package/dist/hooks/useAvailableProviders.d.ts +43 -43
- package/dist/hooks/useAvailableProviders.js +112 -112
- package/dist/lib/app-slug.d.ts +95 -95
- package/dist/lib/app-slug.js +172 -172
- package/dist/lib/test-aware-get-token.js +86 -86
- package/dist/lib/token-lifecycle.d.ts +78 -78
- package/dist/lib/token-lifecycle.js +360 -360
- package/dist/pages/admin-login/page.js +73 -73
- package/dist/pages/client-admin/ClientSiteAdminPage.js +179 -179
- package/dist/pages/login/page.js +202 -202
- package/dist/pages/showcase/ShowcasePage.js +142 -142
- package/dist/pages/test-env/EmergencyLogoutPage.js +99 -99
- package/dist/pages/test-env/JwtInspectPage.js +116 -116
- package/dist/pages/test-env/TestEnvPage.js +51 -51
- package/dist/pages/verify-code/page.js +412 -412
- package/dist/routes/auth/logout.d.ts +31 -31
- package/dist/routes/auth/logout.js +98 -98
- package/dist/routes/auth/session.js +157 -157
- package/dist/routes/auth/viability.js +190 -190
- package/package.json +6 -16
- package/dist/auth/auth-options.d.ts +0 -57
- package/dist/auth/auth-options.js +0 -213
- package/dist/auth/callbacks/index.d.ts +0 -6
- package/dist/auth/callbacks/index.js +0 -12
- package/dist/auth/callbacks/jwt.d.ts +0 -45
- package/dist/auth/callbacks/jwt.js +0 -305
- package/dist/auth/callbacks/session.d.ts +0 -60
- package/dist/auth/callbacks/session.js +0 -170
- package/dist/auth/callbacks/signin.d.ts +0 -23
- package/dist/auth/callbacks/signin.js +0 -44
- package/dist/auth/events/index.d.ts +0 -4
- package/dist/auth/events/index.js +0 -8
- package/dist/auth/events/signout.d.ts +0 -17
- package/dist/auth/events/signout.js +0 -32
- package/dist/auth/providers/credentials.d.ts +0 -32
- package/dist/auth/providers/credentials.js +0 -223
- package/dist/auth/providers/index.d.ts +0 -5
- package/dist/auth/providers/index.js +0 -21
- package/dist/auth/providers/oauth.d.ts +0 -26
- package/dist/auth/providers/oauth.js +0 -105
- package/dist/lib/nextauth-secret.d.ts +0 -10
- package/dist/lib/nextauth-secret.js +0 -100
- package/dist/pages/profile/profile-patch.d.ts +0 -1
- package/dist/pages/profile/profile-patch.js +0 -281
- package/dist/pages/security/security-patch.d.ts +0 -1
- package/dist/pages/security/security-patch.js +0 -302
|
@@ -1,218 +1,218 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* Token Utilities
|
|
4
|
-
*
|
|
5
|
-
* JWT decoding and expiry checking utilities.
|
|
6
|
-
*
|
|
7
|
-
* @version 1.0.0
|
|
8
|
-
* @since auth-refactor-2026-01
|
|
9
|
-
*/
|
|
10
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
-
exports.extractKidFromToken = exports.decodeJwtHeader = void 0;
|
|
12
|
-
exports.decodeIdpAccessToken = decodeIdpAccessToken;
|
|
13
|
-
exports.decodeIdpAccessTokenFull = decodeIdpAccessTokenFull;
|
|
14
|
-
exports.extractEmailFromToken = extractEmailFromToken;
|
|
15
|
-
exports.extractRolesFromToken = extractRolesFromToken;
|
|
16
|
-
exports.extractAmrFromToken = extractAmrFromToken;
|
|
17
|
-
exports.tokenNeedsRefresh = tokenNeedsRefresh;
|
|
18
|
-
exports.tokenIsExpired = tokenIsExpired;
|
|
19
|
-
exports.msUntilExpiry = msUntilExpiry;
|
|
20
|
-
exports.expClaimToMs = expClaimToMs;
|
|
21
|
-
exports.validateTokenExpiry = validateTokenExpiry;
|
|
22
|
-
const jwt_decode_1 = require("../../lib/jwt-decode");
|
|
23
|
-
// Re-export header utilities for consumers
|
|
24
|
-
var jwt_decode_2 = require("../../lib/jwt-decode");
|
|
25
|
-
Object.defineProperty(exports, "decodeJwtHeader", { enumerable: true, get: function () { return jwt_decode_2.decodeJwtHeader; } });
|
|
26
|
-
Object.defineProperty(exports, "extractKidFromToken", { enumerable: true, get: function () { return jwt_decode_2.extractKidFromToken; } });
|
|
27
|
-
// ============================================================================
|
|
28
|
-
// TOKEN DECODING
|
|
29
|
-
// ============================================================================
|
|
30
|
-
/**
|
|
31
|
-
* Decode an IDP access token and extract claims.
|
|
32
|
-
*
|
|
33
|
-
* @param token - The JWT access token from IDP
|
|
34
|
-
* @returns Decoded token claims, or null if decode fails
|
|
35
|
-
*/
|
|
36
|
-
function decodeIdpAccessToken(token) {
|
|
37
|
-
try {
|
|
38
|
-
return (0, jwt_decode_1.jwtDecode)(token);
|
|
39
|
-
}
|
|
40
|
-
catch (error) {
|
|
41
|
-
console.error('[TOKEN_UTILS] Failed to decode access token:', error);
|
|
42
|
-
return null;
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* Decode both JWT header and payload from an IDP access token.
|
|
47
|
-
* Returns the signing key ID (kid) along with payload claims.
|
|
48
|
-
*
|
|
49
|
-
* @param token - The JWT access token from IDP
|
|
50
|
-
* @returns Object with header (including kid) and payload, or null if decode fails
|
|
51
|
-
*/
|
|
52
|
-
function decodeIdpAccessTokenFull(token) {
|
|
53
|
-
try {
|
|
54
|
-
const header = (0, jwt_decode_1.decodeJwtHeader)(token);
|
|
55
|
-
const payload = (0, jwt_decode_1.jwtDecode)(token);
|
|
56
|
-
if (!header || !payload) {
|
|
57
|
-
return null;
|
|
58
|
-
}
|
|
59
|
-
return {
|
|
60
|
-
header,
|
|
61
|
-
payload,
|
|
62
|
-
bearerKeyId: header.kid,
|
|
63
|
-
};
|
|
64
|
-
}
|
|
65
|
-
catch (error) {
|
|
66
|
-
console.error('[TOKEN_UTILS] Failed to decode access token (full):', error);
|
|
67
|
-
return null;
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
/**
|
|
71
|
-
* Extract user email from decoded token.
|
|
72
|
-
* Handles multiple possible claim names used by IDP.
|
|
73
|
-
*/
|
|
74
|
-
function extractEmailFromToken(decoded) {
|
|
75
|
-
return (decoded.email ||
|
|
76
|
-
decoded['http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name'] ||
|
|
77
|
-
'');
|
|
78
|
-
}
|
|
79
|
-
/**
|
|
80
|
-
* Extract roles from decoded token.
|
|
81
|
-
* Handles both 'role' and 'roles' claims, and both string and array formats.
|
|
82
|
-
*/
|
|
83
|
-
function extractRolesFromToken(decoded) {
|
|
84
|
-
const rolesClaim = decoded.role || decoded.roles;
|
|
85
|
-
if (!rolesClaim) {
|
|
86
|
-
return [];
|
|
87
|
-
}
|
|
88
|
-
if (Array.isArray(rolesClaim)) {
|
|
89
|
-
return rolesClaim;
|
|
90
|
-
}
|
|
91
|
-
if (typeof rolesClaim === 'string') {
|
|
92
|
-
// Could be a single role or JSON array string
|
|
93
|
-
try {
|
|
94
|
-
const parsed = JSON.parse(rolesClaim);
|
|
95
|
-
return Array.isArray(parsed) ? parsed : [rolesClaim];
|
|
96
|
-
}
|
|
97
|
-
catch {
|
|
98
|
-
return [rolesClaim];
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
return [];
|
|
102
|
-
}
|
|
103
|
-
/**
|
|
104
|
-
* Extract AMR (Authentication Methods References) from decoded token.
|
|
105
|
-
*/
|
|
106
|
-
function extractAmrFromToken(decoded) {
|
|
107
|
-
const amr = decoded.amr;
|
|
108
|
-
if (!amr) {
|
|
109
|
-
return [];
|
|
110
|
-
}
|
|
111
|
-
if (Array.isArray(amr)) {
|
|
112
|
-
return amr;
|
|
113
|
-
}
|
|
114
|
-
if (typeof amr === 'string') {
|
|
115
|
-
try {
|
|
116
|
-
const parsed = JSON.parse(amr);
|
|
117
|
-
return Array.isArray(parsed) ? parsed : [amr];
|
|
118
|
-
}
|
|
119
|
-
catch {
|
|
120
|
-
return [amr];
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
return [];
|
|
124
|
-
}
|
|
125
|
-
// ============================================================================
|
|
126
|
-
// EXPIRY CHECKING
|
|
127
|
-
// ============================================================================
|
|
128
|
-
/**
|
|
129
|
-
* Check if a token expiry timestamp indicates the token needs refresh.
|
|
130
|
-
*
|
|
131
|
-
* @param expiresAt - Token expiry timestamp (Unix milliseconds)
|
|
132
|
-
* @param bufferMs - How early to trigger refresh (default 5 minutes)
|
|
133
|
-
* @returns true if token is expired or will expire within buffer period
|
|
134
|
-
*/
|
|
135
|
-
function tokenNeedsRefresh(expiresAt, bufferMs = 5 * 60 * 1000) {
|
|
136
|
-
if (!expiresAt) {
|
|
137
|
-
return true; // No expiry info = assume needs refresh
|
|
138
|
-
}
|
|
139
|
-
const timeUntilExpiry = expiresAt - Date.now();
|
|
140
|
-
return timeUntilExpiry <= bufferMs;
|
|
141
|
-
}
|
|
142
|
-
/**
|
|
143
|
-
* Check if a token is completely expired (past its exp time).
|
|
144
|
-
*
|
|
145
|
-
* @param expiresAt - Token expiry timestamp (Unix milliseconds)
|
|
146
|
-
* @returns true if token is expired
|
|
147
|
-
*/
|
|
148
|
-
function tokenIsExpired(expiresAt) {
|
|
149
|
-
if (!expiresAt) {
|
|
150
|
-
return true;
|
|
151
|
-
}
|
|
152
|
-
return Date.now() >= expiresAt;
|
|
153
|
-
}
|
|
154
|
-
/**
|
|
155
|
-
* Calculate milliseconds until token expires.
|
|
156
|
-
*
|
|
157
|
-
* @param expiresAt - Token expiry timestamp (Unix milliseconds)
|
|
158
|
-
* @returns Milliseconds until expiry, or 0 if already expired
|
|
159
|
-
*/
|
|
160
|
-
function msUntilExpiry(expiresAt) {
|
|
161
|
-
if (!expiresAt) {
|
|
162
|
-
return 0;
|
|
163
|
-
}
|
|
164
|
-
return Math.max(0, expiresAt - Date.now());
|
|
165
|
-
}
|
|
166
|
-
/**
|
|
167
|
-
* Convert Unix seconds (from JWT exp claim) to milliseconds.
|
|
168
|
-
*/
|
|
169
|
-
function expClaimToMs(exp) {
|
|
170
|
-
// JWT exp is in seconds, we use milliseconds internally
|
|
171
|
-
return exp * 1000;
|
|
172
|
-
}
|
|
173
|
-
// ============================================================================
|
|
174
|
-
// TOKEN VALIDATION
|
|
175
|
-
// ============================================================================
|
|
176
|
-
/**
|
|
177
|
-
* Validate that an access token's actual JWT exp matches what we have cached.
|
|
178
|
-
* This catches cases where the token was refreshed but cache wasn't updated.
|
|
179
|
-
*
|
|
180
|
-
* @param accessToken - The JWT access token
|
|
181
|
-
* @param cachedExpiresAt - What we think the expiry is (Unix ms)
|
|
182
|
-
* @returns Object with validation result and actual expiry
|
|
183
|
-
*/
|
|
184
|
-
function validateTokenExpiry(accessToken, cachedExpiresAt) {
|
|
185
|
-
try {
|
|
186
|
-
const parts = accessToken.split('.');
|
|
187
|
-
if (parts.length !== 3) {
|
|
188
|
-
return { valid: false, actualExpiresAt: null, mismatch: false };
|
|
189
|
-
}
|
|
190
|
-
const payload = JSON.parse(Buffer.from(parts[1], 'base64url').toString());
|
|
191
|
-
const actualExpiresAt = payload.exp ? payload.exp * 1000 : null;
|
|
192
|
-
if (!actualExpiresAt) {
|
|
193
|
-
return { valid: false, actualExpiresAt: null, mismatch: false };
|
|
194
|
-
}
|
|
195
|
-
const now = Date.now();
|
|
196
|
-
const isExpired = actualExpiresAt < now;
|
|
197
|
-
// Check for mismatch between cached and actual expiry
|
|
198
|
-
const mismatch = cachedExpiresAt
|
|
199
|
-
? Math.abs(actualExpiresAt - cachedExpiresAt) > 1000 // Allow 1 second tolerance
|
|
200
|
-
: false;
|
|
201
|
-
if (mismatch) {
|
|
202
|
-
console.warn('[TOKEN_UTILS] Token expiry mismatch detected:', {
|
|
203
|
-
cached: cachedExpiresAt ? new Date(cachedExpiresAt).toISOString() : 'none',
|
|
204
|
-
actual: new Date(actualExpiresAt).toISOString(),
|
|
205
|
-
diff: cachedExpiresAt ? actualExpiresAt - cachedExpiresAt : 'N/A',
|
|
206
|
-
});
|
|
207
|
-
}
|
|
208
|
-
return {
|
|
209
|
-
valid: !isExpired,
|
|
210
|
-
actualExpiresAt,
|
|
211
|
-
mismatch,
|
|
212
|
-
};
|
|
213
|
-
}
|
|
214
|
-
catch (error) {
|
|
215
|
-
console.error('[TOKEN_UTILS] Failed to validate token expiry:', error);
|
|
216
|
-
return { valid: false, actualExpiresAt: null, mismatch: false };
|
|
217
|
-
}
|
|
218
|
-
}
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Token Utilities
|
|
4
|
+
*
|
|
5
|
+
* JWT decoding and expiry checking utilities.
|
|
6
|
+
*
|
|
7
|
+
* @version 1.0.0
|
|
8
|
+
* @since auth-refactor-2026-01
|
|
9
|
+
*/
|
|
10
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
+
exports.extractKidFromToken = exports.decodeJwtHeader = void 0;
|
|
12
|
+
exports.decodeIdpAccessToken = decodeIdpAccessToken;
|
|
13
|
+
exports.decodeIdpAccessTokenFull = decodeIdpAccessTokenFull;
|
|
14
|
+
exports.extractEmailFromToken = extractEmailFromToken;
|
|
15
|
+
exports.extractRolesFromToken = extractRolesFromToken;
|
|
16
|
+
exports.extractAmrFromToken = extractAmrFromToken;
|
|
17
|
+
exports.tokenNeedsRefresh = tokenNeedsRefresh;
|
|
18
|
+
exports.tokenIsExpired = tokenIsExpired;
|
|
19
|
+
exports.msUntilExpiry = msUntilExpiry;
|
|
20
|
+
exports.expClaimToMs = expClaimToMs;
|
|
21
|
+
exports.validateTokenExpiry = validateTokenExpiry;
|
|
22
|
+
const jwt_decode_1 = require("../../lib/jwt-decode");
|
|
23
|
+
// Re-export header utilities for consumers
|
|
24
|
+
var jwt_decode_2 = require("../../lib/jwt-decode");
|
|
25
|
+
Object.defineProperty(exports, "decodeJwtHeader", { enumerable: true, get: function () { return jwt_decode_2.decodeJwtHeader; } });
|
|
26
|
+
Object.defineProperty(exports, "extractKidFromToken", { enumerable: true, get: function () { return jwt_decode_2.extractKidFromToken; } });
|
|
27
|
+
// ============================================================================
|
|
28
|
+
// TOKEN DECODING
|
|
29
|
+
// ============================================================================
|
|
30
|
+
/**
|
|
31
|
+
* Decode an IDP access token and extract claims.
|
|
32
|
+
*
|
|
33
|
+
* @param token - The JWT access token from IDP
|
|
34
|
+
* @returns Decoded token claims, or null if decode fails
|
|
35
|
+
*/
|
|
36
|
+
function decodeIdpAccessToken(token) {
|
|
37
|
+
try {
|
|
38
|
+
return (0, jwt_decode_1.jwtDecode)(token);
|
|
39
|
+
}
|
|
40
|
+
catch (error) {
|
|
41
|
+
console.error('[TOKEN_UTILS] Failed to decode access token:', error);
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Decode both JWT header and payload from an IDP access token.
|
|
47
|
+
* Returns the signing key ID (kid) along with payload claims.
|
|
48
|
+
*
|
|
49
|
+
* @param token - The JWT access token from IDP
|
|
50
|
+
* @returns Object with header (including kid) and payload, or null if decode fails
|
|
51
|
+
*/
|
|
52
|
+
function decodeIdpAccessTokenFull(token) {
|
|
53
|
+
try {
|
|
54
|
+
const header = (0, jwt_decode_1.decodeJwtHeader)(token);
|
|
55
|
+
const payload = (0, jwt_decode_1.jwtDecode)(token);
|
|
56
|
+
if (!header || !payload) {
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
return {
|
|
60
|
+
header,
|
|
61
|
+
payload,
|
|
62
|
+
bearerKeyId: header.kid,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
catch (error) {
|
|
66
|
+
console.error('[TOKEN_UTILS] Failed to decode access token (full):', error);
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Extract user email from decoded token.
|
|
72
|
+
* Handles multiple possible claim names used by IDP.
|
|
73
|
+
*/
|
|
74
|
+
function extractEmailFromToken(decoded) {
|
|
75
|
+
return (decoded.email ||
|
|
76
|
+
decoded['http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name'] ||
|
|
77
|
+
'');
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Extract roles from decoded token.
|
|
81
|
+
* Handles both 'role' and 'roles' claims, and both string and array formats.
|
|
82
|
+
*/
|
|
83
|
+
function extractRolesFromToken(decoded) {
|
|
84
|
+
const rolesClaim = decoded.role || decoded.roles;
|
|
85
|
+
if (!rolesClaim) {
|
|
86
|
+
return [];
|
|
87
|
+
}
|
|
88
|
+
if (Array.isArray(rolesClaim)) {
|
|
89
|
+
return rolesClaim;
|
|
90
|
+
}
|
|
91
|
+
if (typeof rolesClaim === 'string') {
|
|
92
|
+
// Could be a single role or JSON array string
|
|
93
|
+
try {
|
|
94
|
+
const parsed = JSON.parse(rolesClaim);
|
|
95
|
+
return Array.isArray(parsed) ? parsed : [rolesClaim];
|
|
96
|
+
}
|
|
97
|
+
catch {
|
|
98
|
+
return [rolesClaim];
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return [];
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Extract AMR (Authentication Methods References) from decoded token.
|
|
105
|
+
*/
|
|
106
|
+
function extractAmrFromToken(decoded) {
|
|
107
|
+
const amr = decoded.amr;
|
|
108
|
+
if (!amr) {
|
|
109
|
+
return [];
|
|
110
|
+
}
|
|
111
|
+
if (Array.isArray(amr)) {
|
|
112
|
+
return amr;
|
|
113
|
+
}
|
|
114
|
+
if (typeof amr === 'string') {
|
|
115
|
+
try {
|
|
116
|
+
const parsed = JSON.parse(amr);
|
|
117
|
+
return Array.isArray(parsed) ? parsed : [amr];
|
|
118
|
+
}
|
|
119
|
+
catch {
|
|
120
|
+
return [amr];
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return [];
|
|
124
|
+
}
|
|
125
|
+
// ============================================================================
|
|
126
|
+
// EXPIRY CHECKING
|
|
127
|
+
// ============================================================================
|
|
128
|
+
/**
|
|
129
|
+
* Check if a token expiry timestamp indicates the token needs refresh.
|
|
130
|
+
*
|
|
131
|
+
* @param expiresAt - Token expiry timestamp (Unix milliseconds)
|
|
132
|
+
* @param bufferMs - How early to trigger refresh (default 5 minutes)
|
|
133
|
+
* @returns true if token is expired or will expire within buffer period
|
|
134
|
+
*/
|
|
135
|
+
function tokenNeedsRefresh(expiresAt, bufferMs = 5 * 60 * 1000) {
|
|
136
|
+
if (!expiresAt) {
|
|
137
|
+
return true; // No expiry info = assume needs refresh
|
|
138
|
+
}
|
|
139
|
+
const timeUntilExpiry = expiresAt - Date.now();
|
|
140
|
+
return timeUntilExpiry <= bufferMs;
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Check if a token is completely expired (past its exp time).
|
|
144
|
+
*
|
|
145
|
+
* @param expiresAt - Token expiry timestamp (Unix milliseconds)
|
|
146
|
+
* @returns true if token is expired
|
|
147
|
+
*/
|
|
148
|
+
function tokenIsExpired(expiresAt) {
|
|
149
|
+
if (!expiresAt) {
|
|
150
|
+
return true;
|
|
151
|
+
}
|
|
152
|
+
return Date.now() >= expiresAt;
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Calculate milliseconds until token expires.
|
|
156
|
+
*
|
|
157
|
+
* @param expiresAt - Token expiry timestamp (Unix milliseconds)
|
|
158
|
+
* @returns Milliseconds until expiry, or 0 if already expired
|
|
159
|
+
*/
|
|
160
|
+
function msUntilExpiry(expiresAt) {
|
|
161
|
+
if (!expiresAt) {
|
|
162
|
+
return 0;
|
|
163
|
+
}
|
|
164
|
+
return Math.max(0, expiresAt - Date.now());
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Convert Unix seconds (from JWT exp claim) to milliseconds.
|
|
168
|
+
*/
|
|
169
|
+
function expClaimToMs(exp) {
|
|
170
|
+
// JWT exp is in seconds, we use milliseconds internally
|
|
171
|
+
return exp * 1000;
|
|
172
|
+
}
|
|
173
|
+
// ============================================================================
|
|
174
|
+
// TOKEN VALIDATION
|
|
175
|
+
// ============================================================================
|
|
176
|
+
/**
|
|
177
|
+
* Validate that an access token's actual JWT exp matches what we have cached.
|
|
178
|
+
* This catches cases where the token was refreshed but cache wasn't updated.
|
|
179
|
+
*
|
|
180
|
+
* @param accessToken - The JWT access token
|
|
181
|
+
* @param cachedExpiresAt - What we think the expiry is (Unix ms)
|
|
182
|
+
* @returns Object with validation result and actual expiry
|
|
183
|
+
*/
|
|
184
|
+
function validateTokenExpiry(accessToken, cachedExpiresAt) {
|
|
185
|
+
try {
|
|
186
|
+
const parts = accessToken.split('.');
|
|
187
|
+
if (parts.length !== 3) {
|
|
188
|
+
return { valid: false, actualExpiresAt: null, mismatch: false };
|
|
189
|
+
}
|
|
190
|
+
const payload = JSON.parse(Buffer.from(parts[1], 'base64url').toString());
|
|
191
|
+
const actualExpiresAt = payload.exp ? payload.exp * 1000 : null;
|
|
192
|
+
if (!actualExpiresAt) {
|
|
193
|
+
return { valid: false, actualExpiresAt: null, mismatch: false };
|
|
194
|
+
}
|
|
195
|
+
const now = Date.now();
|
|
196
|
+
const isExpired = actualExpiresAt < now;
|
|
197
|
+
// Check for mismatch between cached and actual expiry
|
|
198
|
+
const mismatch = cachedExpiresAt
|
|
199
|
+
? Math.abs(actualExpiresAt - cachedExpiresAt) > 1000 // Allow 1 second tolerance
|
|
200
|
+
: false;
|
|
201
|
+
if (mismatch) {
|
|
202
|
+
console.warn('[TOKEN_UTILS] Token expiry mismatch detected:', {
|
|
203
|
+
cached: cachedExpiresAt ? new Date(cachedExpiresAt).toISOString() : 'none',
|
|
204
|
+
actual: new Date(actualExpiresAt).toISOString(),
|
|
205
|
+
diff: cachedExpiresAt ? actualExpiresAt - cachedExpiresAt : 'N/A',
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
return {
|
|
209
|
+
valid: !isExpired,
|
|
210
|
+
actualExpiresAt,
|
|
211
|
+
mismatch,
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
catch (error) {
|
|
215
|
+
console.error('[TOKEN_UTILS] Failed to validate token expiry:', error);
|
|
216
|
+
return { valid: false, actualExpiresAt: null, mismatch: false };
|
|
217
|
+
}
|
|
218
|
+
}
|
|
@@ -1,115 +1,115 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
'use client';
|
|
3
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.AuthProvider = AuthProvider;
|
|
5
|
-
exports.useAuthConfig = useAuthConfig;
|
|
6
|
-
exports.useAuthMode = useAuthMode;
|
|
7
|
-
exports.useFederatedProviders = useFederatedProviders;
|
|
8
|
-
exports.useFederatedAuthEnabled = useFederatedAuthEnabled;
|
|
9
|
-
exports.useTraditionalAuthEnabled = useTraditionalAuthEnabled;
|
|
10
|
-
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
11
|
-
const react_1 = require("react");
|
|
12
|
-
const react_query_1 = require("@tanstack/react-query");
|
|
13
|
-
const AuthContext = (0, react_1.createContext)(null);
|
|
14
|
-
const defaultConfig = {
|
|
15
|
-
mode: 'traditional',
|
|
16
|
-
providers: [],
|
|
17
|
-
enableRecovery: true,
|
|
18
|
-
enableEmailSignup: true,
|
|
19
|
-
allowPasswordReset: true,
|
|
20
|
-
};
|
|
21
|
-
// Map NextAuth provider IDs to our FederatedProvider type
|
|
22
|
-
const PROVIDER_MAP = {
|
|
23
|
-
'google': 'google',
|
|
24
|
-
'apple': 'apple',
|
|
25
|
-
'facebook': 'facebook',
|
|
26
|
-
'github': 'github',
|
|
27
|
-
'azure-ad': 'microsoft',
|
|
28
|
-
'microsoft-entra-id': 'microsoft',
|
|
29
|
-
};
|
|
30
|
-
// OAuth providers we support in UI (excludes credentials)
|
|
31
|
-
const OAUTH_PROVIDER_IDS = ['google', 'apple', 'facebook', 'github', 'azure-ad', 'microsoft-entra-id'];
|
|
32
|
-
function AuthProvider({ children, config, useDynamicProviders = true }) {
|
|
33
|
-
const [dynamicProviders, setDynamicProviders] = (0, react_1.useState)([]);
|
|
34
|
-
const [providersLoaded, setProvidersLoaded] = (0, react_1.useState)(!useDynamicProviders);
|
|
35
|
-
// Create QueryClient instance for React Query - used internally by MVP hooks
|
|
36
|
-
const [queryClient] = (0, react_1.useState)(() => new react_query_1.QueryClient({
|
|
37
|
-
defaultOptions: {
|
|
38
|
-
queries: {
|
|
39
|
-
staleTime: 60 * 1000, // 1 minute
|
|
40
|
-
retry: 1,
|
|
41
|
-
refetchOnWindowFocus: false,
|
|
42
|
-
},
|
|
43
|
-
},
|
|
44
|
-
}));
|
|
45
|
-
// Fetch dynamic providers from NextAuth on mount
|
|
46
|
-
(0, react_1.useEffect)(() => {
|
|
47
|
-
if (!useDynamicProviders)
|
|
48
|
-
return;
|
|
49
|
-
let mounted = true;
|
|
50
|
-
async function fetchDynamicProviders() {
|
|
51
|
-
try {
|
|
52
|
-
// Better Auth doesn't have a getProviders equivalent.
|
|
53
|
-
// Use static provider map based on configured social providers.
|
|
54
|
-
const result = {
|
|
55
|
-
google: { id: 'google', name: 'Google' },
|
|
56
|
-
};
|
|
57
|
-
if (!mounted)
|
|
58
|
-
return;
|
|
59
|
-
if (result) {
|
|
60
|
-
// Filter to OAuth providers only and map to FederatedProvider type
|
|
61
|
-
const oauthProviders = Object.keys(result)
|
|
62
|
-
.filter(id => OAUTH_PROVIDER_IDS.includes(id))
|
|
63
|
-
.map(id => PROVIDER_MAP[id])
|
|
64
|
-
.filter((p) => p !== undefined);
|
|
65
|
-
setDynamicProviders(oauthProviders);
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
catch (err) {
|
|
69
|
-
// Fall back to static config providers on error
|
|
70
|
-
}
|
|
71
|
-
finally {
|
|
72
|
-
if (mounted) {
|
|
73
|
-
setProvidersLoaded(true);
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
fetchDynamicProviders();
|
|
78
|
-
return () => {
|
|
79
|
-
mounted = false;
|
|
80
|
-
};
|
|
81
|
-
}, [useDynamicProviders]);
|
|
82
|
-
// Determine final providers list
|
|
83
|
-
const providers = useDynamicProviders && providersLoaded
|
|
84
|
-
? dynamicProviders
|
|
85
|
-
: (config?.providers ?? defaultConfig.providers);
|
|
86
|
-
const authConfig = {
|
|
87
|
-
...defaultConfig,
|
|
88
|
-
...config,
|
|
89
|
-
providers, // Override with dynamic providers if enabled
|
|
90
|
-
};
|
|
91
|
-
return ((0, jsx_runtime_1.jsx)(react_query_1.QueryClientProvider, { client: queryClient, children: (0, jsx_runtime_1.jsx)(AuthContext.Provider, { value: authConfig, children: children }) }));
|
|
92
|
-
}
|
|
93
|
-
function useAuthConfig() {
|
|
94
|
-
const context = (0, react_1.useContext)(AuthContext);
|
|
95
|
-
if (!context) {
|
|
96
|
-
return defaultConfig;
|
|
97
|
-
}
|
|
98
|
-
return context;
|
|
99
|
-
}
|
|
100
|
-
function useAuthMode() {
|
|
101
|
-
const config = useAuthConfig();
|
|
102
|
-
return config.mode;
|
|
103
|
-
}
|
|
104
|
-
function useFederatedProviders() {
|
|
105
|
-
const config = useAuthConfig();
|
|
106
|
-
return config.providers;
|
|
107
|
-
}
|
|
108
|
-
function useFederatedAuthEnabled() {
|
|
109
|
-
const config = useAuthConfig();
|
|
110
|
-
return config.mode === 'federated' && config.providers.length > 0;
|
|
111
|
-
}
|
|
112
|
-
function useTraditionalAuthEnabled() {
|
|
113
|
-
const config = useAuthConfig();
|
|
114
|
-
return config.mode === 'traditional';
|
|
115
|
-
}
|
|
1
|
+
"use strict";
|
|
2
|
+
'use client';
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.AuthProvider = AuthProvider;
|
|
5
|
+
exports.useAuthConfig = useAuthConfig;
|
|
6
|
+
exports.useAuthMode = useAuthMode;
|
|
7
|
+
exports.useFederatedProviders = useFederatedProviders;
|
|
8
|
+
exports.useFederatedAuthEnabled = useFederatedAuthEnabled;
|
|
9
|
+
exports.useTraditionalAuthEnabled = useTraditionalAuthEnabled;
|
|
10
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
11
|
+
const react_1 = require("react");
|
|
12
|
+
const react_query_1 = require("@tanstack/react-query");
|
|
13
|
+
const AuthContext = (0, react_1.createContext)(null);
|
|
14
|
+
const defaultConfig = {
|
|
15
|
+
mode: 'traditional',
|
|
16
|
+
providers: [],
|
|
17
|
+
enableRecovery: true,
|
|
18
|
+
enableEmailSignup: true,
|
|
19
|
+
allowPasswordReset: true,
|
|
20
|
+
};
|
|
21
|
+
// Map NextAuth provider IDs to our FederatedProvider type
|
|
22
|
+
const PROVIDER_MAP = {
|
|
23
|
+
'google': 'google',
|
|
24
|
+
'apple': 'apple',
|
|
25
|
+
'facebook': 'facebook',
|
|
26
|
+
'github': 'github',
|
|
27
|
+
'azure-ad': 'microsoft',
|
|
28
|
+
'microsoft-entra-id': 'microsoft',
|
|
29
|
+
};
|
|
30
|
+
// OAuth providers we support in UI (excludes credentials)
|
|
31
|
+
const OAUTH_PROVIDER_IDS = ['google', 'apple', 'facebook', 'github', 'azure-ad', 'microsoft-entra-id'];
|
|
32
|
+
function AuthProvider({ children, config, useDynamicProviders = true }) {
|
|
33
|
+
const [dynamicProviders, setDynamicProviders] = (0, react_1.useState)([]);
|
|
34
|
+
const [providersLoaded, setProvidersLoaded] = (0, react_1.useState)(!useDynamicProviders);
|
|
35
|
+
// Create QueryClient instance for React Query - used internally by MVP hooks
|
|
36
|
+
const [queryClient] = (0, react_1.useState)(() => new react_query_1.QueryClient({
|
|
37
|
+
defaultOptions: {
|
|
38
|
+
queries: {
|
|
39
|
+
staleTime: 60 * 1000, // 1 minute
|
|
40
|
+
retry: 1,
|
|
41
|
+
refetchOnWindowFocus: false,
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
}));
|
|
45
|
+
// Fetch dynamic providers from NextAuth on mount
|
|
46
|
+
(0, react_1.useEffect)(() => {
|
|
47
|
+
if (!useDynamicProviders)
|
|
48
|
+
return;
|
|
49
|
+
let mounted = true;
|
|
50
|
+
async function fetchDynamicProviders() {
|
|
51
|
+
try {
|
|
52
|
+
// Better Auth doesn't have a getProviders equivalent.
|
|
53
|
+
// Use static provider map based on configured social providers.
|
|
54
|
+
const result = {
|
|
55
|
+
google: { id: 'google', name: 'Google' },
|
|
56
|
+
};
|
|
57
|
+
if (!mounted)
|
|
58
|
+
return;
|
|
59
|
+
if (result) {
|
|
60
|
+
// Filter to OAuth providers only and map to FederatedProvider type
|
|
61
|
+
const oauthProviders = Object.keys(result)
|
|
62
|
+
.filter(id => OAUTH_PROVIDER_IDS.includes(id))
|
|
63
|
+
.map(id => PROVIDER_MAP[id])
|
|
64
|
+
.filter((p) => p !== undefined);
|
|
65
|
+
setDynamicProviders(oauthProviders);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
catch (err) {
|
|
69
|
+
// Fall back to static config providers on error
|
|
70
|
+
}
|
|
71
|
+
finally {
|
|
72
|
+
if (mounted) {
|
|
73
|
+
setProvidersLoaded(true);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
fetchDynamicProviders();
|
|
78
|
+
return () => {
|
|
79
|
+
mounted = false;
|
|
80
|
+
};
|
|
81
|
+
}, [useDynamicProviders]);
|
|
82
|
+
// Determine final providers list
|
|
83
|
+
const providers = useDynamicProviders && providersLoaded
|
|
84
|
+
? dynamicProviders
|
|
85
|
+
: (config?.providers ?? defaultConfig.providers);
|
|
86
|
+
const authConfig = {
|
|
87
|
+
...defaultConfig,
|
|
88
|
+
...config,
|
|
89
|
+
providers, // Override with dynamic providers if enabled
|
|
90
|
+
};
|
|
91
|
+
return ((0, jsx_runtime_1.jsx)(react_query_1.QueryClientProvider, { client: queryClient, children: (0, jsx_runtime_1.jsx)(AuthContext.Provider, { value: authConfig, children: children }) }));
|
|
92
|
+
}
|
|
93
|
+
function useAuthConfig() {
|
|
94
|
+
const context = (0, react_1.useContext)(AuthContext);
|
|
95
|
+
if (!context) {
|
|
96
|
+
return defaultConfig;
|
|
97
|
+
}
|
|
98
|
+
return context;
|
|
99
|
+
}
|
|
100
|
+
function useAuthMode() {
|
|
101
|
+
const config = useAuthConfig();
|
|
102
|
+
return config.mode;
|
|
103
|
+
}
|
|
104
|
+
function useFederatedProviders() {
|
|
105
|
+
const config = useAuthConfig();
|
|
106
|
+
return config.providers;
|
|
107
|
+
}
|
|
108
|
+
function useFederatedAuthEnabled() {
|
|
109
|
+
const config = useAuthConfig();
|
|
110
|
+
return config.mode === 'federated' && config.providers.length > 0;
|
|
111
|
+
}
|
|
112
|
+
function useTraditionalAuthEnabled() {
|
|
113
|
+
const config = useAuthConfig();
|
|
114
|
+
return config.mode === 'traditional';
|
|
115
|
+
}
|