@payez/next-mvp 3.9.1 → 4.0.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.
Files changed (147) hide show
  1. package/dist/api/auth-handler.d.ts +1 -2
  2. package/dist/api/auth-handler.js +9 -9
  3. package/dist/api-handlers/account/change-password.js +110 -112
  4. package/dist/api-handlers/admin/analytics.d.ts +19 -20
  5. package/dist/api-handlers/admin/analytics.js +378 -379
  6. package/dist/api-handlers/admin/audit.d.ts +19 -20
  7. package/dist/api-handlers/admin/audit.js +213 -214
  8. package/dist/api-handlers/admin/index.d.ts +21 -22
  9. package/dist/api-handlers/admin/index.js +42 -43
  10. package/dist/api-handlers/admin/redis-sessions.d.ts +35 -36
  11. package/dist/api-handlers/admin/redis-sessions.js +203 -204
  12. package/dist/api-handlers/admin/sessions.d.ts +20 -21
  13. package/dist/api-handlers/admin/sessions.js +283 -284
  14. package/dist/api-handlers/admin/site-logs.d.ts +45 -46
  15. package/dist/api-handlers/admin/site-logs.js +317 -318
  16. package/dist/api-handlers/admin/stats.d.ts +20 -21
  17. package/dist/api-handlers/admin/stats.js +239 -240
  18. package/dist/api-handlers/admin/users.d.ts +19 -20
  19. package/dist/api-handlers/admin/users.js +221 -222
  20. package/dist/api-handlers/admin/vibe-data.d.ts +79 -80
  21. package/dist/api-handlers/admin/vibe-data.js +267 -268
  22. package/dist/api-handlers/auth/refresh.js +633 -635
  23. package/dist/api-handlers/auth/signout.js +186 -187
  24. package/dist/api-handlers/auth/status.js +4 -7
  25. package/dist/api-handlers/auth/update-session.d.ts +1 -1
  26. package/dist/api-handlers/auth/update-session.js +12 -14
  27. package/dist/api-handlers/auth/verify-code.d.ts +43 -43
  28. package/dist/api-handlers/auth/verify-code.js +90 -94
  29. package/dist/api-handlers/session/viability.js +114 -146
  30. package/dist/api-handlers/test/force-expire.js +59 -65
  31. package/dist/auth/auth-decision.js +182 -182
  32. package/dist/auth/better-auth.d.ts +3 -6
  33. package/dist/auth/better-auth.js +3 -6
  34. package/dist/auth/route-config.js +2 -2
  35. package/dist/auth/utils/token-utils.d.ts +83 -84
  36. package/dist/auth/utils/token-utils.js +218 -219
  37. package/dist/client/AuthContext.js +115 -112
  38. package/dist/client/better-auth-client.d.ts +1020 -1020
  39. package/dist/client/fetch-with-auth.js +2 -2
  40. package/dist/components/SessionSync.js +121 -119
  41. package/dist/components/account/MobileNavDrawer.js +64 -64
  42. package/dist/components/account/UserAvatarMenu.js +91 -88
  43. package/dist/components/admin/VibeAdminLayout.js +71 -69
  44. package/dist/hooks/useAuth.js +9 -7
  45. package/dist/hooks/useAuthSettings.js +93 -93
  46. package/dist/hooks/useAvailableProviders.d.ts +43 -45
  47. package/dist/hooks/useAvailableProviders.js +112 -108
  48. package/dist/hooks/useSessionExpiration.d.ts +2 -3
  49. package/dist/hooks/useSessionExpiration.js +2 -2
  50. package/dist/hooks/useViabilitySession.js +3 -2
  51. package/dist/index.js +4 -6
  52. package/dist/lib/app-slug.d.ts +95 -95
  53. package/dist/lib/app-slug.js +172 -172
  54. package/dist/lib/standardized-client-api.js +10 -5
  55. package/dist/lib/startup-init.js +21 -25
  56. package/dist/lib/test-aware-get-token.js +86 -81
  57. package/dist/lib/token-lifecycle.d.ts +78 -52
  58. package/dist/lib/token-lifecycle.js +360 -398
  59. package/dist/pages/admin-login/page.js +73 -83
  60. package/dist/pages/client-admin/ClientSiteAdminPage.js +179 -177
  61. package/dist/pages/login/page.js +202 -211
  62. package/dist/pages/showcase/ShowcasePage.js +142 -140
  63. package/dist/pages/test-env/EmergencyLogoutPage.js +99 -98
  64. package/dist/pages/test-env/JwtInspectPage.js +116 -114
  65. package/dist/pages/test-env/RefreshTokenPage.js +4 -2
  66. package/dist/pages/test-env/TestEnvPage.js +51 -49
  67. package/dist/pages/verify-code/page.js +412 -408
  68. package/dist/routes/auth/logout.d.ts +31 -31
  69. package/dist/routes/auth/logout.js +98 -113
  70. package/dist/routes/auth/nextauth.d.ts +14 -11
  71. package/dist/routes/auth/nextauth.js +25 -57
  72. package/dist/routes/auth/session.js +157 -179
  73. package/dist/routes/auth/viability.js +190 -201
  74. package/dist/server/auth.d.ts +50 -0
  75. package/dist/server/auth.js +62 -0
  76. package/dist/stores/authStore.js +19 -23
  77. package/dist/utils/logout.js +5 -5
  78. package/package.json +1 -3
  79. package/src/api/auth-handler.ts +550 -549
  80. package/src/api-handlers/account/change-password.ts +5 -8
  81. package/src/api-handlers/admin/analytics.ts +4 -6
  82. package/src/api-handlers/admin/audit.ts +5 -7
  83. package/src/api-handlers/admin/index.ts +1 -2
  84. package/src/api-handlers/admin/redis-sessions.ts +6 -8
  85. package/src/api-handlers/admin/sessions.ts +5 -7
  86. package/src/api-handlers/admin/site-logs.ts +8 -10
  87. package/src/api-handlers/admin/stats.ts +4 -6
  88. package/src/api-handlers/admin/users.ts +5 -7
  89. package/src/api-handlers/admin/vibe-data.ts +10 -12
  90. package/src/api-handlers/auth/refresh.ts +5 -7
  91. package/src/api-handlers/auth/signout.ts +5 -6
  92. package/src/api-handlers/auth/status.ts +4 -7
  93. package/src/api-handlers/auth/update-session.ts +123 -125
  94. package/src/api-handlers/auth/verify-code.ts +9 -13
  95. package/src/api-handlers/session/viability.ts +10 -47
  96. package/src/api-handlers/test/force-expire.ts +4 -11
  97. package/src/auth/auth-decision.ts +1 -1
  98. package/src/auth/better-auth.ts +138 -141
  99. package/src/auth/route-config.ts +219 -219
  100. package/src/auth/utils/token-utils.ts +0 -1
  101. package/src/client/AuthContext.tsx +6 -2
  102. package/src/client/fetch-with-auth.ts +47 -47
  103. package/src/components/SessionSync.tsx +6 -5
  104. package/src/components/account/MobileNavDrawer.tsx +3 -3
  105. package/src/components/account/UserAvatarMenu.tsx +6 -3
  106. package/src/components/admin/VibeAdminLayout.tsx +4 -2
  107. package/src/config/logger.ts +1 -1
  108. package/src/hooks/useAuth.ts +117 -115
  109. package/src/hooks/useAuthSettings.ts +2 -2
  110. package/src/hooks/useAvailableProviders.ts +9 -5
  111. package/src/hooks/useSessionExpiration.ts +101 -102
  112. package/src/hooks/useViabilitySession.ts +336 -335
  113. package/src/index.ts +60 -63
  114. package/src/lib/api-handler.ts +0 -1
  115. package/src/lib/app-slug.ts +6 -6
  116. package/src/lib/standardized-client-api.ts +901 -895
  117. package/src/lib/startup-init.ts +243 -247
  118. package/src/lib/test-aware-get-token.ts +22 -12
  119. package/src/lib/token-lifecycle.ts +12 -53
  120. package/src/pages/admin-login/page.tsx +9 -17
  121. package/src/pages/client-admin/ClientSiteAdminPage.tsx +4 -2
  122. package/src/pages/login/page.tsx +21 -28
  123. package/src/pages/showcase/ShowcasePage.tsx +4 -2
  124. package/src/pages/test-env/EmergencyLogoutPage.tsx +7 -6
  125. package/src/pages/test-env/JwtInspectPage.tsx +5 -3
  126. package/src/pages/test-env/RefreshTokenPage.tsx +157 -155
  127. package/src/pages/test-env/TestEnvPage.tsx +4 -2
  128. package/src/pages/verify-code/page.tsx +10 -6
  129. package/src/routes/auth/logout.ts +7 -25
  130. package/src/routes/auth/nextauth.ts +45 -71
  131. package/src/routes/auth/session.ts +25 -50
  132. package/src/routes/auth/viability.ts +7 -19
  133. package/src/server/auth.ts +60 -0
  134. package/src/stores/authStore.ts +1899 -1904
  135. package/src/utils/logout.ts +30 -30
  136. package/src/auth/auth-options.ts +0 -237
  137. package/src/auth/callbacks/index.ts +0 -7
  138. package/src/auth/callbacks/jwt.ts +0 -382
  139. package/src/auth/callbacks/session.ts +0 -243
  140. package/src/auth/callbacks/signin.ts +0 -56
  141. package/src/auth/events/index.ts +0 -5
  142. package/src/auth/events/signout.ts +0 -33
  143. package/src/auth/providers/credentials.ts +0 -256
  144. package/src/auth/providers/index.ts +0 -6
  145. package/src/auth/providers/oauth.ts +0 -114
  146. package/src/lib/nextauth-secret.ts +0 -121
  147. package/src/types/next-auth.d.ts +0 -15
@@ -1,141 +1,138 @@
1
- /**
2
- * Better Auth Configuration (Phase 1 — parallel install)
3
- *
4
- * NOT wired to routes yet. Exists alongside auth-options.ts for testing.
5
- * Wired in Phase 2 behind USE_BETTER_AUTH flag.
6
- *
7
- * Architecture: No database adapter Better Auth runs in stateless mode
8
- * with JWE cookie cache. User management stays on IDP, sessions on Redis.
9
- *
10
- * @see BETTER-AUTH-MIGRATION-SPEC.md
11
- */
12
-
13
- import 'server-only';
14
- import { betterAuth } from 'better-auth';
15
- import { nextCookies } from 'better-auth/next-js';
16
- import { toNextJsHandler } from 'better-auth/next-js';
17
- import type { IDPClientConfig } from '../lib/idp-client-config';
18
- import { getIDPClientConfig } from '../lib/idp-client-config';
19
-
20
- /**
21
- * Better Auth social provider config shape.
22
- */
23
- export interface BetterAuthSocialProvider {
24
- clientId: string;
25
- clientSecret: string;
26
- scope?: string[];
27
- }
28
-
29
- /**
30
- * Build Better Auth social providers from IDP config.
31
- * Replaces buildOAuthProviders() from providers/oauth.ts.
32
- */
33
- export function buildBetterAuthProviders(
34
- config: IDPClientConfig
35
- ): Record<string, BetterAuthSocialProvider> {
36
- const providers: Record<string, BetterAuthSocialProvider> = {};
37
-
38
- for (const oauth of config.oauthProviders || []) {
39
- if (!oauth.enabled) continue;
40
- const name = oauth.provider.toLowerCase();
41
- providers[name] = {
42
- clientId: oauth.clientId,
43
- clientSecret: oauth.clientSecret,
44
- scope: oauth.scopes?.split(' '),
45
- };
46
- }
47
-
48
- return providers;
49
- }
50
-
51
- /**
52
- * Create Better Auth instance from IDP config.
53
- *
54
- * No database — runs in stateless mode with JWE cookie cache.
55
- * Call after getIDPClientConfig() resolves.
56
- */
57
- export function createBetterAuthInstance(idpConfig: IDPClientConfig) {
58
- return betterAuth({
59
- secret: idpConfig.nextAuthSecret as string,
60
-
61
- socialProviders: buildBetterAuthProviders(idpConfig),
62
-
63
- // Trust the app's own origin + any configured base URL
64
- trustedOrigins: [
65
- ...(idpConfig.baseClientUrl ? [idpConfig.baseClientUrl] : []),
66
- ...(process.env.BETTER_AUTH_URL ? [process.env.BETTER_AUTH_URL] : []),
67
- 'http://localhost:3000',
68
- 'http://localhost:3400',
69
- 'http://localhost:3600',
70
- ],
71
-
72
- // No database — stateless mode. Better Auth defaults to JWE cookie cache.
73
- // Session cookie cache with refreshCache for DB-less setup.
74
- session: {
75
- cookieCache: {
76
- enabled: true,
77
- maxAge: 300,
78
- refreshCache: true,
79
- },
80
- },
81
-
82
- plugins: [
83
- nextCookies(),
84
- ],
85
- });
86
- }
87
-
88
- /**
89
- * Check if Better Auth is enabled via flag.
90
- */
91
- export function isBetterAuthEnabled(): boolean {
92
- return process.env.USE_BETTER_AUTH === 'true';
93
- }
94
-
95
- /**
96
- * Get Better Auth Next.js route handlers (GET, POST).
97
- * Initializes Better Auth from IDP config on first call, caches the instance.
98
- */
99
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
100
- let cachedInstance: any = null;
101
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
102
- let initPromise: Promise<any> | null = null;
103
-
104
- async function getBetterAuthInstance() {
105
- if (cachedInstance) return cachedInstance;
106
-
107
- if (!initPromise) {
108
- initPromise = getIDPClientConfig().then(config => {
109
- const instance = createBetterAuthInstance(config);
110
- cachedInstance = instance;
111
- console.log('[BETTER_AUTH] Instance created for', config.clientSlug || config.clientId);
112
- return instance;
113
- });
114
- }
115
-
116
- return initPromise;
117
- }
118
-
119
- /**
120
- * Get flag-gated auth handler for Next.js route.
121
- *
122
- * When USE_BETTER_AUTH=true, returns Better Auth handlers.
123
- * Otherwise returns null (caller uses NextAuth).
124
- *
125
- * Usage in host app route:
126
- * ```ts
127
- * import { getBetterAuthHandler } from '@payez/next-mvp/auth/better-auth';
128
- *
129
- * export async function GET(req: Request) {
130
- * const ba = await getBetterAuthHandler();
131
- * if (ba) return ba.GET(req);
132
- * // ... existing NextAuth handler
133
- * }
134
- * ```
135
- */
136
- export async function getBetterAuthHandler(): Promise<{ GET: (req: Request) => Promise<Response>; POST: (req: Request) => Promise<Response> } | null> {
137
- if (!isBetterAuthEnabled()) return null;
138
-
139
- const auth = await getBetterAuthInstance();
140
- return toNextJsHandler(auth);
141
- }
1
+ /**
2
+ * Better Auth Configuration
3
+ *
4
+ * Primary auth configuration. Replaces the former NextAuth auth-options.ts.
5
+ *
6
+ * Architecture: No database adapter — Better Auth runs in stateless mode
7
+ * with JWE cookie cache. User management stays on IDP, sessions on Redis.
8
+ *
9
+ * @see BETTER-AUTH-MIGRATION-SPEC.md
10
+ */
11
+
12
+ import 'server-only';
13
+ import { betterAuth } from 'better-auth';
14
+ import { nextCookies } from 'better-auth/next-js';
15
+ import { toNextJsHandler } from 'better-auth/next-js';
16
+ import type { IDPClientConfig } from '../lib/idp-client-config';
17
+ import { getIDPClientConfig } from '../lib/idp-client-config';
18
+
19
+ /**
20
+ * Better Auth social provider config shape.
21
+ */
22
+ export interface BetterAuthSocialProvider {
23
+ clientId: string;
24
+ clientSecret: string;
25
+ scope?: string[];
26
+ }
27
+
28
+ /**
29
+ * Build Better Auth social providers from IDP config.
30
+ */
31
+ export function buildBetterAuthProviders(
32
+ config: IDPClientConfig
33
+ ): Record<string, BetterAuthSocialProvider> {
34
+ const providers: Record<string, BetterAuthSocialProvider> = {};
35
+
36
+ for (const oauth of config.oauthProviders || []) {
37
+ if (!oauth.enabled) continue;
38
+ const name = oauth.provider.toLowerCase();
39
+ providers[name] = {
40
+ clientId: oauth.clientId,
41
+ clientSecret: oauth.clientSecret,
42
+ scope: oauth.scopes?.split(' '),
43
+ };
44
+ }
45
+
46
+ return providers;
47
+ }
48
+
49
+ /**
50
+ * Create Better Auth instance from IDP config.
51
+ *
52
+ * No database runs in stateless mode with JWE cookie cache.
53
+ * Call after getIDPClientConfig() resolves.
54
+ */
55
+ export function createBetterAuthInstance(idpConfig: IDPClientConfig) {
56
+ return betterAuth({
57
+ secret: idpConfig.nextAuthSecret as string,
58
+
59
+ socialProviders: buildBetterAuthProviders(idpConfig),
60
+
61
+ // Trust the app's own origin + any configured base URL
62
+ trustedOrigins: [
63
+ ...(idpConfig.baseClientUrl ? [idpConfig.baseClientUrl] : []),
64
+ ...(process.env.BETTER_AUTH_URL ? [process.env.BETTER_AUTH_URL] : []),
65
+ 'http://localhost:3000',
66
+ 'http://localhost:3400',
67
+ 'http://localhost:3600',
68
+ ],
69
+
70
+ // No database — stateless mode. Better Auth defaults to JWE cookie cache.
71
+ // Session cookie cache with refreshCache for DB-less setup.
72
+ session: {
73
+ cookieCache: {
74
+ enabled: true,
75
+ maxAge: 300,
76
+ refreshCache: true,
77
+ },
78
+ },
79
+
80
+ plugins: [
81
+ nextCookies(),
82
+ ],
83
+ });
84
+ }
85
+
86
+ /**
87
+ * Check if Better Auth is enabled via flag.
88
+ */
89
+ export function isBetterAuthEnabled(): boolean {
90
+ return process.env.USE_BETTER_AUTH === 'true';
91
+ }
92
+
93
+ /**
94
+ * Get Better Auth Next.js route handlers (GET, POST).
95
+ * Initializes Better Auth from IDP config on first call, caches the instance.
96
+ */
97
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
98
+ let cachedInstance: any = null;
99
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
100
+ let initPromise: Promise<any> | null = null;
101
+
102
+ async function getBetterAuthInstance() {
103
+ if (cachedInstance) return cachedInstance;
104
+
105
+ if (!initPromise) {
106
+ initPromise = getIDPClientConfig().then(config => {
107
+ const instance = createBetterAuthInstance(config);
108
+ cachedInstance = instance;
109
+ console.log('[BETTER_AUTH] Instance created for', config.clientSlug || config.clientId);
110
+ return instance;
111
+ });
112
+ }
113
+
114
+ return initPromise;
115
+ }
116
+
117
+ /**
118
+ * Get flag-gated auth handler for Next.js route.
119
+ *
120
+ * When USE_BETTER_AUTH=true, returns Better Auth handlers.
121
+ * Otherwise returns null (auth disabled).
122
+ *
123
+ * Usage in host app route:
124
+ * ```ts
125
+ * import { getBetterAuthHandler } from '@payez/next-mvp/auth/better-auth';
126
+ *
127
+ * export async function GET(req: Request) {
128
+ * const ba = await getBetterAuthHandler();
129
+ * if (ba) return ba.GET(req);
130
+ * }
131
+ * ```
132
+ */
133
+ export async function getBetterAuthHandler(): Promise<{ GET: (req: Request) => Promise<Response>; POST: (req: Request) => Promise<Response> } | null> {
134
+ if (!isBetterAuthEnabled()) return null;
135
+
136
+ const auth = await getBetterAuthInstance();
137
+ return toNextJsHandler(auth);
138
+ }