diff --git a/docs/google-sign-in.md b/docs/google-sign-in.md index 607f868..3d84ead 100644 --- a/docs/google-sign-in.md +++ b/docs/google-sign-in.md @@ -53,8 +53,16 @@ and restart. Never commit the secret or paste it into the repo: [Service] Environment=GOOGLE_CLIENT_ID= Environment=GOOGLE_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 | diff --git a/docs/seo.md b/docs/seo.md index 22840b4..2b719fd 100644 --- a/docs/seo.md +++ b/docs/seo.md @@ -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] diff --git a/server/src/googleAuth.js b/server/src/googleAuth.js index 8f59943..6baaedb 100644 --- a/server/src/googleAuth.js +++ b/server/src/googleAuth.js @@ -10,7 +10,9 @@ GOOGLE_CLIENT_ID from Google Cloud Console GOOGLE_CLIENT_SECRET the same; never in the repo - GOOGLE_REDIRECT_URI default /api/auth/google/callback + GOOGLE_REDIRECT_URI default /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) { - const origin = (loadSite().url ?? "").replace(/\/$/, ""); - redirectUri = `${origin}/api/auth/google/callback`; + try { + const origin = (loadSite().url ?? "").replace(/\/$/, ""); + if (origin) redirectUri = `${origin}/api/auth/google/callback`; + } catch { + redirectUri = null; + } } return { diff --git a/server/src/routes/auth.js b/server/src/routes/auth.js index e2100f9..179452a 100644 --- a/server/src/routes/auth.js +++ b/server/src/routes/auth.js @@ -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")) }), {