@nextsparkjs/core 0.1.0-beta.185 → 0.1.0-beta.187

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 (518) hide show
  1. package/dist/components/app/guards/DeveloperGuard.d.ts.map +1 -1
  2. package/dist/components/app/guards/DeveloperGuard.js +8 -4
  3. package/dist/components/app/guards/SuperAdminGuard.d.ts.map +1 -1
  4. package/dist/components/app/guards/SuperAdminGuard.js +8 -4
  5. package/dist/components/app/layouts/PublicFooter.d.ts.map +1 -1
  6. package/dist/components/app/layouts/PublicFooter.js +2 -2
  7. package/dist/components/app/layouts/PublicNavbar.d.ts.map +1 -1
  8. package/dist/components/app/layouts/PublicNavbar.js +2 -2
  9. package/dist/components/app/misc/ThemeToggle.d.ts.map +1 -1
  10. package/dist/components/app/misc/ThemeToggle.js +4 -1
  11. package/dist/components/auth/DevKeyring.d.ts.map +1 -1
  12. package/dist/components/auth/DevKeyring.js +10 -2
  13. package/dist/components/auth/SessionCookieRefresher.d.ts +21 -0
  14. package/dist/components/auth/SessionCookieRefresher.d.ts.map +1 -0
  15. package/dist/components/auth/SessionCookieRefresher.js +11 -0
  16. package/dist/components/auth/forms/LoginForm.d.ts.map +1 -1
  17. package/dist/components/auth/forms/LoginForm.js +300 -6
  18. package/dist/components/auth/index.d.ts +1 -0
  19. package/dist/components/auth/index.d.ts.map +1 -1
  20. package/dist/components/auth/index.js +2 -0
  21. package/dist/components/billing/InvoicesTable.d.ts.map +1 -1
  22. package/dist/components/billing/InvoicesTable.js +4 -3
  23. package/dist/components/dashboard/block-editor/dynamic-form.d.ts.map +1 -1
  24. package/dist/components/dashboard/layouts/AuthGuard.d.ts.map +1 -1
  25. package/dist/components/dashboard/layouts/AuthGuard.js +2 -2
  26. package/dist/components/dashboard/layouts/DashboardAuthLayout.d.ts.map +1 -1
  27. package/dist/components/dashboard/layouts/DashboardAuthLayout.js +2 -2
  28. package/dist/components/dashboard/layouts/DashboardAuthSkeleton.d.ts +17 -0
  29. package/dist/components/dashboard/layouts/DashboardAuthSkeleton.d.ts.map +1 -0
  30. package/dist/components/dashboard/layouts/DashboardAuthSkeleton.js +52 -0
  31. package/dist/components/dashboard/layouts/Sidebar.d.ts.map +1 -1
  32. package/dist/components/dashboard/layouts/Sidebar.js +2 -3
  33. package/dist/components/dashboard/layouts/TopNavbar.js +2 -2
  34. package/dist/components/dashboard/layouts/index.d.ts +1 -0
  35. package/dist/components/dashboard/layouts/index.d.ts.map +1 -1
  36. package/dist/components/dashboard/layouts/index.js +2 -0
  37. package/dist/components/devtools/api-explorer/ApiExplorer.js +4 -4
  38. package/dist/components/entities/EntityFieldRenderer.d.ts.map +1 -1
  39. package/dist/components/entities/EntityFieldRenderer.js +3 -0
  40. package/dist/components/entities/EntityTable.d.ts.map +1 -1
  41. package/dist/components/entities/EntityTable.js +2 -1
  42. package/dist/components/entities/wrappers/EntityFormWrapper.d.ts.map +1 -1
  43. package/dist/components/entities/wrappers/EntityFormWrapper.js +7 -2
  44. package/dist/components/patterns/pages/PatternEditPage.d.ts +5 -1
  45. package/dist/components/patterns/pages/PatternEditPage.d.ts.map +1 -1
  46. package/dist/components/patterns/pages/PatternEditPage.js +16 -5
  47. package/dist/components/patterns/pages/PatternsCreatePage.d.ts +2 -1
  48. package/dist/components/patterns/pages/PatternsCreatePage.d.ts.map +1 -1
  49. package/dist/components/patterns/pages/PatternsCreatePage.js +17 -5
  50. package/dist/components/ui/combobox.d.ts +8 -1
  51. package/dist/components/ui/combobox.d.ts.map +1 -1
  52. package/dist/components/ui/combobox.js +37 -44
  53. package/dist/components/ui/country-select.d.ts +3 -1
  54. package/dist/components/ui/country-select.d.ts.map +1 -1
  55. package/dist/components/ui/country-select.js +3 -1
  56. package/dist/components/ui/currency-select.d.ts +3 -1
  57. package/dist/components/ui/currency-select.d.ts.map +1 -1
  58. package/dist/components/ui/currency-select.js +3 -1
  59. package/dist/components/ui/sheet.d.ts +5 -0
  60. package/dist/components/ui/sheet.d.ts.map +1 -1
  61. package/dist/components/ui/sheet.js +46 -26
  62. package/dist/components/ui/timezone-select.d.ts +3 -1
  63. package/dist/components/ui/timezone-select.d.ts.map +1 -1
  64. package/dist/components/ui/timezone-select.js +3 -1
  65. package/dist/contexts/TeamContext.d.ts +6 -0
  66. package/dist/contexts/TeamContext.d.ts.map +1 -1
  67. package/dist/contexts/TeamContext.js +8 -1
  68. package/dist/emails/otp-verification.d.ts.map +1 -1
  69. package/dist/emails/reset-password.d.ts.map +1 -1
  70. package/dist/emails/team-invitation.d.ts.map +1 -1
  71. package/dist/emails/team-invitation.js +3 -2
  72. package/dist/emails/verify-email.d.ts.map +1 -1
  73. package/dist/hooks/index.d.ts +1 -0
  74. package/dist/hooks/index.d.ts.map +1 -1
  75. package/dist/hooks/index.js +1 -0
  76. package/dist/hooks/useAuth.d.ts +29 -0
  77. package/dist/hooks/useAuth.d.ts.map +1 -1
  78. package/dist/hooks/useAuth.js +24 -0
  79. package/dist/hooks/useEnsureUserMetadata.d.ts +22 -0
  80. package/dist/hooks/useEnsureUserMetadata.d.ts.map +1 -1
  81. package/dist/hooks/useEnsureUserMetadata.js +18 -5
  82. package/dist/hooks/useSessionCookieRefresh.d.ts +35 -0
  83. package/dist/hooks/useSessionCookieRefresh.d.ts.map +1 -0
  84. package/dist/hooks/useSessionCookieRefresh.js +49 -0
  85. package/dist/lib/api/api-error.d.ts +3 -1
  86. package/dist/lib/api/api-error.d.ts.map +1 -1
  87. package/dist/lib/api/api-error.js +4 -2
  88. package/dist/lib/api/auth/dual-auth.d.ts +38 -3
  89. package/dist/lib/api/auth/dual-auth.d.ts.map +1 -1
  90. package/dist/lib/api/auth/dual-auth.js +42 -7
  91. package/dist/lib/api/auth/permissions.d.ts +7 -2
  92. package/dist/lib/api/auth/permissions.d.ts.map +1 -1
  93. package/dist/lib/api/auth/permissions.js +2 -2
  94. package/dist/lib/api/auth/scope-policy.d.ts +71 -0
  95. package/dist/lib/api/auth/scope-policy.d.ts.map +1 -0
  96. package/dist/lib/api/auth/scope-policy.js +53 -0
  97. package/dist/lib/api/auth.d.ts +15 -0
  98. package/dist/lib/api/auth.d.ts.map +1 -1
  99. package/dist/lib/api/auth.js +17 -4
  100. package/dist/lib/api/entities.js +2 -1
  101. package/dist/lib/api/entity/audit-log.d.ts +35 -0
  102. package/dist/lib/api/entity/audit-log.d.ts.map +1 -0
  103. package/dist/lib/api/entity/audit-log.js +35 -0
  104. package/dist/lib/api/entity/generic-handler.d.ts.map +1 -1
  105. package/dist/lib/api/entity/generic-handler.js +333 -138
  106. package/dist/lib/api/entity-handler.js +2 -2
  107. package/dist/lib/api/helpers.d.ts +29 -4
  108. package/dist/lib/api/helpers.d.ts.map +1 -1
  109. package/dist/lib/api/helpers.js +30 -6
  110. package/dist/lib/api/keys.d.ts +16 -0
  111. package/dist/lib/api/keys.d.ts.map +1 -1
  112. package/dist/lib/api/keys.js +19 -1
  113. package/dist/lib/api/routes/patterns-usages.d.ts.map +1 -1
  114. package/dist/lib/api/routes/patterns-usages.js +3 -11
  115. package/dist/lib/auth/auth-methods.d.ts +52 -0
  116. package/dist/lib/auth/auth-methods.d.ts.map +1 -0
  117. package/dist/lib/auth/auth-methods.js +63 -0
  118. package/dist/lib/auth/registration-guard-plugin.d.ts.map +1 -1
  119. package/dist/lib/auth/registration-helpers.d.ts +9 -0
  120. package/dist/lib/auth/registration-helpers.d.ts.map +1 -1
  121. package/dist/lib/auth/registration-helpers.js +8 -2
  122. package/dist/lib/auth/security-notifications/device-fingerprint.d.ts +52 -0
  123. package/dist/lib/auth/security-notifications/device-fingerprint.d.ts.map +1 -0
  124. package/dist/lib/auth/security-notifications/device-fingerprint.js +42 -0
  125. package/dist/lib/auth/security-notifications/dispatcher.d.ts +37 -0
  126. package/dist/lib/auth/security-notifications/dispatcher.d.ts.map +1 -0
  127. package/dist/lib/auth/security-notifications/dispatcher.js +157 -0
  128. package/dist/lib/auth/security-notifications/index.d.ts +20 -0
  129. package/dist/lib/auth/security-notifications/index.d.ts.map +1 -0
  130. package/dist/lib/auth/security-notifications/index.js +22 -0
  131. package/dist/lib/auth/security-notifications/registry.d.ts +36 -0
  132. package/dist/lib/auth/security-notifications/registry.d.ts.map +1 -0
  133. package/dist/lib/auth/security-notifications/registry.js +26 -0
  134. package/dist/lib/auth/security-notifications/templates/email-changed.d.ts +12 -0
  135. package/dist/lib/auth/security-notifications/templates/email-changed.d.ts.map +1 -0
  136. package/dist/lib/auth/security-notifications/templates/email-changed.js +43 -0
  137. package/dist/lib/auth/security-notifications/templates/new-device-login.d.ts +7 -0
  138. package/dist/lib/auth/security-notifications/templates/new-device-login.d.ts.map +1 -0
  139. package/dist/lib/auth/security-notifications/templates/new-device-login.js +46 -0
  140. package/dist/lib/auth/security-notifications/templates/password-changed.d.ts +7 -0
  141. package/dist/lib/auth/security-notifications/templates/password-changed.d.ts.map +1 -0
  142. package/dist/lib/auth/security-notifications/templates/password-changed.js +35 -0
  143. package/dist/lib/auth/security-notifications/templates/shared.d.ts +36 -0
  144. package/dist/lib/auth/security-notifications/templates/shared.d.ts.map +1 -0
  145. package/dist/lib/auth/security-notifications/templates/shared.js +68 -0
  146. package/dist/lib/auth/security-notifications/types.d.ts +74 -0
  147. package/dist/lib/auth/security-notifications/types.d.ts.map +1 -0
  148. package/dist/lib/auth/session-config.d.ts +37 -0
  149. package/dist/lib/auth/session-config.d.ts.map +1 -0
  150. package/dist/lib/auth/session-config.js +59 -0
  151. package/dist/lib/auth/session-refresh.d.ts +46 -0
  152. package/dist/lib/auth/session-refresh.d.ts.map +1 -0
  153. package/dist/lib/auth/session-refresh.js +11 -0
  154. package/dist/lib/auth-client.d.ts +1639 -0
  155. package/dist/lib/auth-client.d.ts.map +1 -0
  156. package/dist/lib/auth-client.js +13 -1
  157. package/dist/lib/auth.d.ts.map +1 -1
  158. package/dist/lib/auth.js +45 -11
  159. package/dist/lib/billing/gateways/factory.d.ts +8 -3
  160. package/dist/lib/billing/gateways/factory.d.ts.map +1 -1
  161. package/dist/lib/billing/gateways/factory.js +37 -23
  162. package/dist/lib/billing/stripe-webhook.d.ts.map +1 -1
  163. package/dist/lib/billing/stripe-webhook.js +2 -1
  164. package/dist/lib/config/app.config.d.ts.map +1 -1
  165. package/dist/lib/config/app.config.js +71 -0
  166. package/dist/lib/config/config-sync.d.ts +1 -4
  167. package/dist/lib/config/config-sync.d.ts.map +1 -1
  168. package/dist/lib/config/config-sync.js +6 -2
  169. package/dist/lib/config/types.d.ts +130 -0
  170. package/dist/lib/config/types.d.ts.map +1 -1
  171. package/dist/lib/email/templates.d.ts +3 -3
  172. package/dist/lib/email/templates.d.ts.map +1 -1
  173. package/dist/lib/entities/api-generator.js +5 -5
  174. package/dist/lib/entities/entity-hooks.d.ts +9 -1
  175. package/dist/lib/entities/entity-hooks.d.ts.map +1 -1
  176. package/dist/lib/entities/entity-hooks.js +77 -4
  177. package/dist/lib/entities/schema-generator.d.ts.map +1 -1
  178. package/dist/lib/entities/schema-generator.js +20 -4
  179. package/dist/lib/entities/serialization.d.ts +2 -2
  180. package/dist/lib/entities/serialization.d.ts.map +1 -1
  181. package/dist/lib/entities/serialization.js +3 -2
  182. package/dist/lib/entities/types.d.ts +20 -0
  183. package/dist/lib/entities/types.d.ts.map +1 -1
  184. package/dist/lib/locale.d.ts.map +1 -1
  185. package/dist/lib/locale.js +4 -1
  186. package/dist/lib/mcp/audit.d.ts +8 -4
  187. package/dist/lib/mcp/audit.d.ts.map +1 -1
  188. package/dist/lib/plugins/hook-system.d.ts +13 -0
  189. package/dist/lib/plugins/hook-system.d.ts.map +1 -1
  190. package/dist/lib/plugins/hook-system.js +32 -2
  191. package/dist/lib/scheduled-actions/handlers/index.d.ts +1 -0
  192. package/dist/lib/scheduled-actions/handlers/index.d.ts.map +1 -1
  193. package/dist/lib/scheduled-actions/handlers/index.js +3 -1
  194. package/dist/lib/scheduled-actions/handlers/security-notification.d.ts +26 -0
  195. package/dist/lib/scheduled-actions/handlers/security-notification.d.ts.map +1 -0
  196. package/dist/lib/scheduled-actions/handlers/security-notification.js +54 -0
  197. package/dist/lib/selectors/core-selectors.d.ts +12 -0
  198. package/dist/lib/selectors/core-selectors.d.ts.map +1 -1
  199. package/dist/lib/selectors/domains/auth.selectors.d.ts +12 -0
  200. package/dist/lib/selectors/domains/auth.selectors.d.ts.map +1 -1
  201. package/dist/lib/selectors/domains/auth.selectors.js +14 -1
  202. package/dist/lib/selectors/selectors.d.ts +24 -0
  203. package/dist/lib/selectors/selectors.d.ts.map +1 -1
  204. package/dist/lib/services/api-docs.service.d.ts +5 -1
  205. package/dist/lib/services/api-docs.service.d.ts.map +1 -1
  206. package/dist/lib/services/api-presets.service.d.ts +6 -1
  207. package/dist/lib/services/api-presets.service.d.ts.map +1 -1
  208. package/dist/lib/services/feature.service.d.ts +58 -4
  209. package/dist/lib/services/feature.service.d.ts.map +1 -1
  210. package/dist/lib/services/generic-entity.service.d.ts.map +1 -1
  211. package/dist/lib/services/generic-entity.service.js +16 -32
  212. package/dist/lib/services/membership.service.d.ts +10 -2
  213. package/dist/lib/services/membership.service.d.ts.map +1 -1
  214. package/dist/lib/services/membership.service.js +37 -16
  215. package/dist/lib/services/permission.service.d.ts +10 -4
  216. package/dist/lib/services/permission.service.d.ts.map +1 -1
  217. package/dist/lib/services/plugin.service.d.ts +1 -1
  218. package/dist/lib/services/plugin.service.d.ts.map +1 -1
  219. package/dist/lib/services/subscription.service.d.ts.map +1 -1
  220. package/dist/lib/services/subscription.service.js +2 -1
  221. package/dist/lib/services/team-member.service.d.ts +15 -3
  222. package/dist/lib/services/team-member.service.d.ts.map +1 -1
  223. package/dist/lib/services/team-member.service.js +27 -6
  224. package/dist/lib/services/team.service.d.ts.map +1 -1
  225. package/dist/lib/teams/permissions.d.ts +3 -1
  226. package/dist/lib/teams/permissions.d.ts.map +1 -1
  227. package/dist/lib/teams/permissions.js +6 -0
  228. package/dist/lib/teams/schema.d.ts.map +1 -1
  229. package/dist/lib/teams/schema.js +3 -0
  230. package/dist/lib/template-resolver.d.ts +3 -3
  231. package/dist/lib/theme/forced-theme.d.ts +34 -0
  232. package/dist/lib/theme/forced-theme.d.ts.map +1 -0
  233. package/dist/lib/theme/forced-theme.js +27 -0
  234. package/dist/lib/theme/get-default-theme-mode.d.ts +3 -0
  235. package/dist/lib/theme/get-default-theme-mode.d.ts.map +1 -1
  236. package/dist/lib/theme/get-default-theme-mode.js +5 -3
  237. package/dist/lib/utils/cors.d.ts +13 -0
  238. package/dist/lib/utils/cors.d.ts.map +1 -1
  239. package/dist/lib/utils/cors.js +20 -0
  240. package/dist/messages/de/auth.json +25 -2
  241. package/dist/messages/de/index.d.ts +83 -0
  242. package/dist/messages/de/index.d.ts.map +1 -1
  243. package/dist/messages/de/index.js +2 -0
  244. package/dist/messages/de/index.ts +2 -0
  245. package/dist/messages/de/navigation.json +3 -1
  246. package/dist/messages/en/auth.json +25 -2
  247. package/dist/messages/en/index.d.ts +83 -0
  248. package/dist/messages/en/index.d.ts.map +1 -1
  249. package/dist/messages/en/index.js +2 -0
  250. package/dist/messages/en/index.ts +2 -0
  251. package/dist/messages/en/navigation.json +3 -1
  252. package/dist/messages/es/auth.json +25 -2
  253. package/dist/messages/es/index.d.ts +83 -0
  254. package/dist/messages/es/index.d.ts.map +1 -1
  255. package/dist/messages/es/index.js +2 -0
  256. package/dist/messages/es/index.ts +2 -0
  257. package/dist/messages/es/navigation.json +3 -1
  258. package/dist/messages/fr/auth.json +25 -2
  259. package/dist/messages/fr/index.d.ts +83 -0
  260. package/dist/messages/fr/index.d.ts.map +1 -1
  261. package/dist/messages/fr/index.js +2 -0
  262. package/dist/messages/fr/index.ts +2 -0
  263. package/dist/messages/fr/navigation.json +3 -1
  264. package/dist/messages/it/auth.json +25 -2
  265. package/dist/messages/it/index.d.ts +83 -0
  266. package/dist/messages/it/index.d.ts.map +1 -1
  267. package/dist/messages/it/index.js +2 -0
  268. package/dist/messages/it/index.ts +2 -0
  269. package/dist/messages/it/navigation.json +3 -1
  270. package/dist/messages/pt/auth.json +25 -2
  271. package/dist/messages/pt/index.d.ts +83 -0
  272. package/dist/messages/pt/index.d.ts.map +1 -1
  273. package/dist/messages/pt/index.js +2 -0
  274. package/dist/messages/pt/index.ts +2 -0
  275. package/dist/messages/pt/navigation.json +3 -1
  276. package/dist/migrations/005_api_audit_log.sql +2 -1
  277. package/dist/migrations/025_api_audit_log_nullable_api_key.sql +12 -0
  278. package/dist/migrations/026_login_events.sql +91 -0
  279. package/dist/migrations/027_rls_bypass_developer_and_system_admin_team.sql +197 -0
  280. package/dist/nextspark-registries.d.ts +580 -236
  281. package/dist/providers/DashboardProviders.d.ts +12 -3
  282. package/dist/providers/DashboardProviders.d.ts.map +1 -1
  283. package/dist/providers/DashboardProviders.js +6 -2
  284. package/dist/providers/theme-provider.d.ts +19 -2
  285. package/dist/providers/theme-provider.d.ts.map +1 -1
  286. package/dist/providers/theme-provider.js +6 -2
  287. package/dist/templates/app/(auth)/signup/page.tsx +8 -0
  288. package/dist/templates/app/(public)/[...slug]/page.tsx +9 -14
  289. package/dist/templates/app/api/auth/[...all]/route.ts +23 -1
  290. package/dist/templates/app/api/health/route.ts +4 -2
  291. package/dist/templates/app/api/internal/user-metadata/route.ts +5 -3
  292. package/dist/templates/app/api/mcp/route.ts +4 -1
  293. package/dist/templates/app/api/superadmin/subscriptions/route.ts +2 -1
  294. package/dist/templates/app/api/superadmin/teams/[teamId]/route.ts +4 -3
  295. package/dist/templates/app/api/superadmin/users/[userId]/route.ts +10 -3
  296. package/dist/templates/app/api/v1/api-keys/[id]/route.ts +50 -71
  297. package/dist/templates/app/api/v1/api-keys/route.ts +21 -43
  298. package/dist/templates/app/api/v1/auth/signup-with-invite/route.ts +14 -14
  299. package/dist/templates/app/api/v1/billing/cancel/route.ts +7 -6
  300. package/dist/templates/app/api/v1/billing/change-plan/route.ts +5 -4
  301. package/dist/templates/app/api/v1/billing/check-action/route.ts +6 -4
  302. package/dist/templates/app/api/v1/billing/checkout/route.ts +7 -5
  303. package/dist/templates/app/api/v1/billing/plans/route.ts +8 -12
  304. package/dist/templates/app/api/v1/billing/portal/route.ts +7 -5
  305. package/dist/templates/app/api/v1/billing/webhooks/polar/route.ts +1 -1
  306. package/dist/templates/app/api/v1/devtools/blocks/route.ts +5 -4
  307. package/dist/templates/app/api/v1/devtools/features/route.ts +5 -4
  308. package/dist/templates/app/api/v1/devtools/flows/route.ts +5 -4
  309. package/dist/templates/app/api/v1/devtools/scheduled-actions/route.ts +9 -7
  310. package/dist/templates/app/api/v1/devtools/scheduled-actions/run/route.ts +5 -4
  311. package/dist/templates/app/api/v1/devtools/testing/route.ts +5 -4
  312. package/dist/templates/app/api/v1/media/[id]/route.ts +13 -25
  313. package/dist/templates/app/api/v1/media/[id]/tags/route.ts +17 -25
  314. package/dist/templates/app/api/v1/media/check-duplicates/route.ts +5 -7
  315. package/dist/templates/app/api/v1/media/route.ts +5 -9
  316. package/dist/templates/app/api/v1/media/upload/route.ts +9 -21
  317. package/dist/templates/app/api/v1/media-tags/route.ts +9 -13
  318. package/dist/templates/app/api/v1/patterns/[id]/usages/route.ts +11 -19
  319. package/dist/templates/app/api/v1/post-categories/[id]/route.ts +9 -7
  320. package/dist/templates/app/api/v1/post-categories/route.ts +5 -4
  321. package/dist/templates/app/api/v1/team-invitations/[token]/accept/route.ts +16 -18
  322. package/dist/templates/app/api/v1/team-invitations/[token]/decline/route.ts +15 -17
  323. package/dist/templates/app/api/v1/team-invitations/[token]/route.ts +8 -8
  324. package/dist/templates/app/api/v1/team-invitations/route.ts +9 -11
  325. package/dist/templates/app/api/v1/teams/[teamId]/invitations/route.ts +41 -32
  326. package/dist/templates/app/api/v1/teams/[teamId]/invoices/[invoiceNumber]/route.ts +15 -16
  327. package/dist/templates/app/api/v1/teams/[teamId]/invoices/route.ts +14 -15
  328. package/dist/templates/app/api/v1/teams/[teamId]/members/[memberId]/route.ts +32 -36
  329. package/dist/templates/app/api/v1/teams/[teamId]/members/route.ts +33 -33
  330. package/dist/templates/app/api/v1/teams/[teamId]/route.ts +35 -41
  331. package/dist/templates/app/api/v1/teams/[teamId]/subscription/route.ts +5 -3
  332. package/dist/templates/app/api/v1/teams/[teamId]/usage/[limitSlug]/route.ts +10 -6
  333. package/dist/templates/app/api/v1/teams/route.ts +33 -27
  334. package/dist/templates/app/api/v1/teams/switch/route.ts +11 -13
  335. package/dist/templates/app/api/v1/theme/[...path]/route.ts +23 -2
  336. package/dist/templates/app/api/v1/users/[id]/meta/[key]/route.ts +32 -77
  337. package/dist/templates/app/api/v1/users/[id]/route.ts +35 -68
  338. package/dist/templates/app/api/v1/users/route.ts +21 -25
  339. package/dist/templates/app/dashboard/layout.tsx +2 -6
  340. package/dist/templates/app/layout.ppr.tsx +15 -15
  341. package/dist/templates/app/layout.tsx +46 -20
  342. package/dist/templates/contents/themes/starter/config/app.config.ts +19 -0
  343. package/dist/templates/contents/themes/starter/config/theme.config.ts +6 -0
  344. package/dist/templates/contents/themes/starter/styles/globals.css +6 -2
  345. package/dist/templates/proxy.ts +46 -10
  346. package/dist/templates/tsconfig.json +4 -0
  347. package/dist/types/theme.d.ts +9 -0
  348. package/dist/types/theme.d.ts.map +1 -1
  349. package/migrations/005_api_audit_log.sql +2 -1
  350. package/migrations/025_api_audit_log_nullable_api_key.sql +12 -0
  351. package/migrations/026_login_events.sql +91 -0
  352. package/migrations/027_rls_bypass_developer_and_system_admin_team.sql +197 -0
  353. package/package.json +14 -11
  354. package/scripts/build/registry/__tests__/scope-registry.test.mjs +18 -0
  355. package/scripts/build/registry/generators/scope-registry.mjs +9 -2
  356. package/templates/app/(auth)/signup/page.tsx +8 -0
  357. package/templates/app/(public)/[...slug]/page.tsx +9 -14
  358. package/templates/app/api/auth/[...all]/route.ts +23 -1
  359. package/templates/app/api/health/route.ts +4 -2
  360. package/templates/app/api/internal/user-metadata/route.ts +5 -3
  361. package/templates/app/api/mcp/route.ts +4 -1
  362. package/templates/app/api/superadmin/subscriptions/route.ts +2 -1
  363. package/templates/app/api/superadmin/teams/[teamId]/route.ts +4 -3
  364. package/templates/app/api/superadmin/users/[userId]/route.ts +10 -3
  365. package/templates/app/api/v1/api-keys/[id]/route.ts +50 -71
  366. package/templates/app/api/v1/api-keys/route.ts +21 -43
  367. package/templates/app/api/v1/auth/signup-with-invite/route.ts +14 -14
  368. package/templates/app/api/v1/billing/cancel/route.ts +7 -6
  369. package/templates/app/api/v1/billing/change-plan/route.ts +5 -4
  370. package/templates/app/api/v1/billing/check-action/route.ts +6 -4
  371. package/templates/app/api/v1/billing/checkout/route.ts +7 -5
  372. package/templates/app/api/v1/billing/plans/route.ts +8 -12
  373. package/templates/app/api/v1/billing/portal/route.ts +7 -5
  374. package/templates/app/api/v1/billing/webhooks/polar/route.ts +1 -1
  375. package/templates/app/api/v1/devtools/blocks/route.ts +5 -4
  376. package/templates/app/api/v1/devtools/features/route.ts +5 -4
  377. package/templates/app/api/v1/devtools/flows/route.ts +5 -4
  378. package/templates/app/api/v1/devtools/scheduled-actions/route.ts +9 -7
  379. package/templates/app/api/v1/devtools/scheduled-actions/run/route.ts +5 -4
  380. package/templates/app/api/v1/devtools/testing/route.ts +5 -4
  381. package/templates/app/api/v1/media/[id]/route.ts +13 -25
  382. package/templates/app/api/v1/media/[id]/tags/route.ts +17 -25
  383. package/templates/app/api/v1/media/check-duplicates/route.ts +5 -7
  384. package/templates/app/api/v1/media/route.ts +5 -9
  385. package/templates/app/api/v1/media/upload/route.ts +9 -21
  386. package/templates/app/api/v1/media-tags/route.ts +9 -13
  387. package/templates/app/api/v1/patterns/[id]/usages/route.ts +11 -19
  388. package/templates/app/api/v1/post-categories/[id]/route.ts +9 -7
  389. package/templates/app/api/v1/post-categories/route.ts +5 -4
  390. package/templates/app/api/v1/team-invitations/[token]/accept/route.ts +16 -18
  391. package/templates/app/api/v1/team-invitations/[token]/decline/route.ts +15 -17
  392. package/templates/app/api/v1/team-invitations/[token]/route.ts +8 -8
  393. package/templates/app/api/v1/team-invitations/route.ts +9 -11
  394. package/templates/app/api/v1/teams/[teamId]/invitations/route.ts +41 -32
  395. package/templates/app/api/v1/teams/[teamId]/invoices/[invoiceNumber]/route.ts +15 -16
  396. package/templates/app/api/v1/teams/[teamId]/invoices/route.ts +14 -15
  397. package/templates/app/api/v1/teams/[teamId]/members/[memberId]/route.ts +32 -36
  398. package/templates/app/api/v1/teams/[teamId]/members/route.ts +33 -33
  399. package/templates/app/api/v1/teams/[teamId]/route.ts +35 -41
  400. package/templates/app/api/v1/teams/[teamId]/subscription/route.ts +5 -3
  401. package/templates/app/api/v1/teams/[teamId]/usage/[limitSlug]/route.ts +10 -6
  402. package/templates/app/api/v1/teams/route.ts +33 -27
  403. package/templates/app/api/v1/teams/switch/route.ts +11 -13
  404. package/templates/app/api/v1/theme/[...path]/route.ts +23 -2
  405. package/templates/app/api/v1/users/[id]/meta/[key]/route.ts +32 -77
  406. package/templates/app/api/v1/users/[id]/route.ts +35 -68
  407. package/templates/app/api/v1/users/route.ts +21 -25
  408. package/templates/app/dashboard/layout.tsx +2 -6
  409. package/templates/app/layout.ppr.tsx +15 -15
  410. package/templates/app/layout.tsx +46 -20
  411. package/templates/contents/themes/starter/config/app.config.ts +19 -0
  412. package/templates/contents/themes/starter/config/theme.config.ts +6 -0
  413. package/templates/contents/themes/starter/styles/globals.css +6 -2
  414. package/templates/proxy.ts +46 -10
  415. package/templates/tsconfig.json +4 -0
  416. package/tests/jest/__mocks__/@nextsparkjs/registries/permissions-registry.ts +23 -0
  417. package/tests/jest/__mocks__/next-server.js +24 -0
  418. package/dist/components/teams/TeamSwitchModal.d.js +0 -0
  419. package/dist/components/ui/accordion.d.js +0 -0
  420. package/dist/components/ui/avatar.d.js +0 -0
  421. package/dist/components/ui/badge.d.js +0 -0
  422. package/dist/components/ui/button.d.js +0 -0
  423. package/dist/components/ui/card.d.js +0 -0
  424. package/dist/components/ui/dialog.d.js +0 -0
  425. package/dist/components/ui/dropdown-menu.d.js +0 -0
  426. package/dist/components/ui/input.d.js +0 -0
  427. package/dist/components/ui/progress.d.js +0 -0
  428. package/dist/components/ui/scroll-area.d.js +0 -0
  429. package/dist/components/ui/select.d.js +0 -0
  430. package/dist/components/ui/separator.d.js +0 -0
  431. package/dist/components/ui/table.d.js +0 -0
  432. package/dist/components/ui/textarea.d.js +0 -0
  433. package/dist/contexts/TeamContext.d.js +0 -0
  434. package/dist/hooks/useAuth.d.js +0 -0
  435. package/dist/hooks/useLastAuthMethod.d.js +0 -0
  436. package/dist/hooks/useOrigin.d.js +0 -0
  437. package/dist/lib/api/auth/dual-auth.d.js +0 -0
  438. package/dist/lib/api/auth.d.js +0 -0
  439. package/dist/lib/api/cache.d.js +0 -0
  440. package/dist/lib/api/keys.d.js +0 -0
  441. package/dist/lib/auth-client.d.js +0 -0
  442. package/dist/lib/auth-context.d.js +0 -0
  443. package/dist/lib/auth.d.js +0 -0
  444. package/dist/lib/billing/config-types.d.js +0 -0
  445. package/dist/lib/billing/enforcement.d.js +0 -0
  446. package/dist/lib/billing/helpers.d.js +0 -0
  447. package/dist/lib/billing/jobs.d.js +0 -0
  448. package/dist/lib/billing/types.d.js +0 -0
  449. package/dist/lib/blocks/loader.d.js +0 -0
  450. package/dist/lib/config/app.config.d.js +0 -0
  451. package/dist/lib/config/config-loader.d.js +0 -0
  452. package/dist/lib/config/config-sync.d.js +0 -0
  453. package/dist/lib/config/config-types.d.js +0 -0
  454. package/dist/lib/config/config-utils.d.js +0 -0
  455. package/dist/lib/config/dashboard-types.d.js +0 -0
  456. package/dist/lib/config/dashboard.config.d.js +0 -0
  457. package/dist/lib/config/icon-map.d.js +0 -0
  458. package/dist/lib/config/index.d.js +0 -7
  459. package/dist/lib/config/nextspark-types.d.js +0 -0
  460. package/dist/lib/config/roles-merge.d.js +0 -0
  461. package/dist/lib/config/types.d.js +0 -0
  462. package/dist/lib/db.d.js +0 -0
  463. package/dist/lib/email/factory.d.js +0 -0
  464. package/dist/lib/email/index.d.js +0 -3
  465. package/dist/lib/email/providers/console.d.js +0 -0
  466. package/dist/lib/email/providers/resend.d.js +0 -0
  467. package/dist/lib/email/templates.d.js +0 -0
  468. package/dist/lib/email/types.d.js +0 -0
  469. package/dist/lib/entities/types.d.js +0 -0
  470. package/dist/lib/permissions/system.d.js +0 -0
  471. package/dist/lib/permissions/types.d.js +0 -0
  472. package/dist/lib/plugins/hook-system.d.js +0 -0
  473. package/dist/lib/scheduled-actions/cleanup.d.js +0 -0
  474. package/dist/lib/scheduled-actions/index.d.js +0 -19
  475. package/dist/lib/scheduled-actions/initializer.d.js +0 -1
  476. package/dist/lib/scheduled-actions/processor.d.js +0 -0
  477. package/dist/lib/scheduled-actions/registry.d.js +0 -0
  478. package/dist/lib/scheduled-actions/scheduler.d.js +0 -0
  479. package/dist/lib/scheduled-actions/types.d.js +0 -0
  480. package/dist/lib/services/api-routes.service.d.js +0 -0
  481. package/dist/lib/services/entity-type.service.d.js +0 -0
  482. package/dist/lib/services/index.d.js +0 -40
  483. package/dist/lib/services/invoice.service.d.js +0 -0
  484. package/dist/lib/services/membership.service.d.js +0 -0
  485. package/dist/lib/services/meta.service.d.js +0 -0
  486. package/dist/lib/services/middleware.service.d.js +0 -0
  487. package/dist/lib/services/namespace.service.d.js +0 -0
  488. package/dist/lib/services/permission.service.d.js +0 -0
  489. package/dist/lib/services/plan.service.d.js +0 -0
  490. package/dist/lib/services/plugin.service.d.js +0 -0
  491. package/dist/lib/services/route-handler.service.d.js +0 -0
  492. package/dist/lib/services/scope.service.d.js +0 -0
  493. package/dist/lib/services/subscription.service.d.js +0 -0
  494. package/dist/lib/services/team-member.service.d.js +0 -0
  495. package/dist/lib/services/team.service.d.js +0 -0
  496. package/dist/lib/services/theme.service.d.js +0 -0
  497. package/dist/lib/services/usage.service.d.js +0 -0
  498. package/dist/lib/services/user-flags.service.d.js +0 -0
  499. package/dist/lib/services/user.service.d.js +0 -0
  500. package/dist/lib/teams/helpers.d.js +0 -0
  501. package/dist/lib/teams/permissions.d.js +0 -0
  502. package/dist/lib/teams/types.d.js +0 -0
  503. package/dist/lib/utils/config-merge.d.js +0 -0
  504. package/dist/lib/utils/file-logger.d.js +0 -0
  505. package/dist/lib/utils.d.js +0 -0
  506. package/dist/styles/classes.json +0 -1093
  507. package/dist/styles/ui.css +0 -2
  508. package/dist/types/auth.d.js +0 -0
  509. package/dist/types/blocks.d.js +0 -0
  510. package/dist/types/meta.types.d.js +0 -0
  511. package/dist/types/plugin.d.js +0 -0
  512. package/dist/types/testing.types.d.js +0 -0
  513. package/dist/types/theme.d.js +0 -0
  514. package/dist/types/user.types.d.js +0 -0
  515. package/scripts/build/build-ui-css.mjs +0 -351
  516. /package/dist/{components/app/layouts/PublicFooter.d.js → lib/auth/security-notifications/types.js} +0 -0
  517. /package/dist/{components/app/layouts/PublicNavbar.d.js → nextspark-registries.d.js} +0 -0
  518. /package/dist/{components/app/misc/ThemeToggle.d.js → types/use-intl-react-shim.d.js} +0 -0
@@ -14,7 +14,7 @@ import {
14
14
  handleEntityMetadataInResponse,
15
15
  processEntityMetadata
16
16
  } from '@nextsparkjs/core/lib/api/helpers';
17
- import { authenticateRequest } from '@nextsparkjs/core/lib/api/auth/dual-auth';
17
+ import { authenticateRequest, createAuthFailureResponse } from '@nextsparkjs/core/lib/api/auth/dual-auth';
18
18
  import { hasAdminPermission } from '@nextsparkjs/core/lib/api/auth/permissions';
19
19
  import { z } from 'zod';
20
20
  import { withRateLimitTier } from '@nextsparkjs/core/lib/api/rate-limit';
@@ -34,21 +34,19 @@ const createUserSchema = z.object({
34
34
 
35
35
 
36
36
  // Handle CORS preflight
37
- export async function OPTIONS() {
38
- return handleCorsPreflightRequest();
37
+ export async function OPTIONS(request: NextRequest) {
38
+ return handleCorsPreflightRequest(request);
39
39
  }
40
40
 
41
41
  // GET /api/v1/users - List users with dual auth
42
42
  export const GET = withRateLimitTier(withApiLogging(async (req: NextRequest): Promise<NextResponse> => {
43
43
  try {
44
- // Authenticate using dual auth
45
- const authResult = await authenticateRequest(req);
46
-
44
+ // Authenticate using dual auth; the API-key scope is declared at the entry
45
+ // point, which fails closed for keys that lack it (#93).
46
+ const authResult = await authenticateRequest(req, { requiredScope: 'users:read' });
47
+
47
48
  if (!authResult.success) {
48
- return NextResponse.json(
49
- { success: false, error: 'Authentication required', code: 'AUTHENTICATION_FAILED' },
50
- { status: 401 }
51
- );
49
+ return createAuthFailureResponse(authResult);
52
50
  }
53
51
 
54
52
  if (authResult.rateLimitResponse) {
@@ -58,7 +56,7 @@ export const GET = withRateLimitTier(withApiLogging(async (req: NextRequest): Pr
58
56
  // SECURITY: Only superadmins can list all users
59
57
  if (!hasAdminPermission(authResult, 'users:read')) {
60
58
  const response = createApiError('Insufficient permissions. Superadmin access required.', 403);
61
- return addCorsHeaders(response);
59
+ return addCorsHeaders(response, req);
62
60
  }
63
61
 
64
62
  const metaParams = parseMetaParams(req);
@@ -103,25 +101,23 @@ export const GET = withRateLimitTier(withApiLogging(async (req: NextRequest): Pr
103
101
  const usersWithMeta = await includeEntityMetadata('user', users as { id: string }[], metaParams, authResult.user!.id);
104
102
 
105
103
  const response = createApiResponse(usersWithMeta, paginationMeta);
106
- return addCorsHeaders(response);
104
+ return addCorsHeaders(response, req);
107
105
  } catch (error) {
108
106
  console.error('Error fetching users:', error);
109
107
  const response = createApiError('Internal server error', 500);
110
- return addCorsHeaders(response);
108
+ return addCorsHeaders(response, req);
111
109
  }
112
110
  }), 'read');
113
111
 
114
112
  // POST /api/v1/users - Create user with dual auth
115
113
  export const POST = withRateLimitTier(withApiLogging(async (req: NextRequest): Promise<NextResponse> => {
116
114
  try {
117
- // Authenticate using dual auth
118
- const authResult = await authenticateRequest(req);
119
-
115
+ // Authenticate using dual auth; the API-key scope is declared at the entry
116
+ // point, which fails closed for keys that lack it (#93).
117
+ const authResult = await authenticateRequest(req, { requiredScope: 'users:write' });
118
+
120
119
  if (!authResult.success) {
121
- return NextResponse.json(
122
- { success: false, error: 'Authentication required', code: 'AUTHENTICATION_FAILED' },
123
- { status: 401 }
124
- );
120
+ return createAuthFailureResponse(authResult);
125
121
  }
126
122
 
127
123
  if (authResult.rateLimitResponse) {
@@ -131,7 +127,7 @@ export const POST = withRateLimitTier(withApiLogging(async (req: NextRequest): P
131
127
  // SECURITY: Only superadmins can create users
132
128
  if (!hasAdminPermission(authResult, 'users:write')) {
133
129
  const response = createApiError('Insufficient permissions. Superadmin access required.', 403);
134
- return addCorsHeaders(response);
130
+ return addCorsHeaders(response, req);
135
131
  }
136
132
 
137
133
  const body = await req.json();
@@ -147,7 +143,7 @@ export const POST = withRateLimitTier(withApiLogging(async (req: NextRequest): P
147
143
 
148
144
  if (existingUser.length > 0) {
149
145
  const response = createApiError('Email already exists', 409, null, 'EMAIL_EXISTS');
150
- return addCorsHeaders(response);
146
+ return addCorsHeaders(response, req);
151
147
  }
152
148
 
153
149
  const newUserId = globalThis.crypto.randomUUID();
@@ -183,16 +179,16 @@ export const POST = withRateLimitTier(withApiLogging(async (req: NextRequest): P
183
179
  const responseData = await handleEntityMetadataInResponse('user', createdUser as { id: string }, metadataWasProvided, authResult.user!.id);
184
180
 
185
181
  const response = createApiResponse(responseData, { created: true }, 201);
186
- return addCorsHeaders(response);
182
+ return addCorsHeaders(response, req);
187
183
  } catch (error) {
188
184
  if (error instanceof z.ZodError) {
189
185
  const response = createApiError('Validation error', 400, error.issues, 'VALIDATION_ERROR');
190
- return addCorsHeaders(response);
186
+ return addCorsHeaders(response, req);
191
187
  }
192
188
 
193
189
  console.error('Error creating user:', error);
194
190
  const response = createApiError('Internal server error', 500);
195
- return addCorsHeaders(response);
191
+ return addCorsHeaders(response, req);
196
192
  }
197
193
  }), 'write');
198
194
 
@@ -3,7 +3,7 @@
3
3
  import { useAuth } from '@nextsparkjs/core/hooks/useAuth'
4
4
  import { useRouter } from 'next/navigation'
5
5
  import { useEffect, Suspense } from 'react'
6
- import { Loader2 } from 'lucide-react'
6
+ import { DashboardAuthSkeleton } from '@nextsparkjs/core/components/dashboard/layouts/DashboardAuthSkeleton'
7
7
  import { DashboardProviders } from '@nextsparkjs/core/providers/DashboardProviders'
8
8
  import { DashboardTranslationPreloader } from '@nextsparkjs/core/lib/i18n/DashboardTranslationPreloader'
9
9
  import { TranslationDebugger } from '@nextsparkjs/core/utils/dev/TranslationDebugger'
@@ -35,11 +35,7 @@ function DashboardLayoutContent({ children }: { children: React.ReactNode }) {
35
35
  }, [user, isLoading, router])
36
36
 
37
37
  if (isLoading) {
38
- return (
39
- <div className="min-h-screen flex items-center justify-center">
40
- <Loader2 className="h-8 w-8 animate-spin text-muted-foreground" />
41
- </div>
42
- )
38
+ return <DashboardAuthSkeleton />
43
39
  }
44
40
 
45
41
  if (!user) {
@@ -55,27 +55,22 @@ export const metadata: Metadata = getMetadataOrDefault(
55
55
  defaultMetadata
56
56
  )
57
57
 
58
- export default function RootLayout({
58
+ export default async function RootLayout({
59
59
  children,
60
60
  }: Readonly<{
61
61
  children: React.ReactNode
62
62
  }>) {
63
+ const billingHints = await getBillingResourceHints()
64
+
63
65
  return (
64
66
  <html lang={DEFAULT_LOCALE} suppressHydrationWarning>
65
67
  <head>
66
- {(() => {
67
- const hints = getBillingResourceHints()
68
- return (
69
- <>
70
- {hints.preconnect.map((domain) => (
71
- <link key={`pre-${domain}`} rel="preconnect" href={domain} />
72
- ))}
73
- {[...hints.preconnect, ...hints.dnsPrefetch].map((domain) => (
74
- <link key={`dns-${domain}`} rel="dns-prefetch" href={domain} />
75
- ))}
76
- </>
77
- )
78
- })()}
68
+ {billingHints.preconnect.map((domain) => (
69
+ <link key={`pre-${domain}`} rel="preconnect" href={domain} />
70
+ ))}
71
+ {[...billingHints.preconnect, ...billingHints.dnsPrefetch].map((domain) => (
72
+ <link key={`dns-${domain}`} rel="dns-prefetch" href={domain} />
73
+ ))}
79
74
  </head>
80
75
  <body
81
76
  className={`${geistSans.variable} ${geistMono.variable} antialiased`}
@@ -84,7 +79,12 @@ export default function RootLayout({
84
79
  <NextThemeProvider
85
80
  attribute="class"
86
81
  defaultTheme={DEFAULT_THEME_MODE}
87
- enableSystem={DEFAULT_THEME_MODE === 'system'}
82
+ // Always true here: this layout never passes forcedTheme, so
83
+ // ThemeToggle is unconditionally visible and offers "System" as one
84
+ // of its 3 choices — gating on DEFAULT_THEME_MODE === 'system' had
85
+ // the same bug as the non-PPR layout (see #175): a build-time
86
+ // default of 'light'/'dark' silently broke the "System" option.
87
+ enableSystem
88
88
  disableTransitionOnChange
89
89
  >
90
90
  <CustomThemeProvider>
@@ -15,11 +15,15 @@ import { getBillingResourceHints } from "@nextsparkjs/core/lib/billing/gateways/
15
15
  import { ThemeProvider as NextThemeProvider } from "@nextsparkjs/core/providers/theme-provider"
16
16
  import { ThemeProvider as CustomThemeProvider } from "@nextsparkjs/core/lib/theme/ThemeProvider"
17
17
  import { Toaster } from "@nextsparkjs/core/components/ui/sonner"
18
+ import { QueryProvider } from "@nextsparkjs/core/providers/query-provider"
19
+ import { TeamProvider } from "@nextsparkjs/core/contexts/TeamContext"
20
+ import { SubscriptionProvider } from "@nextsparkjs/core/contexts/SubscriptionContext"
18
21
  import { getUserLocale } from '@nextsparkjs/core/lib/locale'
19
22
  import { TranslationContextManager } from "@nextsparkjs/core/providers/TranslationContextManager"
23
+ import { SessionCookieRefresher } from "@nextsparkjs/core/components/auth/SessionCookieRefresher"
20
24
  import { PluginService } from '@nextsparkjs/core/lib/services'
21
25
  import { getMetadataOrDefault } from '@nextsparkjs/core/lib/template-resolver'
22
- import { getDefaultThemeMode } from '@nextsparkjs/core/lib/theme/get-default-theme-mode'
26
+ import { getThemeSettings } from '@nextsparkjs/core/lib/theme/get-default-theme-mode'
23
27
 
24
28
  const geistSans = Geist({
25
29
  variable: "--font-geist-sans",
@@ -55,24 +59,18 @@ export default async function RootLayout({
55
59
 
56
60
  const locale = await getUserLocale()
57
61
  const messages = await getMessages({ locale })
58
- const defaultTheme = await getDefaultThemeMode()
62
+ const { defaultMode, allowUserToggle, forcedThemeRoutes } = await getThemeSettings()
63
+ const billingHints = await getBillingResourceHints()
59
64
 
60
65
  return (
61
66
  <html lang={locale} suppressHydrationWarning>
62
67
  <head>
63
- {(() => {
64
- const hints = getBillingResourceHints()
65
- return (
66
- <>
67
- {hints.preconnect.map((domain) => (
68
- <link key={`pre-${domain}`} rel="preconnect" href={domain} />
69
- ))}
70
- {[...hints.preconnect, ...hints.dnsPrefetch].map((domain) => (
71
- <link key={`dns-${domain}`} rel="dns-prefetch" href={domain} />
72
- ))}
73
- </>
74
- )
75
- })()}
68
+ {billingHints.preconnect.map((domain) => (
69
+ <link key={`pre-${domain}`} rel="preconnect" href={domain} />
70
+ ))}
71
+ {[...billingHints.preconnect, ...billingHints.dnsPrefetch].map((domain) => (
72
+ <link key={`dns-${domain}`} rel="dns-prefetch" href={domain} />
73
+ ))}
76
74
  </head>
77
75
  <body
78
76
  className={`${geistSans.variable} ${geistMono.variable} antialiased`}
@@ -80,14 +78,42 @@ export default async function RootLayout({
80
78
  <NextIntlClientProvider locale={locale} messages={messages}>
81
79
  <NextThemeProvider
82
80
  attribute="class"
83
- defaultTheme={defaultTheme}
84
- enableSystem
81
+ defaultTheme={defaultMode}
82
+ // When allowUserToggle is false, force the theme and ignore localStorage/system
83
+ forcedTheme={!allowUserToggle ? defaultMode : undefined}
84
+ // Detect OS preference whenever the user can toggle at all. ThemeToggle
85
+ // always offers "System" as one of its 3 choices when toggling is
86
+ // allowed, so the provider must be able to resolve it whenever that's
87
+ // true — gating on `defaultMode === 'system'` looks equivalent at
88
+ // first render, but defaultMode is the user's last SAVED preference
89
+ // for a logged-in user (see getThemeSettings), not a static "is
90
+ // system-matching enabled" flag. That made "System" silently write a
91
+ // literal, unmatched `class="system"` for anyone whose saved
92
+ // preference (or the theme's own config default) wasn't already
93
+ // 'system' — see #175.
94
+ enableSystem={allowUserToggle}
95
+ // Force a theme on the routes declared in theme.config.ts (forcedThemeRoutes)
96
+ forcedThemeRoutes={forcedThemeRoutes}
85
97
  disableTransitionOnChange
86
98
  >
87
99
  <CustomThemeProvider>
88
- <TranslationContextManager />
89
- <main>{children}</main>
90
- <Suspense><Toaster position="bottom-left" /></Suspense>
100
+ {/* Team providers live here (not only in DashboardProviders) so the activeTeamId
101
+ cookie is synced from any route, matching layout.ppr.tsx — see #115.
102
+ DashboardProviders skips re-mounting them when already present. */}
103
+ <QueryProvider>
104
+ <TeamProvider>
105
+ <SubscriptionProvider>
106
+ <TranslationContextManager />
107
+ {/* Real session-cookie renewal for installed PWAs: the render-time
108
+ session reads above (getUserLocale/getThemeSettings) cannot
109
+ write cookies, so the rolling refresh is triggered from the
110
+ client through the auth Route Handler instead. */}
111
+ <SessionCookieRefresher />
112
+ <main>{children}</main>
113
+ <Suspense><Toaster position="bottom-left" /></Suspense>
114
+ </SubscriptionProvider>
115
+ </TeamProvider>
116
+ </QueryProvider>
91
117
  </CustomThemeProvider>
92
118
  </NextThemeProvider>
93
119
  </NextIntlClientProvider>
@@ -115,6 +115,25 @@ export const APP_CONFIG_OVERRIDES = {
115
115
  enabled: false,
116
116
  },
117
117
  },
118
+ /**
119
+ * Login methods, in priority order. Core default = passwordless preset:
120
+ * a one-time code by email + Google, no password field (needs an email
121
+ * provider — RESEND_API_KEY — and GOOGLE_CLIENT_ID/SECRET for Google).
122
+ *
123
+ * Uncomment to switch to the classic email + password preset, or list both
124
+ * (the first email method is the form shown first):
125
+ */
126
+ // methods: ['email-password', 'google'],
127
+ // methods: ['email-otp', 'email-password', 'google'],
128
+
129
+ /**
130
+ * Session duration / renewal in seconds (defaults: 7 days, renewed daily).
131
+ * Long-lived sessions for an installed PWA, for example:
132
+ */
133
+ // session: {
134
+ // expiresIn: 60 * 60 * 24 * 90, // 90 days
135
+ // updateAge: 60 * 60 * 24 * 7, // renew weekly
136
+ // },
118
137
  /**
119
138
  * Whether Better Auth automatically sends the verification email on signup.
120
139
  *
@@ -22,6 +22,12 @@ export const starterThemeConfig: ThemeConfig = {
22
22
  // ==========================================
23
23
  plugins: [],
24
24
 
25
+ // ==========================================
26
+ // THEME MODE (Optional)
27
+ // ==========================================
28
+ // defaultMode: 'system', // 'light' | 'dark' | 'system'
29
+ // forcedThemeRoutes: { '/login': 'light' }, // force a theme on route prefixes (longest wins)
30
+
25
31
  // ==========================================
26
32
  // STYLES CONFIGURATION
27
33
  // ==========================================
@@ -200,8 +200,12 @@
200
200
  /* =============================================
201
201
  BASE STYLES
202
202
  ============================================= */
203
- * {
204
- border-color: var(--border);
203
+ /* Must live in @layer base: un-layered rules beat every Tailwind v4 utility
204
+ (border-primary, border-destructive, ...) regardless of specificity. */
205
+ @layer base {
206
+ * {
207
+ border-color: var(--border);
208
+ }
205
209
  }
206
210
 
207
211
  body {
@@ -64,9 +64,48 @@ function isPublicPath(pathname: string): boolean {
64
64
  )
65
65
  }
66
66
 
67
+ /**
68
+ * Identity headers this proxy injects for downstream server code.
69
+ *
70
+ * SECURITY (#87): these headers are trusted by RSC layouts and permission
71
+ * checks, so an inbound value must NEVER reach the app. They are stripped from
72
+ * every request before any path branching and re-added only from the verified
73
+ * session. Gating the strip on protected prefixes is not enough: Next.js
74
+ * dispatches Server Actions by the `Next-Action` header, not by the URL, so an
75
+ * action can be POSTed to a public path with a forged `x-user-id`.
76
+ */
77
+ const TRUSTED_IDENTITY_HEADERS = ['x-user-id', 'x-user-email', 'x-pathname'] as const
78
+
79
+ /**
80
+ * Build the request headers forwarded to the app: inbound copy minus every
81
+ * trusted identity header, plus the real pathname.
82
+ */
83
+ function sanitizeRequestHeaders(request: NextRequest): Headers {
84
+ const headers = new Headers(request.headers)
85
+ for (const name of TRUSTED_IDENTITY_HEADERS) {
86
+ headers.delete(name)
87
+ }
88
+ headers.set('x-pathname', request.nextUrl.pathname)
89
+ return headers
90
+ }
91
+
92
+ /**
93
+ * Continue to the app with the (sanitized) request headers.
94
+ * Every pass-through in this proxy MUST go through here so the strip applies
95
+ * to public paths, /api/v1 and unmatched routes alike.
96
+ */
97
+ function passThrough(requestHeaders: Headers): NextResponse {
98
+ return NextResponse.next({
99
+ request: { headers: requestHeaders },
100
+ })
101
+ }
102
+
67
103
  export async function proxy(request: NextRequest) {
68
104
  const { pathname } = request.nextUrl
69
105
 
106
+ // 0. Strip forgeable identity headers before ANY branching (see #87)
107
+ const requestHeaders = sanitizeRequestHeaders(request)
108
+
70
109
  // 1. Check for theme middleware override
71
110
  const activeTheme = process.env.NEXT_PUBLIC_ACTIVE_THEME
72
111
  if (activeTheme && hasThemeMiddleware(activeTheme)) {
@@ -114,17 +153,17 @@ export async function proxy(request: NextRequest) {
114
153
  return NextResponse.redirect(loginUrl)
115
154
  }
116
155
  }
117
- return NextResponse.next()
156
+ return passThrough(requestHeaders)
118
157
  }
119
158
 
120
159
  // 4. Allow public paths
121
160
  if (isPublicPath(pathname)) {
122
- return NextResponse.next()
161
+ return passThrough(requestHeaders)
123
162
  }
124
163
 
125
164
  // 5. API v1 routes handle their own dual authentication
126
165
  if (pathname.startsWith('/api/v1')) {
127
- return NextResponse.next()
166
+ return passThrough(requestHeaders)
128
167
  }
129
168
 
130
169
  // 6. Protected routes - require authentication and inject user headers
@@ -162,20 +201,17 @@ export async function proxy(request: NextRequest) {
162
201
  }
163
202
  }
164
203
 
165
- // Inject user headers for downstream use
204
+ // Inject user headers for downstream use, ONLY from the verified session
205
+ // (requestHeaders already had any inbound values stripped).
166
206
  // IMPORTANT: EntityPermissionLayout depends on these headers
167
- const requestHeaders = new Headers(request.headers)
168
207
  if (session.user?.id) {
169
208
  requestHeaders.set('x-user-id', session.user.id)
170
209
  }
171
210
  if (session.user?.email) {
172
211
  requestHeaders.set('x-user-email', session.user.email)
173
212
  }
174
- requestHeaders.set('x-pathname', pathname)
175
213
 
176
- return NextResponse.next({
177
- request: { headers: requestHeaders },
178
- })
214
+ return passThrough(requestHeaders)
179
215
  } catch (error) {
180
216
  console.error('Proxy error:', error)
181
217
  const loginUrl = new URL('/login', request.url)
@@ -184,7 +220,7 @@ export async function proxy(request: NextRequest) {
184
220
  }
185
221
  }
186
222
 
187
- return NextResponse.next()
223
+ return passThrough(requestHeaders)
188
224
  }
189
225
 
190
226
  export const config = {
@@ -56,6 +56,10 @@
56
56
  ],
57
57
  "exclude": [
58
58
  "node_modules",
59
+ // PPR template variants (layout.ppr.tsx) are copied over layout.tsx when a project
60
+ // enables cacheComponents; in place they import registry exports that only exist
61
+ // in PPR mode, so they are never type-checked as-is.
62
+ "**/*.ppr.tsx",
59
63
  "**/*.test.ts",
60
64
  "**/*.test.tsx",
61
65
  "**/*.spec.ts",
@@ -17,6 +17,15 @@ export interface ThemeConfig {
17
17
  enabled?: boolean;
18
18
  /** Default theme mode - controls next-themes behavior */
19
19
  defaultMode?: 'light' | 'dark' | 'system';
20
+ /**
21
+ * Force a theme on specific routes, ignoring user and system preference.
22
+ * Keys are route prefixes matched at segment boundaries (the longest match
23
+ * wins); values are the theme to force. The root ThemeProvider passes the
24
+ * resolved value to next-themes' `forcedTheme`, so the route renders with
25
+ * that theme from the first paint and on client-side navigations.
26
+ * @example { '/login': 'light', '/signup': 'light', '/embed': 'dark' }
27
+ */
28
+ forcedThemeRoutes?: Record<string, 'light' | 'dark'>;
20
29
  dependencies?: string[];
21
30
  plugins?: string[];
22
31
  styles?: {
@@ -1 +1 @@
1
- {"version":3,"file":"theme.d.ts","sourceRoot":"","sources":["../../src/types/theme.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAE9B;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B,IAAI,EAAE,MAAM,CAAA;IACZ,WAAW,EAAE,MAAM,CAAA;IACnB,OAAO,EAAE,MAAM,CAAA;IACf,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,OAAO,CAAC,EAAE,OAAO,CAAA;IACjB,yDAAyD;IACzD,WAAW,CAAC,EAAE,OAAO,GAAG,MAAM,GAAG,QAAQ,CAAA;IACzC,YAAY,CAAC,EAAE,MAAM,EAAE,CAAA;IACvB,OAAO,CAAC,EAAE,MAAM,EAAE,CAAA;IAClB,MAAM,CAAC,EAAE;QACP,OAAO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EXT PRIMARY KEY DEFAULT gen_random_uuid()::text,
6
- "apiKeyId" TEXT NOT NULL REFERENCES public."api_key"(id) ON DELETE CASCADE,
6
+ -- NULL for session-authenticated requests (see 025_api_audit_log_nullable_api_key.sql)
7
+ "apiKeyId" TEXT REFERENCES public."api_key"(id) ON DELETE CASCADE,
7
8
  "userId" TEXT NOT NULL REFERENCES public."users"(id) ON DELETE CASCADE,
8
9
  endpoint TEXT NOT NULL,
9
10
  method TEXT NOT NULL,
@@ -0,0 +1,12 @@
1
+ -- Migration: 025_api_audit_log_nullable_api_key.sql
2
+ -- Description: Allow api_audit_log rows without an API key (#105)
3
+ --
4
+ -- The generic entity routes (/api/v1/[entity], /api/v1/[entity]/[id]) now
5
+ -- write an audit row for every authenticated request, whether it was
6
+ -- authenticated with an API key or with a browser session. Session requests
7
+ -- have no key, so "apiKeyId" must accept NULL. The foreign key stays as-is:
8
+ -- a FK only constrains non-null values.
9
+ --
10
+ -- Idempotent: DROP NOT NULL on an already-nullable column is a no-op.
11
+
12
+ ALTER TABLE "api_audit_log" ALTER COLUMN "apiKeyId" DROP NOT NULL;
@@ -0,0 +1,91 @@
1
+ -- Migration: 026_login_events.sql
2
+ -- Description: login_events — per-user device/login history for built-in
3
+ -- security email notifications (new-device login alerts).
4
+ -- Date: 2026-09-01
5
+ -- Issue: #75
6
+ --
7
+ -- Why 026 (and not 022 as the issue text proposed, nor 025):
8
+ -- The issue was written when the migration sequence ended earlier. By the
9
+ -- time this branch was authored the sequence already went through
10
+ -- 024_teams_soft_delete.sql (022 and 023 are taken by rls_runtime_roles and
11
+ -- the invitations index), so this migration was originally 025. It was
12
+ -- renumbered to 026 when merging to main, because 025 had in the meantime
13
+ -- been taken by 025_api_audit_log_nullable_api_key.sql (issue #105).
14
+ --
15
+ -- What this backs:
16
+ -- The security-notifications dispatcher records one row per successful sign-in
17
+ -- with a device fingerprint = sha256(normalizedUA | ip/24). A sign-in whose
18
+ -- (userId, deviceFingerprint) pair has never been seen is flagged isNew=true
19
+ -- and triggers a "new device" email via the auth:security-notification
20
+ -- scheduled action. Known fingerprints are still logged (isNew=false) but send
21
+ -- no email. The table is also the substrate for a future login-history UI
22
+ -- (out of scope here — see issue #75 "Out of scope").
23
+ --
24
+ -- Append-only: rows are never updated, so there is no "updatedAt"/set_updated_at
25
+ -- trigger (unlike mutable entity tables). city/country are nullable for a future
26
+ -- geolocation enhancement (also out of scope).
27
+
28
+ -- ============================================
29
+ -- TABLE
30
+ -- ============================================
31
+ DROP TABLE IF EXISTS public."login_events" CASCADE;
32
+
33
+ CREATE TABLE IF NOT EXISTS public."login_events" (
34
+ -- Primary Key (TEXT not UUID — Better Auth convention)
35
+ id TEXT PRIMARY KEY DEFAULT gen_random_uuid()::text,
36
+
37
+ -- Relational Fields
38
+ "userId" TEXT NOT NULL REFERENCES public."users"(id) ON DELETE CASCADE,
39
+
40
+ -- Entity-specific fields
41
+ "ipAddress" TEXT,
42
+ "userAgent" TEXT,
43
+ "deviceFingerprint" TEXT NOT NULL,
44
+ city TEXT,
45
+ country TEXT,
46
+ "isNew" BOOLEAN NOT NULL DEFAULT false,
47
+
48
+ -- System fields (append-only: created only, never updated)
49
+ "createdAt" TIMESTAMPTZ NOT NULL DEFAULT now()
50
+ );
51
+
52
+ COMMENT ON TABLE public."login_events" IS 'Per-user login/device history backing built-in new-device security email notifications (issue #75).';
53
+ COMMENT ON COLUMN public."login_events"."userId" IS 'Owner of the login event.';
54
+ COMMENT ON COLUMN public."login_events"."ipAddress" IS 'Best-effort client IP captured at sign-in (may be a proxy address).';
55
+ COMMENT ON COLUMN public."login_events"."userAgent" IS 'Raw User-Agent header captured at sign-in.';
56
+ COMMENT ON COLUMN public."login_events"."deviceFingerprint" IS 'sha256(normalizedUA | ip/24). Stable across patch-version UA churn and within a /24 network so routine re-logins from the same device do not re-alert.';
57
+ COMMENT ON COLUMN public."login_events".city IS 'Reserved for future geolocation enrichment (nullable, unused today).';
58
+ COMMENT ON COLUMN public."login_events".country IS 'Reserved for future geolocation enrichment (nullable, unused today).';
59
+ COMMENT ON COLUMN public."login_events"."isNew" IS 'True when this (userId, deviceFingerprint) pair had not been seen before this row → a new-device email was queued.';
60
+
61
+ -- ============================================
62
+ -- INDEXES
63
+ -- ============================================
64
+ -- Most-recent-first history lookups per user (login-history UI, TTL checks).
65
+ CREATE INDEX IF NOT EXISTS idx_login_events_user_created
66
+ ON public."login_events" ("userId", "createdAt" DESC);
67
+
68
+ -- "Have we seen this fingerprint for this user?" — the new-device decision.
69
+ CREATE INDEX IF NOT EXISTS idx_login_events_user_fingerprint
70
+ ON public."login_events" ("userId", "deviceFingerprint");
71
+
72
+ -- ============================================
73
+ -- RLS
74
+ -- ============================================
75
+ -- A user may read ONLY their own login events; superadmin/developer bypass via
76
+ -- can_bypass_rls() (defined in 001). Writes are service-only: the dispatcher
77
+ -- inserts with no user GUC (service/bypass pool), matching how session/account
78
+ -- rows are written in 002. Mirrors the "self read + service write" pattern.
79
+ ALTER TABLE public."login_events" ENABLE ROW LEVEL SECURITY;
80
+
81
+ DROP POLICY IF EXISTS "Login events self read" ON public."login_events";
82
+ DROP POLICY IF EXISTS "Login events service write" ON public."login_events";
83
+
84
+ CREATE POLICY "Login events self read" ON public."login_events"
85
+ FOR SELECT TO authenticated
86
+ USING (public.can_bypass_rls() OR "userId" = public.get_auth_user_id());
87
+
88
+ CREATE POLICY "Login events service write" ON public."login_events"
89
+ FOR ALL TO authenticated
90
+ USING (public.can_bypass_rls())
91
+ WITH CHECK (public.can_bypass_rls());