Files
superbrain/web/app/unsubscribe/client.tsx
T
Andrew S Erwin 7a7e77e039
release gate / check (push) Failing after 6m56s
Branded email template + footer pages: help center, token unsubscribe, settings deep links
Transactional mail now goes out as the Superbrain HTML design
(internal/mailer/template.html, embedded verbatim) with a plain-text
alternative: multipart/alternative (plain first per RFC 2046), random
boundary, CRLF normalization and SMTP dot-stuffing. Render() fills the
{{placeholders}} with HTML-escaped values and removes optional sections
(CTA + fallback link, note block, footer links, address) when empty;
anchor drift fails the unit tests and degrades to plain text at send
time, never blocking delivery. Password reset gets the headline,
first-name greeting, "Choose a new password" button (Outlook VML
fallback) and expiry/single-use note; notification and agent emails
render title-as-headline with body paragraphs.

The template's footer links are now real pages:
- /help: public help center (10 topic sections + FAQ accordion), linked
  from the login page.
- /unsubscribe?token=...: public confirmation page; the HMAC-signed user
  id (notify.UnsubscribeToken, domain-separated, stable, no stored row)
  IS the credential. POST /api/notifications/unsubscribe accepts the
  RFC 8058 one-click shape (token in query, form body ignored) and the
  browser JSON shape; it disables the user's enabled email channels and
  pauses enabled agent email tasks addressed to their own email
  (idempotent, re-enableable, per-client rate limited, audit logged).
- Email preferences deep-links to /settings?tab=notifications; Settings
  now honors ?tab=<section> (unknown values fall back to profile).

notify.FooterLinks is the single eligibility point: preferences + help
whenever NEXTAUTH_URL is set; unsubscribe ONLY when the recipient is the
owner's own account email — third-party channel addresses and agent
recipients never receive a link that changes someone else's settings,
and transactional mail (password reset) never carries unsubscribe.
Owner-addressed mail also carries List-Unsubscribe +
List-Unsubscribe-Post headers.

Verified: Go build/vet/unit green; new TestUnsubscribeFlow integration
test pins footer + headers, blast radius (slack channel and third-party
task survive), one-click + JSON paths, idempotence, tampered tokens and
the third-party/transactional exclusions; notifications, phase-2,
password-reset and agent suites green; web lint/SSR-lint/build 0 errors;
full browser regressions pass; live smoke of /help, /unsubscribe states,
the settings deep-link auth gate and bad-token 400 through the proxy.
2026-11-20 10:00:03 -06:00

128 lines
5.3 KiB
TypeScript

'use client'
import { useState } from 'react'
import { useSearchParams } from 'next/navigation'
import { Button } from '@/components/ui/button'
import { BellOff, Check, Link2 } from 'lucide-react'
import Link from 'next/link'
import { toast } from 'sonner'
// The token in the link IS the credential (HMAC-signed user id, issued only
// in mail to the owner's own address). The page never needs a session; the
// Go endpoint validates the token and disables email notification channels
// plus self-addressed agent email tasks. Transactional mail (password
// resets) keeps flowing — the copy must say so.
export function UnsubscribeClient() {
const params = useSearchParams()
const token = params.get('token') ?? ''
const [loading, setLoading] = useState(false)
const [done, setDone] = useState(false)
const [invalid, setInvalid] = useState(false)
const handleUnsubscribe = async () => {
setLoading(true)
try {
const res = await fetch('/api/notifications/unsubscribe', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ token }),
})
const data = await res.json().catch(() => null)
if (res.ok && data?.ok) {
setDone(true)
} else if (res.status === 400) {
setInvalid(true)
} else {
toast.error(data?.error ?? 'Could not unsubscribe right now')
}
} catch {
toast.error('Could not unsubscribe right now')
}
setLoading(false)
}
if (!token || invalid) {
return (
<div className="min-h-screen flex items-center justify-center bg-background p-4">
<div className="w-full max-w-sm space-y-8 text-center">
<div className="flex justify-center mb-4">
<div className="flex h-14 w-14 items-center justify-center rounded-2xl bg-primary text-primary-foreground">
<Link2 className="h-7 w-7" />
</div>
</div>
<h1 className="font-display text-3xl font-bold tracking-tight">Invalid Link</h1>
<p className="text-sm text-muted-foreground mt-2">
This unsubscribe link is invalid or has expired. Open Settings → Notifications
in the app to manage your email preferences directly.
</p>
<p className="text-center text-xs text-muted-foreground">
<Link href="/settings?tab=notifications" className="text-primary hover:underline">Email preferences</Link>
{' · '}
<Link href="/help" className="text-primary hover:underline">Help center</Link>
{' · '}
<Link href="/login" className="text-primary hover:underline">Back to sign in</Link>
</p>
</div>
</div>
)
}
if (done) {
return (
<div className="min-h-screen flex items-center justify-center bg-background p-4">
<div className="w-full max-w-sm space-y-8 text-center">
<div className="flex justify-center mb-4">
<div className="flex h-14 w-14 items-center justify-center rounded-2xl bg-primary text-primary-foreground">
<Check className="h-7 w-7" />
</div>
</div>
<h1 className="font-display text-3xl font-bold tracking-tight">You&apos;re Unsubscribed</h1>
<p className="text-sm text-muted-foreground mt-2">
Notification emails and automations that email you have been turned off.
You can re-enable them anytime in Settings → Notifications. Account emails
such as password resets are not affected.
</p>
<p className="text-center text-xs text-muted-foreground">
<Link href="/settings?tab=notifications" className="text-primary hover:underline">Email preferences</Link>
{' · '}
<Link href="/help" className="text-primary hover:underline">Help center</Link>
{' · '}
<Link href="/login" className="text-primary hover:underline">Back to sign in</Link>
</p>
</div>
</div>
)
}
return (
<div className="min-h-screen flex items-center justify-center bg-background p-4">
<div className="w-full max-w-sm space-y-8">
<div className="text-center">
<div className="flex justify-center mb-4">
<div className="flex h-14 w-14 items-center justify-center rounded-2xl bg-primary text-primary-foreground">
<BellOff className="h-7 w-7" />
</div>
</div>
<h1 className="font-display text-3xl font-bold tracking-tight">Unsubscribe</h1>
<p className="text-sm text-muted-foreground mt-2">
Stop notification emails and automations that email you. Password resets and
other account emails keep working, and you can re-enable notifications anytime
in Settings.
</p>
</div>
<div className="space-y-3">
<Button className="w-full" onClick={handleUnsubscribe} loading={loading}>
Unsubscribe from notification emails
</Button>
<Button variant="outline" className="w-full" asChild>
<Link href="/settings?tab=notifications">Keep my current preferences</Link>
</Button>
</div>
<p className="text-center text-xs text-muted-foreground">
<Link href="/help" className="text-primary hover:underline">Help center</Link>
</p>
</div>
</div>
)
}