Lumen & Gesso / website · lumengesso.com / app / admin / pinterest / page.tsx
| 1 | import { redirect } from 'next/navigation'; |
| 2 | import { getCustomerUser } from '@/app/lib/lumen-auth'; |
| 3 | import { isAdmin } from '@/app/lib/admin'; |
| 4 | import { listBoards, pinterestSetupStatus, callbackUrl, type PinterestBoard } from '@/app/lib/pinterest'; |
| 5 | import { AdminNav } from '../AdminNav'; |
| 6 | import { DisconnectButton } from './DisconnectButton'; |
| 7 | |
| 8 | export const dynamic = 'force-dynamic'; |
| 9 | |
| 10 | // Admin · Pinterest setup + connection status. Renders one of three states |
| 11 | // depending on which env vars are set: |
| 12 | // 1. Client creds missing → instructions for creating the Pinterest app |
| 13 | // 2. Client creds set, refresh token missing → "Connect Pinterest" CTA |
| 14 | // 3. Fully wired → boards listed + sanity check passes |
| 15 | export default async function AdminPinterestPage() { |
| 16 | const user = await getCustomerUser(); |
| 17 | if (!user) redirect('/login?next=/admin/pinterest'); |
| 18 | if (!isAdmin(user.email)) redirect('/orders'); |
| 19 | |
| 20 | const status = await pinterestSetupStatus(); |
| 21 | let boards: PinterestBoard[] | null = null; |
| 22 | let boardsError: string | null = null; |
| 23 | |
| 24 | if (status.ready) { |
| 25 | try { |
| 26 | boards = await listBoards(); |
| 27 | } catch (e) { |
| 28 | boardsError = e instanceof Error ? e.message : 'failed to list boards'; |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | return ( |
| 33 | <main className="orders"> |
| 34 | <div className="orders__inner" style={{ maxWidth: 760 }}> |
| 35 | <h1 className="orders__title">Admin · Pinterest</h1> |
| 36 | <AdminNav email={user.email!} current="pinterest" /> |
| 37 | |
| 38 | <StatusRow label="Access token (direct-bearer mode)" ok={status.hasAccessToken} /> |
| 39 | <StatusRow label="Client ID / Secret (OAuth mode)" ok={status.hasClient} /> |
| 40 | <StatusRow |
| 41 | label="Refresh token (OAuth mode)" |
| 42 | ok={status.hasRefreshToken} |
| 43 | note={ |
| 44 | status.refreshTokenSource === 'db' |
| 45 | ? 'saved via Connect — managed in DB' |
| 46 | : status.refreshTokenSource === 'env' |
| 47 | ? 'legacy: PINTEREST_REFRESH_TOKEN env var — reconnect to migrate to DB' |
| 48 | : undefined |
| 49 | } |
| 50 | /> |
| 51 | |
| 52 | {status.mode === 'access-token' && ( |
| 53 | <p style={{ color: 'var(--ink-soft)', fontSize: 13, margin: '4px 0 18px' }}> |
| 54 | Wired via PINTEREST_ACCESS_TOKEN. Skipping OAuth — Pinterest’s |
| 55 | dev-console tokens are long-lived but not refreshable; rotate by |
| 56 | generating a new one in the dev portal and re-pasting into env. |
| 57 | </p> |
| 58 | )} |
| 59 | |
| 60 | {status.mode === 'unconfigured' && !status.hasClient && ( |
| 61 | <Step n={1} title="Create the Pinterest app"> |
| 62 | <ol style={{ paddingLeft: 22, margin: '8px 0' }}> |
| 63 | <li>Go to <a href="https://developers.pinterest.com/apps/" target="_blank" rel="noopener noreferrer">developers.pinterest.com/apps</a> and click <strong>Connect app</strong>.</li> |
| 64 | <li>Name it "Lumen & Gesso", upload the logo, and submit.</li> |
| 65 | <li>Open the app, add a <strong>Redirect URI</strong> exactly equal to: |
| 66 | <pre style={pre}>{callbackUrl()}</pre> |
| 67 | (and the production one — <code>https://www.lumengesso.com/api/admin/pinterest/callback</code> — if different).</li> |
| 68 | <li>Copy the <strong>App ID</strong> and <strong>App secret key</strong>.</li> |
| 69 | <li>Paste into your env and restart: |
| 70 | <pre style={pre}>PINTEREST_CLIENT_ID=… |
| 71 | PINTEREST_CLIENT_SECRET=…</pre> |
| 72 | </li> |
| 73 | </ol> |
| 74 | </Step> |
| 75 | )} |
| 76 | |
| 77 | {status.mode !== 'access-token' && status.hasClient && !status.hasRefreshToken && ( |
| 78 | <Step n={2} title="Authorize the app"> |
| 79 | <p>Click below to start the OAuth handshake. Pinterest will ask you to grant |
| 80 | <code>pins:read</code>, <code>pins:write</code>, and <code>boards:read</code> — accept, |
| 81 | and you'll be redirected back here with a refresh token to copy into env.</p> |
| 82 | <p><a href="/api/admin/pinterest/start" style={cta}>Connect Pinterest →</a></p> |
| 83 | <p style={{ color: 'var(--ink-soft)', fontSize: 13 }}> |
| 84 | Callback URL Pinterest will hit: <code>{callbackUrl()}</code> |
| 85 | </p> |
| 86 | </Step> |
| 87 | )} |
| 88 | |
| 89 | {status.ready && ( |
| 90 | <Step n={3} title="Connected"> |
| 91 | {boardsError ? ( |
| 92 | <p style={{ color: 'var(--rose-deep)' }}>Couldn't list boards: {boardsError}</p> |
| 93 | ) : ( |
| 94 | <> |
| 95 | <p>Found <strong>{boards?.length ?? 0}</strong> board{boards?.length === 1 ? '' : 's'}. |
| 96 | Use the <a href="/admin/images">/admin/images</a> page to pin individual portraits.</p> |
| 97 | {boards && boards.length > 0 && ( |
| 98 | <ul style={{ paddingLeft: 22, margin: '6px 0' }}> |
| 99 | {boards.map((b) => ( |
| 100 | <li key={b.id}> |
| 101 | <strong>{b.name}</strong>{' '} |
| 102 | <span style={{ color: 'var(--ink-soft)', fontSize: 12 }}> |
| 103 | (id <code>{b.id}</code>{b.privacy ? `, ${b.privacy}` : ''}) |
| 104 | </span> |
| 105 | </li> |
| 106 | ))} |
| 107 | </ul> |
| 108 | )} |
| 109 | <p style={{ color: 'var(--ink-soft)', fontSize: 13 }}> |
| 110 | Need to re-auth? <a href="/api/admin/pinterest/start">Reconnect</a> (refresh tokens last ~1 year). |
| 111 | </p> |
| 112 | {status.mode === 'oauth' && ( |
| 113 | <DisconnectButton legacyEnvSet={status.refreshTokenSource === 'env'} /> |
| 114 | )} |
| 115 | </> |
| 116 | )} |
| 117 | </Step> |
| 118 | )} |
| 119 | </div> |
| 120 | </main> |
| 121 | ); |
| 122 | } |
| 123 | |
| 124 | function StatusRow({ label, ok, note }: { label: string; ok: boolean; note?: string }) { |
| 125 | return ( |
| 126 | <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', padding: '10px 14px', border: '1px solid var(--rule)', borderRadius: 8, marginBottom: 8, fontSize: 14, background: '#fff', gap: 12 }}> |
| 127 | <span style={{ minWidth: 0 }}> |
| 128 | {label} |
| 129 | {note && ( |
| 130 | <span style={{ display: 'block', color: 'var(--ink-soft)', fontSize: 11.5, fontWeight: 400, marginTop: 2 }}> |
| 131 | {note} |
| 132 | </span> |
| 133 | )} |
| 134 | </span> |
| 135 | <span style={{ color: ok ? 'var(--ink)' : 'var(--rose-deep)', fontSize: 13, fontWeight: 600, flexShrink: 0 }}> |
| 136 | {ok ? 'set ✓' : 'not set'} |
| 137 | </span> |
| 138 | </div> |
| 139 | ); |
| 140 | } |
| 141 | |
| 142 | function Step({ n, title, children }: { n: number; title: string; children: React.ReactNode }) { |
| 143 | return ( |
| 144 | <section style={{ marginTop: 24, padding: '18px 22px', border: '1px solid var(--rule)', borderRadius: 10, background: '#fff' }}> |
| 145 | <div style={{ fontSize: 11, fontWeight: 700, letterSpacing: '0.16em', textTransform: 'uppercase', color: 'var(--ink-soft)', marginBottom: 6 }}>Step {n}</div> |
| 146 | <h2 style={{ fontSize: 20, margin: '0 0 10px' }}>{title}</h2> |
| 147 | {children} |
| 148 | </section> |
| 149 | ); |
| 150 | } |
| 151 | |
| 152 | const pre: React.CSSProperties = { |
| 153 | background: '#1d1a17', color: '#f3eadd', padding: '10px 14px', |
| 154 | borderRadius: 8, fontSize: 12.5, lineHeight: 1.45, overflowX: 'auto', margin: '6px 0', |
| 155 | }; |
| 156 | |
| 157 | const cta: React.CSSProperties = { |
| 158 | display: 'inline-block', padding: '9px 16px', borderRadius: 8, |
| 159 | background: 'var(--ink)', color: 'var(--cream)', textDecoration: 'none', fontSize: 14, fontWeight: 600, |
| 160 | }; |