Show Google sign-in even when site.json isn't deployed

/api/auth/providers read site.json to build Google's return address,
and the deploy copies server/ into /srv/ngu-api without site.json, so
it threw and the login page hid the button. Whether Google is on now
depends only on the client id and secret; a missing return address is
logged when a sign-in starts, naming GOOGLE_REDIRECT_URI and
SITE_CONFIG. The docs say how the server is actually deployed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Zaldimmar 2026-09-27 21:51:40 -05:00
parent d4b39251d9
commit e932ee3007
4 changed files with 35 additions and 6 deletions

View file

@ -53,8 +53,16 @@ and restart. Never commit the secret or paste it into the repo:
[Service]
Environment=GOOGLE_CLIENT_ID=<client id>
Environment=GOOGLE_CLIENT_SECRET=<client secret>
Environment=GOOGLE_REDIRECT_URI=https://nextgenerationofunity.org/api/auth/google/callback
```
`GOOGLE_REDIRECT_URI` must match the redirect URI in step 3 exactly.
Without it the API builds it from `site.json`'s `url`, which the API only
finds if `site.json` is deployed with it and `SITE_CONFIG` points at it
(the deploy copies `server/` into `/srv/ngu-api`, and `site.json` lives
at the repo root, so it isn't there by default). If neither is set,
sign-in fails and the log says so.
Optional:
| Variable | Default |

View file

@ -29,8 +29,11 @@ with it off it blocks everything.
## Production setup
1. The API needs to find two files. Both default to the repo layout
(`site.json` at the root, the build in `dist/`); set them on the
`ngu-api` unit if the deploy puts them elsewhere:
(`site.json` at the root, the build in `dist/`), which isn't how the
server is deployed: the deploy copies `server/` into `/srv/ngu-api`,
so neither file is next to it. Copy `site.json` there with each
deploy (eg. rsync it to `/srv/ngu-api/site.json`) and set both on the
`ngu-api` unit:
```ini
[Service]

View file

@ -10,7 +10,9 @@
GOOGLE_CLIENT_ID from Google Cloud Console
GOOGLE_CLIENT_SECRET the same; never in the repo
GOOGLE_REDIRECT_URI default <site.json url>/api/auth/google/callback
GOOGLE_REDIRECT_URI default <site.json url>/api/auth/google/callback;
set it when the server is deployed
without site.json (SITE_CONFIG)
GOOGLE_ALLOWED_DOMAIN default nextgenerationofunity.org
With no client id or secret it's off: the login page doesn't
@ -45,10 +47,18 @@ export function googleConfig() {
const clientSecret = process.env.GOOGLE_CLIENT_SECRET;
if (!clientId || !clientSecret) return null;
let redirectUri = process.env.GOOGLE_REDIRECT_URI;
// Whether Google is on depends only on the id and secret. The
// return address can come from site.json, which a deploy of the
// server alone may not have; that's reported where it's needed
// (the sign-in routes), rather than hiding the button.
let redirectUri = process.env.GOOGLE_REDIRECT_URI ?? null;
if (!redirectUri) {
try {
const origin = (loadSite().url ?? "").replace(/\/$/, "");
redirectUri = `${origin}/api/auth/google/callback`;
if (origin) redirectUri = `${origin}/api/auth/google/callback`;
} catch {
redirectUri = null;
}
}
return {

View file

@ -136,6 +136,14 @@ auth.get("/providers", (c) =>
auth.get("/google", (c) => {
const config = googleConfig();
if (!config) return c.json({ error: "Google sign-in isn't set up." }, 404);
if (!config.redirectUri) {
// Said in the log, where whoever runs the box will look; the
// person at the login page just learns it didn't work.
console.error(
"google login: no return address. Set GOOGLE_REDIRECT_URI, or SITE_CONFIG to a site.json with a url.",
);
return backToLogin(c, "google_failed");
}
const { url, state, verifier } = startLogin(config);
setCookie(c, OAUTH_COOKIE, JSON.stringify({ state, verifier, next: safeNext(c.req.query("next")) }), {