@payez/next-mvp 4.7.9 → 4.8.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.
@@ -16,6 +16,7 @@
16
16
  */
17
17
  Object.defineProperty(exports, "__esModule", { value: true });
18
18
  exports.GET = GET;
19
+ const revocation_fields_1 = require("../../auth/revocation-fields");
19
20
  const server_1 = require("next/server");
20
21
  const auth_1 = require("../../server/auth");
21
22
  const session_store_1 = require("../../lib/session-store");
@@ -150,6 +151,7 @@ async function GET(req) {
150
151
  needsRefresh: false,
151
152
  authenticated: false,
152
153
  revocationStatus: 'unknown',
154
+ unavailable: true, // PAY-2339: a reader of this JSON gets the hold signal (the response is non-OK, so the middleware holds as service_error)
153
155
  error: 'Session store unavailable',
154
156
  code: 'STORE_UNAVAILABLE'
155
157
  }, { status: 500 });
@@ -361,8 +363,7 @@ async function GET(req) {
361
363
  // Session revocation (PAY-1882 step 4): the four-state status, plus the
362
364
  // boolean the middleware's existing force-invalid branch already reads.
363
365
  revocationStatus,
364
- forceInvalid: revocationStatus === 'revoked' || revocationStatus === 'unknown',
365
- revoked: revocationStatus === 'revoked',
366
+ ...(0, revocation_fields_1.revocationFields)(revocationStatus), // PAY-2339: forceInvalid only for 'revoked'; unavailable:true for 'unknown'
366
367
  // Clear names for middleware decision-making
367
368
  tenantRequiresTwoFactor,
368
369
  userHasCompletedTenantTwoFactorRequirements,
@@ -436,8 +437,7 @@ async function GET(req) {
436
437
  reason: 'Token expired',
437
438
  // Session revocation (PAY-1882 step 4) - same value on every return.
438
439
  revocationStatus,
439
- forceInvalid: revocationStatus === 'revoked' || revocationStatus === 'unknown',
440
- revoked: revocationStatus === 'revoked',
440
+ ...(0, revocation_fields_1.revocationFields)(revocationStatus), // PAY-2339: forceInvalid only for 'revoked'; unavailable:true for 'unknown'
441
441
  // RBAC fields
442
442
  roles: session?.roles || [],
443
443
  clientId: resolveClientId(session?.idpClientId),
@@ -452,8 +452,7 @@ async function GET(req) {
452
452
  sessionTokenId: sessionToken ? (0, token_log_id_1.tokenLogId)(sessionToken) : null,
453
453
  // Session revocation (PAY-1882 step 4) - same value on every return.
454
454
  revocationStatus,
455
- forceInvalid: revocationStatus === 'revoked' || revocationStatus === 'unknown',
456
- revoked: revocationStatus === 'revoked',
455
+ ...(0, revocation_fields_1.revocationFields)(revocationStatus), // PAY-2339: forceInvalid only for 'revoked'; unavailable:true for 'unknown'
457
456
  // Clear names
458
457
  tenantRequiresTwoFactor,
459
458
  userHasCompletedTenantTwoFactorRequirements,
@@ -21,6 +21,12 @@ export interface AuthConfig {
21
21
  * An OAuth-only client is a deliberate choice, never the default.
22
22
  */
23
23
  allowTraditionalLogin?: boolean;
24
+ /**
25
+ * PAY-2334: where /api/auth/settings stands. 'loading' = not answered yet, 'ready' = answered (or nothing to ask:
26
+ * useDynamicProviders off), 'failed' = non-2xx, thrown, or timed out. Absent means 'ready'. A page must not tell the user
27
+ * "sign-in is not available" while this is 'loading', and must say a failed read is a failed read, not an empty answer.
28
+ */
29
+ settingsStatus?: 'loading' | 'ready' | 'failed';
24
30
  providers: FederatedProvider[];
25
31
  enableRecovery: boolean;
26
32
  enableEmailSignup: boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@payez/next-mvp",
3
- "version": "4.7.9",
3
+ "version": "4.8.0",
4
4
  "sideEffects": false,
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -910,6 +910,11 @@
910
910
  "require": "./dist/pages/client-admin/index.js",
911
911
  "default": "./dist/pages/client-admin/index.js"
912
912
  },
913
+ "./pages/service-unavailable": {
914
+ "types": "./dist/pages/service-unavailable/page.d.ts",
915
+ "require": "./dist/pages/service-unavailable/page.js",
916
+ "default": "./dist/pages/service-unavailable/page.js"
917
+ },
913
918
  "./pages/coming-soon": {
914
919
  "types": "./dist/pages/coming-soon/page.d.ts",
915
920
  "require": "./dist/pages/coming-soon/page.js",
@@ -4,6 +4,7 @@
4
4
  * This API route is called by the middleware to securely check if a session is valid.
5
5
  */
6
6
 
7
+ import { revocationFields } from '../../auth/revocation-fields';
7
8
  import { NextRequest, NextResponse } from 'next/server';
8
9
  import { getSession as getRedisSession, getBetterAuthSession } from '../../lib/session-store';
9
10
  import { resolveSessionState } from '../../server/auth';
@@ -52,7 +53,7 @@ export async function GET(req: NextRequest) {
52
53
  if (sessionState.state === 'unknown') {
53
54
  console.warn('[VIABILITY] Session store unavailable - session state unknown');
54
55
  return NextResponse.json(
55
- { authenticated: false, revocationStatus: 'unknown' as RevocationStatus, error: 'Session store unavailable', code: 'STORE_UNAVAILABLE' },
56
+ { authenticated: false, revocationStatus: 'unknown' as RevocationStatus, unavailable: true, error: 'Session store unavailable', code: 'STORE_UNAVAILABLE' },
56
57
  { status: 500 }
57
58
  );
58
59
  }
@@ -192,8 +193,7 @@ export async function GET(req: NextRequest) {
192
193
  // Session revocation (PAY-1882 step 4): the four-state status, plus the
193
194
  // boolean the middleware's existing force-invalid branch already reads.
194
195
  revocationStatus,
195
- forceInvalid: revocationStatus === 'revoked' || revocationStatus === 'unknown',
196
- revoked: revocationStatus === 'revoked',
196
+ ...revocationFields(revocationStatus), // PAY-2339: forceInvalid only for 'revoked'; unavailable:true for 'unknown'
197
197
  // Cards 141688/141741 — the IDP's routing decision, relayed verbatim.
198
198
  //
199
199
  // THIS IS THE SECOND IMPLEMENTATION OF VIABILITY. The other is
@@ -22,6 +22,8 @@ export interface AuthContext {
22
22
  sessionStatus: {
23
23
  exists: boolean | null; // null = check failed/timeout
24
24
  forceInvalid: boolean | null; // null = check failed/timeout
25
+ /** PAY-2339: the producer said the revocation backend could not answer. makeAuthDecision HOLDS (no sign-out). */
26
+ unavailable?: boolean;
25
27
  requires2FA: boolean;
26
28
  twoFactorComplete: boolean;
27
29
  };
@@ -34,7 +36,7 @@ export interface AuthContext {
34
36
  export type AuthAction =
35
37
  | { type: 'allow' }
36
38
  | { type: 'redirect'; location: string; reason: string; clearCookies?: boolean }
37
- | { type: 'service_error'; reason: string };
39
+ | { type: 'service_error'; reason: string; check?: 'revocation' | 'viability' };
38
40
 
39
41
  /** PAY-2180: warn once per process, not per request. */
40
42
  let deprecationWarned = false;
@@ -139,6 +141,16 @@ function decide(context: AuthContext): AuthAction {
139
141
  };
140
142
  }
141
143
 
144
+ // PAY-2339 (BAPert 74633): the revocation backend could not answer. Hold: not a verdict on the session, so no
145
+ // sign-out and no cookie is touched. A caller maps this to its own "temporarily unavailable" answer (the
146
+ // createMvpMiddleware twin sends /service-unavailable or a 503 with unavailable:true, check:'revocation').
147
+ if (sessionStatus.unavailable) {
148
+ if (isLoginPage) {
149
+ return { type: 'allow' };
150
+ }
151
+ return { type: 'service_error', reason: 'revocation_unknown', check: 'revocation' };
152
+ }
153
+
142
154
  // Session force invalidated - need fresh login
143
155
  if (sessionStatus.forceInvalid) {
144
156
  if (isLoginPage) {
@@ -0,0 +1,16 @@
1
+ /**
2
+ * PAY-2339 (BAPert 74633 gap 1): the two revocation fields every viability producer emits, in ONE place so the
3
+ * producers (api-handlers/session/viability.ts and routes/auth/viability.ts, four return sites) cannot drift.
4
+ *
5
+ * forceInvalid - true ONLY for 'revoked'. An 'unknown' answer is no longer a sign-out verdict.
6
+ * unavailable - true when the revocation backend could not answer: 'unknown' and ANY string that is not one of the
7
+ * three recognised states. A consumer that reads this JSON (useSessionCheck, makeAuthDecision) holds
8
+ * instead of signing the member out. `revocationStatus` itself is emitted unchanged.
9
+ *
10
+ * 'not_configured' is a configuration fault, not an outage: neither field is set for it (unchanged).
11
+ */
12
+ export function revocationFields(status: unknown): { forceInvalid: boolean; unavailable: boolean; revoked: boolean } {
13
+ const revoked = status === 'revoked';
14
+ const unavailable = !(status === 'revoked' || status === 'active' || status === 'not_configured');
15
+ return { forceInvalid: revoked, unavailable, revoked };
16
+ }
@@ -38,9 +38,15 @@ interface AuthProviderProps {
38
38
  useDynamicProviders?: boolean;
39
39
  }
40
40
 
41
+ /** PAY-2334: how long /api/auth/settings may take before the sign-in card stops waiting and says it could not load. */
42
+ export const SETTINGS_FETCH_TIMEOUT_MS = 10000;
43
+
41
44
  export function AuthProvider({ children, config, useDynamicProviders = true }: AuthProviderProps) {
42
45
  const [dynamicProviders, setDynamicProviders] = useState<FederatedProvider[]>([]);
43
46
  const [providersLoaded, setProvidersLoaded] = useState(!useDynamicProviders);
47
+ // PAY-2334: providersLoaded is true on failure too (it only means "stop waiting"), so it cannot tell an empty answer from
48
+ // no answer. This can: loading -> ready (answered) | failed (non-2xx, thrown, timed out).
49
+ const [settingsStatus, setSettingsStatus] = useState<'loading' | 'ready' | 'failed'>(useDynamicProviders ? 'loading' : 'ready');
44
50
 
45
51
  // What the IDP says this client offers. null until /api/auth/settings answers.
46
52
  const [serverSettings, setServerSettings] = useState<{
@@ -66,6 +72,9 @@ export function AuthProvider({ children, config, useDynamicProviders = true }: A
66
72
  let mounted = true;
67
73
 
68
74
  async function fetchDynamicProviders() {
75
+ const controller = new AbortController();
76
+ const timer = setTimeout(() => controller.abort(), SETTINGS_FETCH_TIMEOUT_MS);
77
+ let status: 'ready' | 'failed' = 'failed';
69
78
  try {
70
79
  // READ THE IDP. This used to be a hardcoded literal:
71
80
  // const result = { google: { id: 'google', name: 'Google' } };
@@ -75,7 +84,7 @@ export function AuthProvider({ children, config, useDynamicProviders = true }: A
75
84
  //
76
85
  // /api/auth/settings already serves the real answer (enabledProviders,
77
86
  // allowFederatedLogin, enablePasswordReset) and nothing consumed it.
78
- const response = await fetch('/api/auth/settings');
87
+ const response = await fetch('/api/auth/settings', { signal: controller.signal });
79
88
  if (!response.ok) {
80
89
  // No silent substitution. Absent settings means we do not know what this
81
90
  // client offers, and inventing a provider list is how a tenant ends up with a
@@ -98,6 +107,7 @@ export function AuthProvider({ children, config, useDynamicProviders = true }: A
98
107
  .map(id => PROVIDER_MAP[id])
99
108
  .filter((p): p is FederatedProvider => p !== undefined);
100
109
 
110
+ status = 'ready';
101
111
  setDynamicProviders(oauthProviders);
102
112
  setServerSettings({
103
113
  allowFederatedLogin: settings?.allowFederatedLogin !== false,
@@ -109,7 +119,9 @@ export function AuthProvider({ children, config, useDynamicProviders = true }: A
109
119
  } catch (err) {
110
120
  console.error('[AuthProvider] could not read /api/auth/settings:', err);
111
121
  } finally {
122
+ clearTimeout(timer);
112
123
  if (mounted) {
124
+ setSettingsStatus(status);
113
125
  setProvidersLoaded(true);
114
126
  }
115
127
  }
@@ -130,6 +142,7 @@ export function AuthProvider({ children, config, useDynamicProviders = true }: A
130
142
  ...defaultConfig,
131
143
  ...config,
132
144
  providers, // from the IDP when useDynamicProviders is on
145
+ settingsStatus: useDynamicProviders ? settingsStatus : 'ready',
133
146
  // THE SERVER DECIDES, THE SITE MAY OVERRIDE. A site that passes an explicit value
134
147
  // still wins; absent that, what the tenant's IDP reports is what renders. A client
135
148
  // hardcoding its own auth mode is how a sign-in screen and its IDP disagree.
@@ -105,7 +105,27 @@ export function ModeAwareLoginPage({
105
105
  and rendered BOTH sections on the same condition - so a client configured with
106
106
  no sign-in method at all got the full form. Say so instead: a blank card leaves
107
107
  the user pressing nothing, and the operator with no idea what is wrong. */}
108
- {!isFederated && !isTraditional && (
108
+ {!isFederated && !isTraditional && config.settingsStatus === 'loading' && (
109
+ <div role="status" className="px-4 py-3 rounded-lg text-sm text-left" style={{ color: 'var(--text-secondary, #6b7280)' }}>
110
+ <p>Loading sign-in options…</p>
111
+ </div>
112
+ )}
113
+
114
+ {!isFederated && !isTraditional && config.settingsStatus === 'failed' && (
115
+ <div
116
+ role="alert"
117
+ className="px-4 py-3 rounded-lg text-sm text-left"
118
+ style={{
119
+ border: '1px solid var(--border-default, #e5e7eb)',
120
+ color: 'var(--text-primary, #111827)',
121
+ }}
122
+ >
123
+ <p className="font-semibold">Could not load sign-in options.</p>
124
+ <p className="mt-1">Please reload the page. If it keeps happening, contact an administrator.</p>
125
+ </div>
126
+ )}
127
+
128
+ {!isFederated && !isTraditional && (config.settingsStatus ?? 'ready') === 'ready' && (
109
129
  <div
110
130
  role="alert"
111
131
  className="px-4 py-3 rounded-lg text-sm text-left"
@@ -154,116 +154,46 @@ export function ModeAwareSignupPage({
154
154
  </form>
155
155
  )}
156
156
 
157
- {/* Hybrid Mode - Show both */}
158
- {isFederated === false && isTraditional === false && (
159
- <>
160
- <FederatedAuthSection
161
- providers={config.providers}
162
- onProviderClick={handleFederatedClick}
163
- isLoading={totalLoading}
164
- />
165
-
166
- {config.providers.length > 0 && config.enableEmailSignup && (
167
- <div className="relative my-6">
168
- <div className="absolute inset-0 flex items-center">
169
- <div
170
- className="w-full"
171
- style={{
172
- borderTop: '1px solid var(--border-default)',
173
- }}
174
- ></div>
175
- </div>
176
- <div className="relative flex justify-center text-sm">
177
- <span
178
- className="px-2"
179
- style={{
180
- background: 'var(--bg-card)',
181
- color: 'var(--text-secondary)',
182
- }}
183
- >
184
- Or sign up with email
185
- </span>
186
- </div>
187
- </div>
188
- )}
189
-
190
- {config.enableEmailSignup && (
191
- <form onSubmit={handleTraditionalSubmit} className="space-y-4">
192
- <div>
193
- <label className="block text-sm font-medium mb-2" style={{ color: 'var(--text-primary)' }}>
194
- Email Address
195
- </label>
196
- <input
197
- type="email"
198
- value={email}
199
- onChange={(e) => setEmail(e.target.value)}
200
- placeholder="you@example.com"
201
- disabled={totalLoading}
202
- required
203
- className="w-full px-4 py-3 rounded-lg border transition-colors focus:outline-none focus:ring-2 disabled:opacity-50"
204
- style={{
205
- border: '1px solid var(--border-default)',
206
- color: 'var(--text-primary)',
207
- background: 'var(--bg-default)',
208
- }}
209
- />
210
- </div>
211
-
212
- <div>
213
- <label className="block text-sm font-medium mb-2" style={{ color: 'var(--text-primary)' }}>
214
- Password
215
- </label>
216
- <input
217
- type="password"
218
- value={password}
219
- onChange={(e) => setPassword(e.target.value)}
220
- placeholder="••••••••"
221
- disabled={totalLoading}
222
- required
223
- className="w-full px-4 py-3 rounded-lg border transition-colors focus:outline-none focus:ring-2 disabled:opacity-50"
224
- style={{
225
- border: '1px solid var(--border-default)',
226
- color: 'var(--text-primary)',
227
- background: 'var(--bg-default)',
228
- }}
229
- />
230
- </div>
157
+ {/* PAY-2334: same three states as the login page. Not answered yet -> neutral text; read failed -> say so;
158
+ only an ANSWERED "nothing offered" falls through to the branch below. */}
159
+ {!isFederated && !isTraditional && config.settingsStatus === 'loading' && (
160
+ <div role="status" className="px-4 py-3 rounded-lg text-sm text-left" style={{ color: 'var(--text-secondary, #6b7280)' }}>
161
+ <p>Loading sign-up options…</p>
162
+ </div>
163
+ )}
231
164
 
232
- <div>
233
- <label className="block text-sm font-medium mb-2" style={{ color: 'var(--text-primary)' }}>
234
- Confirm Password
235
- </label>
236
- <input
237
- type="password"
238
- value={confirmPassword}
239
- onChange={(e) => setConfirmPassword(e.target.value)}
240
- placeholder="••••••••"
241
- disabled={totalLoading}
242
- required
243
- className="w-full px-4 py-3 rounded-lg border transition-colors focus:outline-none focus:ring-2 disabled:opacity-50"
244
- style={{
245
- border: '1px solid var(--border-default)',
246
- color: 'var(--text-primary)',
247
- background: 'var(--bg-default)',
248
- }}
249
- />
250
- </div>
165
+ {!isFederated && !isTraditional && config.settingsStatus === 'failed' && (
166
+ <div
167
+ role="alert"
168
+ className="px-4 py-3 rounded-lg text-sm text-left"
169
+ style={{
170
+ border: '1px solid var(--border-default, #e5e7eb)',
171
+ color: 'var(--text-primary, #111827)',
172
+ }}
173
+ >
174
+ <p className="font-semibold">Could not load sign-up options.</p>
175
+ <p className="mt-1">Please reload the page. If it keeps happening, contact an administrator.</p>
176
+ </div>
177
+ )}
251
178
 
252
- <button
253
- type="submit"
254
- disabled={totalLoading}
255
- className="w-full py-3 px-4 rounded-lg font-semibold focus:outline-none focus:ring-2 disabled:opacity-50 disabled:cursor-not-allowed transition-colors mt-6"
256
- style={{
257
- border: '1px solid var(--border-default)',
258
- color: 'white',
259
- background: 'var(--color-primary, #10b981)',
260
- }}
261
- >
262
- {totalLoading ? 'Creating Account...' : 'Create Account'}
263
- </button>
264
- </form>
265
- )}
266
- </>
179
+ {/* PAY-2341: ANSWERED, and nothing is offered. This branch used to be labelled "Hybrid Mode - Show both" and
180
+ rendered the federated section plus the email sign-up form on the same "neither" condition, so a client with no
181
+ sign-up method showed a working-looking form. Say so, as the login page does (ModeAwareLoginPage). */}
182
+ {!isFederated && !isTraditional && (config.settingsStatus ?? 'ready') === 'ready' && (
183
+ <div
184
+ role="alert"
185
+ className="px-4 py-3 rounded-lg text-sm text-left"
186
+ style={{
187
+ border: '1px solid var(--border-default, #e5e7eb)',
188
+ color: 'var(--text-primary, #111827)',
189
+ }}
190
+ >
191
+ <p className="font-semibold">Sign-up is not available for this application.</p>
192
+ <p className="mt-1">
193
+ No sign-up method is enabled for this client. An administrator needs to enable
194
+ a federated provider or username and password sign-in.
195
+ </p>
196
+ </div>
267
197
  )}
268
198
  </>
269
199
  );