Set up GitHub authentication
This guide shows you how to set up a GitHub App and configure a Cloudflare Worker so editors can sign in to the Administration UI with their GitHub accounts and save content changes to your site’s repository.
Your site’s Worker handles sign-in at /_auth. If you already use a separate authenticator, follow Move an existing authenticator into your site.
Before you begin
Section titled “Before you begin”- Have a LightNet site with the Administration UI and a GitHub repository.
- Use a GitHub account with permission to create and install a GitHub App for the repository owner. For an organization-owned repository, an organization owner can do both.
- Deploy your site to Cloudflare Workers and make sure you can manage the Worker in the Cloudflare dashboard. See the hosting guide.
- Set
siteinastro.config.mjsto the public URL editors use, such as https://my-library.org. If your site uses itsworkers.devaddress, use that URL.
From the root of your site project, run pnpm update to update your installed dependencies, including @lightnet/sveltia-admin.
Step 1. Connect the Administration UI to GitHub
Section titled “Step 1. Connect the Administration UI to GitHub”In astro.config.mjs, add or update backend in your existing lightnetSveltiaAdmin integration. Set repo to your site’s repository in owner/repository format.
// ...export default defineConfig({ site: "https://my-library.org", integrations: [ // ... lightnetSveltiaAdmin({ backend: { name: "github", repo: "your-org/your-site", }, }), ],});For a new setup on your site’s Worker, leave baseUrl unset. The Administration UI defaults to /_auth on your site’s origin.
Step 2. Create the GitHub App
Section titled “Step 2. Create the GitHub App”The LightNet CLI fills in the GitHub App form, installs @lightnet/sveltia-github-auth if needed, and updates wrangler.jsonc. Use the manual tab if your setup requires manual configuration. See When to set it up by hand.
-
Install the LightNet CLI from the root of your site project if needed.
Terminal window pnpm add -D @lightnet/cli@latest -
Run the setup command from the same folder and follow the CLI prompts.
Terminal window pnpm lightnet setup-github-auth
-
Open the settings for the GitHub account or organization that owns the repository. Go to Developer settings → GitHub Apps → New GitHub App.
-
Fill in the GitHub App form using these settings.
Field Value GitHub App name A name that’s unique on GitHub, such as My Library Authentication. Editors see it during authorization. Homepage URL https://lightnet.community Callback URL Your site’s public URL with /_auth/callbackappended, for example https://my-library.org/_auth/callback.Expire user authorization tokens Checked Request user authorization (OAuth) during installation Unchecked Enable Device Flow Unchecked Webhook → Active Unchecked Repository permissions → Contents Read and write. Leave the other permissions at their defaults. Where can this GitHub App be installed? Only on this account -
Select Create GitHub App. Copy the Client ID from the app’s settings.
-
Under Client secrets, select Generate a new client secret. Copy it while it’s visible and keep it secure until you add it to Cloudflare.
-
Select Install App in the sidebar, then Install next to the account that owns the repository. Choose Only select repositories, select your site’s repository, and confirm the installation.
-
Install the authentication Worker from the root of your site project.
Terminal window pnpm add @lightnet/sveltia-github-auth -
Merge the highlighted settings into your site’s
wrangler.jsonc. Replaceyour-github-client-idwith the Client ID from GitHub. If you already have avarsblock, addGITHUB_CLIENT_IDto it.wrangler.jsonc {"name": "your-site","main": "node_modules/@lightnet/sveltia-github-auth/src/site-worker.ts",// ..."assets": {"directory": "./dist","binding": "ASSETS","run_worker_first": ["/_auth/*"],"not_found_handling": "404-page"},"vars": {"GITHUB_CLIENT_ID": "your-github-client-id"}// ...}The
run_worker_firstsetting makes the Worker handle requests under/_auth/before Cloudflare looks for static files. -
Commit and push the changes to
package.json,pnpm-lock.yaml,astro.config.mjs, andwrangler.jsoncto the branch Cloudflare deploys from. Wait for the deployment to succeed. -
In the Cloudflare dashboard, open your site’s Worker and go to Settings → Variables and Secrets. Add
GITHUB_CLIENT_SECRETwith type Secret, paste the value from GitHub, and deploy the change.
See GitHub’s guide to registering a GitHub App and Cloudflare’s guide to adding Worker secrets.
Step 3. Sign in
Section titled “Step 3. Sign in”- Open the Administration UI on your site’s public URL, for example https://my-library.org/admin. If you configured a custom Administration UI path, use that path.
- Choose the GitHub sign-in option. Use a GitHub account with write access to the repository and authorize the app when prompted.
- Open a collection and check that its entries load. If sign-in fails, see Troubleshooting.
Give other editors access
Section titled “Give other editors access”Give each editor write access to the site’s GitHub repository, either as a collaborator or through an organization team. Editors sign in with their own GitHub accounts and authorize the app the first time.
Move an existing authenticator into your site
Section titled “Move an existing authenticator into your site”Follow this procedure if your site is deployed to Cloudflare Workers and currently uses a separate authenticator.
-
Follow steps 1 and 2 to deploy authentication on your site’s Worker and add the client secret. Keep your existing
baseUrlin place during these steps.The CLI creates a new GitHub App. To reuse a GitHub App dedicated to this site, take the Client ID from the existing app and start at the Worker package installation in the Manually tab. Use the app’s existing secret if you have it, or generate a new one in GitHub.
-
Remove
baseUrlfrombackend. Commit, push, and wait for Cloudflare to deploy the change. If you reused the GitHub App, update its first Callback URL to the site’s new callback, for example https://my-library.org/_auth/callback. Sign-in may be interrupted between the deployment and the callback change. -
Follow step 3 in a private browser window to test a fresh sign-in. If you created a new app, editors authorize it on their next sign-in.
-
Once the test succeeds, delete the old Worker and archive its repository if they serve only this site. If you created a new app and the old app serves only this site, remove the old app from Developer settings.
Troubleshooting
Section titled “Troubleshooting”| What you see | What to check |
|---|---|
| “You don’t have access to the repository” | Open the GitHub App’s Install App settings and confirm that the app is installed on your site’s repository. Also check that the GitHub account you’re signing in with has write access. |
| “Your domain is not allowed to use the authenticator” | Open the Administration UI on the public URL in site. |
| The sign-in window opens your old URL or shows “Your Git backend is not supported by the authenticator” | Check the GitHub App’s first callback URL. See Switch to your own domain. |
| “Variables cannot be added to a Worker that only has static assets” | Wait for the deployment that includes main in wrangler.jsonc to succeed, then add the secret again. |
| “GitHub client ID or secret is not configured” | Check GITHUB_CLIENT_ID in the authenticator’s deployed wrangler.jsonc and GITHUB_CLIENT_SECRET in that Worker’s settings. |
| The sign-in window shows your site’s 404 page | Check that package.json includes @lightnet/sveltia-github-auth and that the deployed wrangler.jsonc has the main and run_worker_first settings from step 2. |
| Editors have to sign in again every eight hours | Check the authenticator’s version of @lightnet/sveltia-github-auth. Versions before 1.2.0 can’t refresh expiring access tokens. |