@payez/next-mvp 4.5.7 → 4.7.10

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 (68) hide show
  1. package/dist/api-handlers/auth/credential-login-email-code.d.ts +24 -0
  2. package/dist/api-handlers/auth/credential-login-email-code.js +83 -0
  3. package/dist/api-handlers/auth/credential-login.d.ts +41 -0
  4. package/dist/api-handlers/auth/credential-login.js +858 -0
  5. package/dist/api-handlers/oauth/resume.d.ts +16 -0
  6. package/dist/api-handlers/oauth/resume.js +207 -0
  7. package/dist/auth/better-auth.d.ts +14 -2
  8. package/dist/auth/better-auth.js +59 -34
  9. package/dist/auth/idp-oauth.d.ts +106 -0
  10. package/dist/auth/idp-oauth.js +280 -0
  11. package/dist/auth/safe-callback.js +10 -2
  12. package/dist/client/AuthContext.d.ts +2 -0
  13. package/dist/client/AuthContext.js +14 -1
  14. package/dist/components/auth/CredentialSignInForm.d.ts +33 -0
  15. package/dist/components/auth/CredentialSignInForm.js +134 -0
  16. package/dist/components/auth/ModeAwareLoginPage.js +4 -1
  17. package/dist/components/auth/ModeAwareSignupPage.js +7 -22
  18. package/dist/lib/authorize-resume.d.ts +87 -0
  19. package/dist/lib/authorize-resume.js +190 -0
  20. package/dist/lib/bearer-fetch.d.ts +17 -0
  21. package/dist/lib/bearer-fetch.js +32 -0
  22. package/dist/lib/code-step-copy.d.ts +53 -0
  23. package/dist/lib/code-step-copy.js +73 -0
  24. package/dist/lib/credential-sign-in-client.d.ts +59 -0
  25. package/dist/lib/credential-sign-in-client.js +89 -0
  26. package/dist/lib/device-id.d.ts +37 -0
  27. package/dist/lib/device-id.js +70 -0
  28. package/dist/lib/lock-window.d.ts +29 -0
  29. package/dist/lib/lock-window.js +53 -0
  30. package/dist/lib/login-error-message.d.ts +18 -0
  31. package/dist/lib/login-error-message.js +47 -0
  32. package/dist/lib/twofa-rejection.d.ts +20 -0
  33. package/dist/lib/twofa-rejection.js +73 -0
  34. package/dist/middleware/create-middleware.d.ts +12 -1
  35. package/dist/middleware/create-middleware.js +19 -5
  36. package/dist/pages/login/page.js +11 -37
  37. package/dist/routes/auth/credential-login-email-code.d.ts +6 -0
  38. package/dist/routes/auth/credential-login-email-code.js +10 -0
  39. package/dist/routes/auth/credential-login.d.ts +6 -0
  40. package/dist/routes/auth/credential-login.js +10 -0
  41. package/dist/routes/oauth/resume.d.ts +11 -0
  42. package/dist/routes/oauth/resume.js +16 -0
  43. package/dist/types/auth.d.ts +6 -0
  44. package/package.json +1102 -1047
  45. package/src/api-handlers/auth/credential-login-email-code.ts +92 -0
  46. package/src/api-handlers/auth/credential-login.ts +948 -0
  47. package/src/api-handlers/oauth/resume.ts +182 -0
  48. package/src/auth/better-auth.ts +81 -5
  49. package/src/auth/idp-oauth.ts +310 -0
  50. package/src/auth/safe-callback.ts +11 -2
  51. package/src/client/AuthContext.tsx +14 -1
  52. package/src/components/auth/CredentialSignInForm.tsx +240 -0
  53. package/src/components/auth/ModeAwareLoginPage.tsx +21 -1
  54. package/src/components/auth/ModeAwareSignupPage.tsx +38 -108
  55. package/src/lib/authorize-resume.ts +187 -0
  56. package/src/lib/bearer-fetch.ts +31 -0
  57. package/src/lib/code-step-copy.ts +83 -0
  58. package/src/lib/credential-sign-in-client.ts +101 -0
  59. package/src/lib/device-id.ts +67 -0
  60. package/src/lib/lock-window.ts +49 -0
  61. package/src/lib/login-error-message.ts +41 -0
  62. package/src/lib/twofa-rejection.ts +91 -0
  63. package/src/middleware/create-middleware.ts +35 -4
  64. package/src/pages/login/page.tsx +20 -34
  65. package/src/routes/auth/credential-login-email-code.ts +6 -0
  66. package/src/routes/auth/credential-login.ts +6 -0
  67. package/src/routes/oauth/resume.ts +11 -0
  68. package/src/types/auth.ts +6 -0
@@ -158,6 +158,18 @@ export interface MvpMiddlewareOptions {
158
158
  * is signed out for not implementing the check).
159
159
  */
160
160
  revocationFailMode?: 'closed' | 'open';
161
+
162
+ /**
163
+ * PAY-2302 cut 1c (BAPert 73585): this app has NO revocation source - for example a development environment
164
+ * provisioned without a vibe client id / HMAC key - and says so EXPLICITLY. Under it a viability answer of
165
+ * `revocationStatus: 'not_configured'` is viable, and a loud warning is logged once at startup.
166
+ *
167
+ * It relaxes ONLY 'not_configured'. 'unknown' (a revocation read that FAILED) and 'revoked' stay closed whatever
168
+ * `revocationFailMode` says, and the default (option absent) is unchanged: 'not_configured' fails closed, because
169
+ * for an app that HAS a vibe client it means a misconfiguration (BAPert 66356). Do not use this to paper over a
170
+ * misconfigured production app; remove it when the environment has a vibe client + HMAC key.
171
+ */
172
+ noRevocationSource?: boolean;
161
173
  }
162
174
 
163
175
  // =============================================================================
@@ -195,8 +207,13 @@ const defaultLogger: MiddlewareLogger = {
195
207
  const LOGIN_PAGE = '/account-auth/login';
196
208
  const VERIFY_CODE_PAGE = '/account-auth/verify-code';
197
209
 
210
+ // PAY-2315 (4.7.1): /login is a public alias of the login page. It used to take the 'public: allow' arm and never reached the
211
+ // 'already_authenticated' redirect, so a signed-in user was shown the sign-in form. It is the login page for every arm now.
212
+ // (safe-callback refuses /login as a destination, so this cannot redirect to itself.)
213
+ const LOGIN_ALIAS = '/login';
214
+
198
215
  function isLoginPage(pathname: string): boolean {
199
- return pathname === LOGIN_PAGE;
216
+ return pathname === LOGIN_PAGE || pathname === LOGIN_ALIAS;
200
217
  }
201
218
 
202
219
  /**
@@ -542,6 +559,15 @@ export function createMvpMiddleware(options: MvpMiddlewareOptions = {}) {
542
559
  const rbacExemptPaths = options.rbacExemptPaths || [];
543
560
  // PAY-1882 step 4: default 'closed' (Jon's option A / BAPert 66316).
544
561
  const revocationFailMode = options.revocationFailMode || 'closed';
562
+ // PAY-2302 cut 1c: strictly `=== true`, so a truthy string in a config file cannot switch revocation off.
563
+ const noRevocationSource = options.noRevocationSource === true;
564
+ if (noRevocationSource) {
565
+ log.warn(
566
+ '[next-mvp] noRevocationSource is ON: sessions of this app CANNOT be revoked server-side (no vibe client / HMAC key). ' +
567
+ "revocationStatus 'not_configured' is treated as viable; 'unknown' and 'revoked' still sign the user out. " +
568
+ 'Remove this option once the environment has a vibe client id and HMAC key.'
569
+ );
570
+ }
545
571
  const sessionResetRoute =
546
572
  options.sessionResetRoute === true ? DEFAULT_SESSION_RESET_ROUTE
547
573
  : typeof options.sessionResetRoute === 'string' && options.sessionResetRoute ? options.sessionResetRoute
@@ -604,7 +630,8 @@ export function createMvpMiddleware(options: MvpMiddlewareOptions = {}) {
604
630
  request,
605
631
  viabilityEndpoint,
606
632
  log,
607
- revocationFailMode
633
+ revocationFailMode,
634
+ noRevocationSource
608
635
  );
609
636
 
610
637
  // --- Stale session (cookie exists but session doesn't): OBSERVE ONLY, DO NOT DECIDE ---
@@ -687,11 +714,14 @@ export function createMvpMiddleware(options: MvpMiddlewareOptions = {}) {
687
714
  export function decideForceInvalid(
688
715
  data: { revocationStatus?: unknown; forceInvalid?: unknown; revoked?: unknown },
689
716
  revocationFailMode: 'closed' | 'open',
717
+ noRevocationSource: boolean = false,
690
718
  ): boolean {
691
719
  const status = data.revocationStatus;
692
720
  if (typeof status === 'string') {
693
721
  if (status === 'revoked') return true;
694
722
  if (status === 'active') return false;
723
+ // PAY-2302 cut 1c: an app that declared it has no revocation source. ONLY the exact 'not_configured' string.
724
+ if (status === 'not_configured' && noRevocationSource === true) return false;
695
725
  // S1 (NightHawk 66409): ANY other string - 'unknown', 'not_configured', a
696
726
  // typo like 'Revoked', 'error', or '' - is treated as 'unknown' (fail mode
697
727
  // applies), NEVER as 'active'. Returning false here would let the first
@@ -720,7 +750,8 @@ async function checkViability(
720
750
  request: NextRequest,
721
751
  endpoint: string,
722
752
  log: MiddlewareLogger,
723
- revocationFailMode: 'closed' | 'open'
753
+ revocationFailMode: 'closed' | 'open',
754
+ noRevocationSource: boolean = false
724
755
  ): Promise<{
725
756
  sessionStatus: SessionStatus;
726
757
  sessionPointer: SessionPointer;
@@ -798,7 +829,7 @@ async function checkViability(
798
829
  // revocations status, decide from it + failMode and ignore the folded
799
830
  // booleans for unknown/not_configured. The booleans are read ONLY when
800
831
  // revocationStatus is ABSENT (idealresume.online's older boolean route).
801
- sessionStatus.forceInvalid = decideForceInvalid(data, revocationFailMode);
832
+ sessionStatus.forceInvalid = decideForceInvalid(data, revocationFailMode, noRevocationSource);
802
833
  // PAY-2216: a STALE session - the cookie resolved to a token, but no store holds the session - used to set
803
834
  // pointer.exists=false, so makeAuthDecision took no_session (login, cookie NOT cleared) and its stale_session
804
835
  // arm (clearCookies) was unreachable: a dead cookie lingered and re-ran viability on every request.
@@ -29,6 +29,7 @@ import { Suspense } from 'react';
29
29
  import ReservedStatusBox from '../../components/reserved/ReservedStatusBox';
30
30
  import ReservedRecoveryWarning from '../../components/reserved/ReservedRecoveryWarning';
31
31
  import { useBranding, useColors, useLayout } from '../../theme/useTheme';
32
+ import { friendlyLoginError, oauthFailureMessage } from '../../lib/login-error-message';
32
33
 
33
34
  function LoginForm({ altAuthSlot }: { altAuthSlot?: React.ReactNode }) {
34
35
  const searchParams = useSearchParams();
@@ -117,46 +118,20 @@ function LoginForm({ altAuthSlot }: { altAuthSlot?: React.ReactNode }) {
117
118
  // Atomic state management - only ONE can be true at a time
118
119
  const [isSubmitting, setIsSubmitting] = useState(false);
119
120
  const [loginSuccess, setLoginSuccess] = useState(false);
120
- const [loginError, setLoginError] = useState<string | null>(null);
121
+ // undefined = the user has not acted yet (show what the URL says); null/string = an explicit state set by a handler below.
122
+ const [loginError, setLoginError] = useState<string | null | undefined>(undefined);
121
123
  const [loading, setLoading] = useState(false);
122
124
  const [showRecoveryOptions, setShowRecoveryOptions] = useState(false);
123
125
 
124
126
  // Force show form after timeout if stuck in loading
125
127
  const [forceShowForm, setForceShowForm] = useState(false);
126
128
 
127
- // Surface OAuth failures the Better Auth mount bounced back here.
129
+ // PAY-2319 (4.7.2): what the URL says, derived at render (no effect) so it is shown on the FIRST paint and in a static render.
130
+ // 4.7.1 mapped these in effects that only set loginError, which was never rendered. Unknown codes are NOT echoed (see lib/login-error-message).
131
+ const urlMessage = oauthFailureMessage(oauthError) ?? friendlyLoginError(urlError);
132
+ const shownError: string | null = loginError !== undefined ? loginError : urlMessage;
128
133
  useEffect(() => {
129
- if (oauthError) {
130
- const name = oauthError.charAt(0).toUpperCase() + oauthError.slice(1);
131
- setLoginError(`${name} sign-in did not complete. Please try again.`);
132
- }
133
- }, [oauthError]);
134
-
135
- // Handle URL errors with user-friendly messages
136
- useEffect(() => {
137
- if (urlError) {
138
- let friendlyError: string | null = urlError;
139
-
140
- if (urlError === 'SessionExpired') {
141
- // Session expiration is normal - don't show as error
142
- console.log('[LOGIN] SessionExpired detected - normal behavior');
143
- friendlyError = null;
144
- } else if (urlError === 'TwoFactorTimeout') {
145
- // PAY-884: the sign-in expired while the verification code was outstanding. Unlike
146
- // SessionExpired this is NOT silent - the user was mid-2FA and must know why they are here.
147
- friendlyError = 'Your verification step timed out before the code was confirmed. Please sign in again.';
148
- } else if (urlError === 'ServiceUnavailable') {
149
- friendlyError = 'Authentication service is temporarily unavailable. Please try again in a few minutes.';
150
- } else if (urlError.startsWith("Unexpected token")) {
151
- friendlyError = 'An authentication error occurred. Please try signing in again.';
152
- } else if (urlError.includes('not valid JSON')) {
153
- friendlyError = 'An authentication error occurred. Please try signing in again.';
154
- } else if (urlError === 'CredentialsSignin') {
155
- friendlyError = 'Invalid email or password. Please try again.';
156
- }
157
-
158
- setLoginError(friendlyError);
159
- }
134
+ if (urlError === 'SessionExpired') console.log('[LOGIN] SessionExpired detected - normal behavior');
160
135
  }, [urlError]);
161
136
 
162
137
  // Timeout to prevent infinite loading states
@@ -186,7 +161,7 @@ function LoginForm({ altAuthSlot }: { altAuthSlot?: React.ReactNode }) {
186
161
  // package never configures emailAndPassword. See the note at the render site.
187
162
 
188
163
  // Show loading spinner for initial loads
189
- if (!forceShowForm && !loginError && (loading || (status === 'loading' && !session))) {
164
+ if (!forceShowForm && !shownError && (loading || (status === 'loading' && !session))) {
190
165
  return (
191
166
  <div className={`flex flex-col items-center justify-center min-h-screen ${colors.background}`}>
192
167
  <svg className="animate-spin h-6 w-6" style={{ color: buttonColor }} viewBox="0 0 24 24" fill="none">
@@ -246,6 +221,17 @@ function LoginForm({ altAuthSlot }: { altAuthSlot?: React.ReactNode }) {
246
221
  </p>
247
222
  </div>
248
223
 
224
+ {/* PAY-2319 (4.7.2): the error sentence. 4.7.1 set loginError but never rendered it. */}
225
+ {shownError && (
226
+ <div
227
+ role="alert"
228
+ data-testid="login-error"
229
+ className="mb-6 rounded-lg border border-red-200 bg-red-50 px-4 py-3 text-sm text-red-700"
230
+ >
231
+ {shownError}
232
+ </div>
233
+ )}
234
+
249
235
  {/* Social sign-in — rendered ONLY from the IDP client config's
250
236
  enabled providers (see the /api/auth/settings discovery above).
251
237
  Above the form, divider idiom per the package's ModeAwareLoginPage. */}
@@ -0,0 +1,6 @@
1
+ /**
2
+ * PAY-2302 cut 1: ask the IdP to email a code when the authenticator lane failed on our side.
3
+ * The handler lives in api-handlers/auth/credential-login-email-code.ts (moved from VibeID-Customer-Portal src/app/api/auth/admin-login/email-code/route.ts). A consuming app's route file
4
+ * re-exports POST and writes `export const runtime = 'nodejs'` literally.
5
+ */
6
+ export { POST } from '../../api-handlers/auth/credential-login-email-code';
@@ -0,0 +1,6 @@
1
+ /**
2
+ * PAY-2302 cut 1: password sign-in route (IdP ExternalAuth/login, session written via lib/session-store, 2FA-outstanding handling).
3
+ * The handler lives in api-handlers/auth/credential-login.ts (moved from VibeID-Customer-Portal src/app/api/auth/admin-login/route.ts). A consuming app's route file
4
+ * re-exports POST and writes `export const runtime = 'nodejs'` literally.
5
+ */
6
+ export { POST } from '../../api-handlers/auth/credential-login';
@@ -0,0 +1,11 @@
1
+ /**
2
+ * PAY-2302 cut 2: post-2FA OAuth authorize resume route. The handler lives in api-handlers/oauth/resume.ts
3
+ * (moved from VibeID-Customer-Portal src/app/api/oauth/resume/route.ts). A consuming app's route file re-exports GET
4
+ * and writes `export const runtime = 'nodejs'` literally.
5
+ *
6
+ * 4.7.3: `createOAuthResumeHandler({ refusalLocation })` and `DEFAULT_REFUSAL_LOCATION` are re-exported HERE because a consumer can only
7
+ * import what `package.json` `exports` lists, and this route module is the entry. An app whose sign-in page is not
8
+ * /account-auth/login (the portal uses /sign-in) writes `export const GET = createOAuthResumeHandler({ refusalLocation: '/sign-in?error=AUTHORIZE_RESUME_REFUSED' });`.
9
+ */
10
+ export { GET, createOAuthResumeHandler, DEFAULT_REFUSAL_LOCATION } from '../../api-handlers/oauth/resume';
11
+ export type { OAuthResumeOptions, OAuthResumeDeps } from '../../api-handlers/oauth/resume';
package/src/types/auth.ts CHANGED
@@ -25,6 +25,12 @@ export interface AuthConfig {
25
25
  * An OAuth-only client is a deliberate choice, never the default.
26
26
  */
27
27
  allowTraditionalLogin?: boolean;
28
+ /**
29
+ * PAY-2334: where /api/auth/settings stands. 'loading' = not answered yet, 'ready' = answered (or nothing to ask:
30
+ * useDynamicProviders off), 'failed' = non-2xx, thrown, or timed out. Absent means 'ready'. A page must not tell the user
31
+ * "sign-in is not available" while this is 'loading', and must say a failed read is a failed read, not an empty answer.
32
+ */
33
+ settingsStatus?: 'loading' | 'ready' | 'failed';
28
34
  providers: FederatedProvider[];
29
35
  enableRecovery: boolean;
30
36
  enableEmailSignup: boolean;