7.8 KiB
Cloudflare Workers OAuth Proxy Setup
Complete guide to deploying Sveltia CMS authentication using Cloudflare Workers.
Overview
This OAuth proxy enables Sveltia CMS to authenticate with GitHub or GitLab without requiring a backend server. The Worker handles the OAuth flow securely.
Official Repository: https://github.com/sveltia/sveltia-cms-auth
Prerequisites
- Cloudflare account (free tier works)
- GitHub or GitLab account
- Wrangler CLI installed (
npm install -g wrangler) - Git repository with Sveltia CMS
Step 1: Deploy the Worker
Option A: Use Official Repository (Recommended)
# Clone the official auth worker
git clone https://github.com/sveltia/sveltia-cms-auth
cd sveltia-cms-auth
# Install dependencies
npm install
# Login to Cloudflare (if not already logged in)
npx wrangler login
# Deploy to Workers
npx wrangler deploy
Output:
⛅️ wrangler 3.x.x
-------------------
Uploaded sveltia-cms-auth (x.x KiB)
Published sveltia-cms-auth (x.xx sec)
https://sveltia-cms-auth.<your-subdomain>.workers.dev
Save this URL - you'll need it later!
Option B: One-Click Deploy
- Visit https://github.com/sveltia/sveltia-cms-auth
- Click the "Deploy to Cloudflare Workers" button
- Follow the deployment wizard
- Note the deployed Worker URL
Option C: Use This Template
If you want to customize the Worker:
-
Copy template to your project:
mkdir -p workers/sveltia-auth cp templates/cloudflare-workers/* workers/sveltia-auth/ -
Update
wrangler.jsoncwith your account ID -
Deploy:
cd workers/sveltia-auth npx wrangler deploy
Step 2: Register OAuth App on GitHub
-
Go to GitHub Settings: https://github.com/settings/developers
-
Click "New OAuth App"
-
Fill in details:
- Application name:
Your Site CMS(or any name) - Homepage URL:
https://yourdomain.com(your actual site) - Authorization callback URL:
https://sveltia-cms-auth.<your-subdomain>.workers.dev/callback- ⚠️ Critical: This must be the Worker URL +
/callback - ❌ NOT:
https://yourdomain.com/callback - ✅ Example:
https://sveltia-cms-auth.my-account.workers.dev/callback
- ⚠️ Critical: This must be the Worker URL +
- Application name:
-
Click "Register application"
-
Save these values:
- Client ID:
Ov23li...(visible immediately) - Client Secret: Click "Generate a new client secret" and copy it (shown once!)
- Client ID:
Step 3: Configure Worker Environment Variables
Set the secrets using Wrangler CLI:
cd sveltia-cms-auth # or your Worker directory
# Set GitHub Client ID
npx wrangler secret put GITHUB_CLIENT_ID
# Paste your Client ID when prompted
# Set GitHub Client Secret
npx wrangler secret put GITHUB_CLIENT_SECRET
# Paste your Client Secret when prompted
Optional: Restrict to specific domains:
npx wrangler secret put ALLOWED_DOMAINS
# Enter domains when prompted, e.g.: yourdomain.com,*.yourdomain.com
Wildcards:
yourdomain.com- Exact match only*.yourdomain.com- All subdomains (www.yourdomain.com, blog.yourdomain.com)yourdomain.com,*.yourdomain.com- Both root and subdomains
Step 4: Update Sveltia CMS Config
Add the base_url to your CMS configuration:
# static/admin/config.yml (Hugo)
# or admin/config.yml (Jekyll/11ty)
# or public/admin/config.yml (Astro/Next.js)
backend:
name: github
repo: owner/repo # Your GitHub repository
branch: main # Your default branch
base_url: https://sveltia-cms-auth.<your-subdomain>.workers.dev # ← Add this line
Example:
backend:
name: github
repo: jezweb/my-blog
branch: main
base_url: https://sveltia-cms-auth.my-account.workers.dev
Step 5: Test Authentication
-
Start your local dev server:
hugo server # or jekyll serve, or npm run dev -
Open admin in browser:
http://localhost:1313/admin/ # Hugo http://localhost:4000/admin/ # Jekyll http://localhost:8080/admin/ # 11ty http://localhost:4321/admin/ # Astro -
Click "Login with GitHub"
-
Authorize the app when GitHub asks
-
You should be redirected back to the CMS with content loaded
Step 6: Deploy Your Site
-
Commit changes:
git add static/admin/config.yml # or your admin path git commit -m "Add Cloudflare Workers OAuth for Sveltia CMS" git push -
Deploy to your hosting:
- Cloudflare Pages: Automatic on push
- Vercel: Automatic on push
- Netlify: Automatic on push
- GitHub Pages: GitHub Actions workflow
-
Test on production:
https://yourdomain.com/admin/
GitLab Setup (Alternative)
If using GitLab instead of GitHub:
1. Create GitLab Application
- Go to https://gitlab.com/-/profile/applications
- Click "Add new application"
- Fill in:
- Name:
Your Site CMS - Redirect URI:
https://sveltia-cms-auth.<your-subdomain>.workers.dev/callback - Scopes: Check
apiandwrite_repository
- Name:
- Click "Save application"
- Copy Application ID and Secret
2. Set Worker Secrets
npx wrangler secret put GITLAB_APPLICATION_ID
# Paste Application ID
npx wrangler secret put GITLAB_SECRET
# Paste Secret
3. Update CMS Config
backend:
name: gitlab
repo: group/project
branch: main
base_url: https://sveltia-cms-auth.<your-subdomain>.workers.dev
Troubleshooting
Authentication Fails
Problem: "Error: Failed to authenticate"
Solutions:
- Check callback URL matches exactly (Worker URL +
/callback) - Verify secrets are set:
npx wrangler secret list - Test Worker directly:
curl https://your-worker.workers.dev/health - Check browser console for CORS errors
Redirect to Wrong Domain
Problem: Redirects to api.netlify.com/auth
Solutions:
- Ensure
base_urlis in config.yml - Hard refresh browser (Ctrl+Shift+R)
- Clear browser cache
CORS Errors
Problem: "Cross-Origin-Opener-Policy blocked"
Solutions:
- Add COOP header to site (see SKILL.md Error #8)
- Set
Cross-Origin-Opener-Policy: same-origin-allow-popups - For Cloudflare Pages, add
_headersfile:/* Cross-Origin-Opener-Policy: same-origin-allow-popups
Worker Not Found (404)
Problem: Worker URL returns 404
Solutions:
- Verify deployment:
npx wrangler deployments list - Check Worker name in
wrangler.jsonc - Wait 1-2 minutes after deployment (DNS propagation)
Security Best Practices
- Never commit secrets - Use
npx wrangler secret put - Restrict domains - Set
ALLOWED_DOMAINSfor production - Use HTTPS only - Sveltia CMS requires secure context
- Rotate secrets periodically - Update GitHub OAuth secrets every 6-12 months
- Monitor access logs - Check Cloudflare Workers dashboard for unusual activity
Custom Domain (Optional)
To use a custom domain for your OAuth Worker:
-
Add Worker route in Cloudflare:
- Dashboard > Workers > Your Worker > Triggers
- Add custom domain:
auth.yourdomain.com
-
Update GitHub OAuth callback:
- Change to:
https://auth.yourdomain.com/callback
- Change to:
-
Update CMS config:
backend: base_url: https://auth.yourdomain.com
Cost
Cloudflare Workers Free Tier:
- 100,000 requests/day
- 10ms CPU time per request
- More than enough for CMS authentication
Typical usage: 50-100 requests/month for small teams
Additional Resources
- Official Auth Worker: https://github.com/sveltia/sveltia-cms-auth
- Sveltia CMS Docs: https://github.com/sveltia/sveltia-cms
- Wrangler Docs: https://developers.cloudflare.com/workers/wrangler/
- OAuth 2.0 Spec: https://oauth.net/2/
Last Updated: 2025-10-24