import { createSignal, onMount, Show } from 'solid-js'; import { Button, ConfirmDialog } from '@components'; import API from '@util/api'; /** * Cloudflare cloud mark. Inherits currentColor so it can sit on the primary * button (white) or the zone card (brand orange). */ function CloudflareMark(props) { return ( ); } export default function CloudflareSetup(props) { const [matchedZone, setMatchedZone] = createSignal(null); const [siteHost, setSiteHost] = createSignal(''); const [step, setStep] = createSignal(1); const [loading, setLoading] = createSignal(''); const [error, setError] = createSignal(''); const [notice, setNotice] = createSignal(''); const [confirmDisconnect, setConfirmDisconnect] = createSignal(false); // How the Cloudflare account is linked ('oauth' | 'token' | ''). Seeded // from the localized status so a reload still shows the link, then kept // current as the user connects or disconnects. const [connectedVia, setConnectedVia] = createSignal( window.ajaxpress_admin_vars?.cf_status?.auth_method || '' ); const authLabel = () => connectedVia() === 'oauth' ? 'one-click authorization' : 'an API token'; // Drop the stored credentials so the user can start over. Does not touch a // deployed worker - that is the teardown button on the status screen. const disconnect = async () => { setConfirmDisconnect(false); setLoading('Disconnecting...'); setError(''); try { await API.post('cf/disconnect', {}); setConnectedVia(''); setMatchedZone(null); setStep(1); setNotice('Cloudflare account disconnected.'); } catch (err) { setError('Could not disconnect. Please try again.'); } finally { setLoading(''); } }; // After a token or OAuth connect, find the zone that matches this site. const loadZones = async () => { setError(''); const zonesRes = await API.get('cf/zones'); setSiteHost(zonesRes.data.site_host || ''); if (zonesRes.data?.matched_zone) { setMatchedZone(zonesRes.data.matched_zone); setStep(2); } else { setError( `Your site domain (${zonesRes.data.site_host}) is not found in your Cloudflare account. ` + 'Please add your domain to Cloudflare and ensure DNS is proxied (orange cloud).' ); } }; // Same lookup, but driven by the retry button, so it needs its own // spinner and must not leave the button stuck on failure. const retryZones = async () => { setLoading('Looking for your domain...'); try { await loadZones(); } catch (err) { setError('Connection error. Please try again.'); } finally { setLoading(''); } }; // Where Cloudflare should drop the user back. Must keep ?page=ajaxpress - // without it this lands on WordPress' General Settings screen instead of // the plugin - and point at the Cache tab. const buildReturnUrl = () => { const url = new URL(window.top.location.href); url.searchParams.delete('ajaxpress_cf_code'); url.searchParams.delete('ajaxpress_cf_error'); url.searchParams.delete('state'); url.hash = '#/cache'; return url.toString(); }; // Kick off the one-click flow: get the relay URL and send the user there. const startOAuth = async () => { setLoading('Connecting to Cloudflare...'); setError(''); try { const res = await API.post('cf/oauth-start', { return_url: buildReturnUrl() }); if (res.data?.success && res.data?.authorize_url) { window.top.location.href = res.data.authorize_url; } else { setError(res.data?.message || 'Could not start the Cloudflare connection.'); setLoading(''); } } catch (err) { setError('Connection error. Please try again.'); setLoading(''); } }; // Turn an OAuth error code from Cloudflare into something readable. const describeOAuthError = (code) => { const known = { access_denied: 'You cancelled the Cloudflare authorization.', invalid_scope: 'Cloudflare rejected the requested permissions. Please contact support.', invalid_request: 'Cloudflare rejected the authorization request. Please try again.', }; return known[code] || `Cloudflare returned an error (${code}). Please try again.`; }; // When the relay sends us back with ?ajaxpress_cf_code= (or an error), // finish the flow and report the outcome either way. onMount(async () => { const params = new URLSearchParams(window.top.location.search); const code = params.get('ajaxpress_cf_code'); const oauthError = params.get('ajaxpress_cf_error'); const state = params.get('state'); if (!code && !oauthError) return; // Strip our params so a refresh can't replay the code. window.top.history.replaceState({}, '', buildReturnUrl()); if (oauthError) { setError(describeOAuthError(oauthError)); return; } if (!state) { setError('The Cloudflare response was incomplete. Please try connecting again.'); return; } setLoading('Finishing Cloudflare connection...'); try { const res = await API.post('cf/oauth-exchange', { code, state }); if (res.data?.success) { setConnectedVia('oauth'); setNotice('Connected to your Cloudflare account.'); await loadZones(); } else { setError(res.data?.message || 'Could not complete the Cloudflare connection.'); } } catch (err) { setError('Connection error. Please try again.'); } finally { setLoading(''); } }); const deploy = async () => { const zone = matchedZone(); if (!zone) { setError('No matching zone found'); return; } setLoading('Deploying cache worker...'); setError(''); try { const res = await API.post('cf/deploy', { zone_id: zone.id }); if (res.data?.success) { props.onConnected?.(); } else { setError(res.data?.message || 'Failed to deploy'); } } catch (err) { console.error('CF deploy error:', err); setError('Connection error. Please try again.'); } finally { setLoading(''); } }; return (
Authorize AjaxPress in your Cloudflare account. Nothing to create or copy.
{siteHost()}/*