create-nextblock 0.11.2 → 0.12.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 (52) hide show
  1. package/package.json +1 -1
  2. package/templates/nextblock-template/app/(auth-pages)/sign-up/SignUpForm.tsx +141 -0
  3. package/templates/nextblock-template/app/(auth-pages)/sign-up/page.tsx +40 -122
  4. package/templates/nextblock-template/app/(auth-pages)/two-factor/components/TwoFactorForm.tsx +5 -0
  5. package/templates/nextblock-template/app/[slug]/page.tsx +5 -2
  6. package/templates/nextblock-template/app/[slug]/page.utils.ts +10 -9
  7. package/templates/nextblock-template/app/actions/email.ts +8 -1
  8. package/templates/nextblock-template/app/actions/feedback.ts +1 -0
  9. package/templates/nextblock-template/app/actions/formActions.ts +20 -97
  10. package/templates/nextblock-template/app/actions/interactions.ts +3 -2
  11. package/templates/nextblock-template/app/actions/twoFactorEmail.ts +3 -2
  12. package/templates/nextblock-template/app/actions/visualEditingActions.test.ts +1 -1
  13. package/templates/nextblock-template/app/actions/visualEditingActions.ts +2 -0
  14. package/templates/nextblock-template/app/actions.ts +14 -0
  15. package/templates/nextblock-template/app/api/brand/email-logo/route.ts +48 -0
  16. package/templates/nextblock-template/app/api/cron/reset-sandbox/route.ts +47 -1
  17. package/templates/nextblock-template/app/api/cron/reset-sandbox/sandboxResetSql.ts +4970 -8566
  18. package/templates/nextblock-template/app/article/[slug]/page.tsx +5 -2
  19. package/templates/nextblock-template/app/article/[slug]/page.utils.ts +1 -0
  20. package/templates/nextblock-template/app/cms/pages/actions.ts +22 -18
  21. package/templates/nextblock-template/app/cms/pages/components/PageForm.tsx +20 -2
  22. package/templates/nextblock-template/app/cms/posts/actions.ts +5 -0
  23. package/templates/nextblock-template/app/cms/posts/components/PostForm.tsx +137 -133
  24. package/templates/nextblock-template/app/cms/settings/copyright/actions.ts +36 -0
  25. package/templates/nextblock-template/app/cms/settings/copyright/components/CopyrightForm.tsx +26 -1
  26. package/templates/nextblock-template/app/cms/settings/copyright/page.tsx +6 -2
  27. package/templates/nextblock-template/app/cms/settings/email/actions.ts +7 -3
  28. package/templates/nextblock-template/app/cms/settings/logos/actions.ts +29 -14
  29. package/templates/nextblock-template/app/cms/settings/logos/components/SetActiveLogoButton.tsx +42 -0
  30. package/templates/nextblock-template/app/cms/settings/logos/page.tsx +18 -5
  31. package/templates/nextblock-template/app/layout.tsx +29 -17
  32. package/templates/nextblock-template/app/lib/seo.test.ts +36 -0
  33. package/templates/nextblock-template/app/lib/seo.ts +32 -0
  34. package/templates/nextblock-template/app/product/[slug]/page.tsx +5 -2
  35. package/templates/nextblock-template/components/AppShell.tsx +16 -1
  36. package/templates/nextblock-template/components/auth/AuthBotProtection.tsx +182 -0
  37. package/templates/nextblock-template/docs/04-DATABASE-AND-AUTH.md +36 -42
  38. package/templates/nextblock-template/docs/13-STAYING-UP-TO-DATE.md +7 -0
  39. package/templates/nextblock-template/lib/botProtection/verify.ts +134 -0
  40. package/templates/nextblock-template/lib/email/branding-format.test.ts +133 -0
  41. package/templates/nextblock-template/lib/email/branding-format.ts +123 -0
  42. package/templates/nextblock-template/lib/email/branding.ts +76 -0
  43. package/templates/nextblock-template/lib/logos/active-logo.ts +53 -0
  44. package/templates/nextblock-template/lib/media/resolveMediaUrl.ts +1 -0
  45. package/templates/nextblock-template/lib/setup/migrations-bundle.ts +8 -203
  46. package/templates/nextblock-template/lib/visual-editing/draft-content.ts +4 -2
  47. package/templates/nextblock-template/lib/visual-editing/mutations.ts +2 -2
  48. package/templates/nextblock-template/lib/visual-editing/product-drafts.ts +1 -0
  49. package/templates/nextblock-template/package.json +1 -1
  50. package/templates/nextblock-template/proxy.ts +16 -0
  51. package/templates/nextblock-template/public/images/nextblock-logo-button-tiny.png +0 -0
  52. package/templates/nextblock-template/tsconfig.tsbuildinfo +1 -1
@@ -0,0 +1,123 @@
1
+ // Pure, dependency-free rendering of the transactional-email brand header. Kept separate
2
+ // from branding.ts (which is `server-only` and hits the DB) so this string logic — the
3
+ // part worth unit-testing — imports nothing server-side and runs anywhere.
4
+
5
+ // Tenant branding used to white-label every transactional email.
6
+ // - logoUrl — an ABSOLUTE, email-safe logo URL, or `null` when none is configured.
7
+ // - siteName — the site name, used as the logo `alt` text and the no-logo text banner.
8
+ export interface EmailBranding {
9
+ logoUrl: string | null;
10
+ siteName: string;
11
+ }
12
+
13
+ /**
14
+ * Marker an app-rendered email template drops where the brand header belongs.
15
+ * `applyEmailBranding` swaps it for the resolved logo (or text banner). This is a plain
16
+ * literal — NOT a Supabase/GoTrue `{{ .Var }}` merge tag — because it is substituted by
17
+ * our own nodemailer pipeline, never by Supabase.
18
+ */
19
+ export const EMAIL_BRAND_HEADER_TOKEN = '{{brand_header}}';
20
+
21
+ /** Hard width cap so an oversized custom logo can't blow out Gmail/Outlook layouts. */
22
+ export const EMAIL_LOGO_MAX_WIDTH = 150;
23
+
24
+ // The pre-white-label hardcoded logo. The app templates no longer embed it, but any stray
25
+ // occurrence in an outbound email is swapped for the tenant header defensively.
26
+ const LEGACY_LOGO_IMG_RE =
27
+ /<img\b[^>]*\bsrc=["'][^"']*nextblock-logo-small\.webp["'][^>]*>/gi;
28
+
29
+ function escapeHtml(value: string): string {
30
+ return value
31
+ .replace(/&/g, '&amp;')
32
+ .replace(/</g, '&lt;')
33
+ .replace(/>/g, '&gt;')
34
+ .replace(/"/g, '&quot;')
35
+ .replace(/'/g, '&#39;');
36
+ }
37
+
38
+ /**
39
+ * Render the branded header block: an email-safe logo `<img>` (hard-capped at 150px so a
40
+ * large custom logo can't break the layout) when a logo exists, otherwise a clean, styled
41
+ * text banner showing the site name.
42
+ */
43
+ export function renderEmailBrandHeader(branding: EmailBranding): string {
44
+ const name = escapeHtml(branding.siteName);
45
+
46
+ if (branding.logoUrl) {
47
+ const src = escapeHtml(branding.logoUrl);
48
+ return (
49
+ '<div style="text-align:center;padding:0 0 24px;">' +
50
+ `<img src="${src}" alt="${name}" width="${EMAIL_LOGO_MAX_WIDTH}" ` +
51
+ `style="display:block;margin:0 auto;width:${EMAIL_LOGO_MAX_WIDTH}px;` +
52
+ 'max-width:100%;height:auto;border:0;outline:none;text-decoration:none;" />' +
53
+ '</div>'
54
+ );
55
+ }
56
+
57
+ // No logo → strip the image entirely and fall back to a text banner with the site name.
58
+ return (
59
+ '<div style="text-align:center;padding:0 0 24px;">' +
60
+ '<span style="display:inline-block;font-family:-apple-system,BlinkMacSystemFont,' +
61
+ "'Segoe UI',Roboto,Helvetica,Arial,sans-serif;font-size:22px;line-height:1.2;" +
62
+ `font-weight:700;color:#0f172a;letter-spacing:-0.01em;">${name}</span>` +
63
+ '</div>'
64
+ );
65
+ }
66
+
67
+ /**
68
+ * Intercept a fully-rendered email body and apply tenant branding:
69
+ * 1. Replace the `{{brand_header}}` token (used by our app-rendered templates).
70
+ * 2. Defensively swap any stray hardcoded legacy NextBlock logo `<img>` for the header.
71
+ * Purely a string transform so it is trivially unit-testable and side-effect free.
72
+ */
73
+ export function applyEmailBranding(html: string, branding: EmailBranding): string {
74
+ const header = renderEmailBrandHeader(branding);
75
+ let out = html;
76
+ if (out.includes(EMAIL_BRAND_HEADER_TOKEN)) {
77
+ out = out.split(EMAIL_BRAND_HEADER_TOKEN).join(header);
78
+ }
79
+ out = out.replace(LEGACY_LOGO_IMG_RE, header);
80
+ return out;
81
+ }
82
+
83
+ // The `variants` JSONB the upload pipeline writes for a media row (camelCase keys).
84
+ interface MediaVariant {
85
+ objectKey?: string | null;
86
+ variantLabel?: string | null;
87
+ fileType?: string | null;
88
+ }
89
+
90
+ // The subset of a media row the email logo resolver needs.
91
+ export interface LogoMediaLike {
92
+ object_key?: string | null;
93
+ file_path?: string | null;
94
+ variants?: unknown;
95
+ }
96
+
97
+ /** Label the upload pipeline gives the untouched original file among a media row's variants. */
98
+ export const ORIGINAL_UPLOAD_VARIANT_LABEL = 'original_uploaded';
99
+
100
+ /**
101
+ * Pick the storage key to use for a logo IN EMAIL.
102
+ *
103
+ * The image pipeline stores the AVIF derivative as `object_key` — that's what the website
104
+ * (navbar, blocks) renders for performance — and keeps the ORIGINAL uploaded file among
105
+ * `variants` under the label `original_uploaded`. Email clients, Outlook especially, can't
106
+ * render AVIF (or WebP), so for email we prefer that untouched original. Fall back to
107
+ * `object_key`/`file_path` only when no original variant was kept (e.g. the seeded default
108
+ * logo, which has no variants). This keeps the site on AVIF while email uses the original.
109
+ */
110
+ export function pickEmailLogoObjectKey(media: LogoMediaLike | null | undefined): string | null {
111
+ if (!media) return null;
112
+ const variants = Array.isArray(media.variants) ? (media.variants as MediaVariant[]) : [];
113
+ const original = variants.find(
114
+ (v) =>
115
+ v &&
116
+ typeof v === 'object' &&
117
+ v.variantLabel === ORIGINAL_UPLOAD_VARIANT_LABEL &&
118
+ typeof v.objectKey === 'string' &&
119
+ v.objectKey.length > 0,
120
+ );
121
+ if (original?.objectKey) return original.objectKey;
122
+ return media.object_key ?? media.file_path ?? null;
123
+ }
@@ -0,0 +1,76 @@
1
+ import 'server-only';
2
+
3
+ import { getServiceRoleSupabaseClient } from '@nextblock-cms/db/server';
4
+
5
+ import { getSiteSettings } from '../../app/lib/site-settings';
6
+ import { DEFAULT_SITE_TITLE } from '../../app/lib/seo';
7
+ import { resolveMediaUrl } from '../media/resolveMediaUrl';
8
+ import { resolveMediaBaseUrl } from '../storage/provider';
9
+ import { resolveSiteUrl } from '../site-url';
10
+ import { resolveActiveLogo } from '../logos/active-logo';
11
+ import { pickEmailLogoObjectKey } from './branding-format';
12
+ import type { EmailBranding, LogoMediaLike } from './branding-format';
13
+
14
+ // Server-side resolution of the tenant branding that white-labels every transactional
15
+ // email. The mailer choke point (app/actions/email.ts) and the auth-email logo endpoint
16
+ // (app/api/brand/email-logo) both read this, so every dispatched pipeline — 2FA codes,
17
+ // form/interaction notifications, the SMTP test, feedback, and the Supabase auth emails —
18
+ // shows the operator's own logo instead of a hardcoded NextBlock one.
19
+ //
20
+ // The pure header-rendering helpers live in ./branding-format (no server-only imports, so
21
+ // they're unit-testable); re-export them here so callers have a single import surface.
22
+ export {
23
+ EMAIL_BRAND_HEADER_TOKEN,
24
+ EMAIL_LOGO_MAX_WIDTH,
25
+ applyEmailBranding,
26
+ renderEmailBrandHeader,
27
+ } from './branding-format';
28
+ export type { EmailBranding } from './branding-format';
29
+
30
+ /**
31
+ * Resolve the tenant's active logo to an ABSOLUTE URL usable in an email `<img>`.
32
+ * The newest `logos` row wins (the table has no is_active flag). Returns `null` when
33
+ * there is no logo, no linked media, or the URL cannot be resolved — the caller then
34
+ * renders a text banner instead.
35
+ */
36
+ async function resolveActiveLogoUrl(): Promise<string | null> {
37
+ try {
38
+ const supabase = getServiceRoleSupabaseClient();
39
+ // Honor the admin-pinned active logo (site_settings.active_logo_id), else newest.
40
+ const logo = await resolveActiveLogo(supabase);
41
+ const media = (logo as { media?: LogoMediaLike | null } | null)?.media ?? null;
42
+ // Prefer the original uploaded file over the AVIF `object_key` (email can't render AVIF).
43
+ const objectKey = pickEmailLogoObjectKey(media);
44
+ if (!objectKey) return null;
45
+
46
+ // Pass the resolved base explicitly so the native-Supabase storage URL is honored
47
+ // even when NEXT_PUBLIC_R2_BASE_URL isn't set.
48
+ const resolved = resolveMediaUrl(objectKey, resolveMediaBaseUrl());
49
+ if (!resolved) return null;
50
+ if (resolved.startsWith('http://') || resolved.startsWith('https://')) {
51
+ return resolved;
52
+ }
53
+ // Relative result (a bundled `/images/*` key, or the no-storage-base case): make it
54
+ // absolute against the public site origin so email clients can fetch it.
55
+ const origin = resolveSiteUrl();
56
+ return `${origin}${resolved.startsWith('/') ? '' : '/'}${resolved}`;
57
+ } catch {
58
+ // Service role unavailable or table missing (pre-setup) — treat as "no logo".
59
+ return null;
60
+ }
61
+ }
62
+
63
+ /**
64
+ * Resolve tenant email branding. Read fresh on each call (no React `cache`, so it is safe
65
+ * from fire-and-forget dispatch paths where the request scope may already be torn down);
66
+ * the underlying site-settings read is already cross-request cached via `unstable_cache`,
67
+ * and a fresh read each time is what keeps the logo "change at will".
68
+ */
69
+ export async function resolveEmailBranding(): Promise<EmailBranding> {
70
+ const [settings, logoUrl] = await Promise.all([
71
+ getSiteSettings().catch(() => null),
72
+ resolveActiveLogoUrl(),
73
+ ]);
74
+ const siteName = settings?.siteTitle?.trim() || DEFAULT_SITE_TITLE;
75
+ return { logoUrl, siteName };
76
+ }
@@ -0,0 +1,53 @@
1
+ import type { SupabaseClient } from '@supabase/supabase-js';
2
+ import type { Database } from '@nextblock-cms/db';
3
+
4
+ // The single, shared definition of "the active logo", used by the public header, the CMS
5
+ // branding page, transactional-email branding, and invoices. The operator can pin a
6
+ // specific logo (stored as `site_settings.active_logo_id`); when none is pinned we fall
7
+ // back to the most recently created logo (the historical default). The setting key is a
8
+ // plain, non-secret `site_settings` row, so it is publicly readable under RLS (the public
9
+ // header can read it with the anon client) and writable by ADMIN/WRITER.
10
+
11
+ /** site_settings key holding the admin-selected active logo id (a `logos.id` uuid string). */
12
+ export const ACTIVE_LOGO_SETTING_KEY = 'active_logo_id';
13
+
14
+ // Accept any of the app's Supabase clients (request-scoped, static/anon, or service-role).
15
+ type AnyClient = SupabaseClient<Database>;
16
+
17
+ /** The admin-pinned active logo id, or null when none has been chosen. */
18
+ export async function resolveActiveLogoId(client: AnyClient): Promise<string | null> {
19
+ const { data } = await client
20
+ .from('site_settings')
21
+ .select('value')
22
+ .eq('key', ACTIVE_LOGO_SETTING_KEY)
23
+ .maybeSingle();
24
+ const value = data?.value;
25
+ return typeof value === 'string' && value.length > 0 ? value : null;
26
+ }
27
+
28
+ /**
29
+ * Resolve the active logo row (joined with its media) for the given client: the pinned
30
+ * logo when set and still present, otherwise the most recently created logo. Returns the
31
+ * raw row; callers cast to their local logo type. Returns null when there are no logos.
32
+ */
33
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
34
+ export async function resolveActiveLogo(client: AnyClient): Promise<any> {
35
+ const activeId = await resolveActiveLogoId(client);
36
+
37
+ if (activeId) {
38
+ const { data } = await client
39
+ .from('logos')
40
+ .select('*, media:media_id (*)')
41
+ .eq('id', activeId)
42
+ .maybeSingle();
43
+ if (data) return data;
44
+ }
45
+
46
+ const { data } = await client
47
+ .from('logos')
48
+ .select('*, media:media_id (*)')
49
+ .order('created_at', { ascending: false })
50
+ .limit(1)
51
+ .maybeSingle();
52
+ return data ?? null;
53
+ }
@@ -12,6 +12,7 @@ const BUNDLED_PUBLIC_MEDIA_KEYS = new Set([
12
12
  'images/included.webp',
13
13
  'images/metadata_image.webp',
14
14
  'images/nextblock-logo-small.webp',
15
+ 'images/nextblock-logo-button-tiny.png',
15
16
  'images/nx-graph.webp',
16
17
  'images/pants.webp',
17
18
  'images/programmer-upscaled.webp',