From b289dd0c9567372ff6aa75c7b31d413f126345c2 Mon Sep 17 00:00:00 2001 From: Thomas Sprock Date: Sun, 1 Nov 2026 17:47:00 +0000 Subject: [PATCH] fix(sources): reuse Google sign-in redirect URI when sharing AUTH_GOOGLE_ID --- docs/de/operator/firebase.md | 2 +- docs/de/operator/photo-sources.md | 6 +++--- docs/en/operator/firebase.md | 2 +- docs/en/operator/photo-sources.md | 6 +++--- scripts/firebase-bootstrap.sh | 11 +++++++---- src/app/api/auth/[...all]/route.ts | 16 +++++++++++++++- src/server/sources/google-oauth.ts | 4 +++- tests/sources/oauth.test.ts | 11 +++++++++++ 8 files changed, 44 insertions(+), 14 deletions(-) diff --git a/docs/de/operator/firebase.md b/docs/de/operator/firebase.md index 852b5eb..bf558e7 100644 --- a/docs/de/operator/firebase.md +++ b/docs/de/operator/firebase.md @@ -73,7 +73,7 @@ Der Re-Deploy ist nötig: Ein laufendes Backend nutzt weiter die Secret-Version, ### Eigene Domain -Verbinde die Domain in der Firebase-Konsole (*App Hosting → Einstellungen → Domains*), starte dann das Skript mit `--reconfigure` und gib die URL ein (setzt `WAYFARE_BASE_URL`). Passe die Redirect-URIs deiner OAuth-Clients an: `/api/auth/callback/` für den Login und `/api/sources/callback` für den Google-Foto-Import. +Verbinde die Domain in der Firebase-Konsole (*App Hosting → Einstellungen → Domains*), starte dann das Skript mit `--reconfigure` und gib die URL ein (setzt `WAYFARE_BASE_URL`). Passe die Redirect-URIs deiner OAuth-Clients an: `/api/auth/callback/` für den Login (und den Google-Foto-Import bei Mitnutzung von `AUTH_GOOGLE_ID`) bzw. `/api/sources/callback` bei einem separaten `GOOGLE_SOURCES_CLIENT_ID`. ### Abbauen diff --git a/docs/de/operator/photo-sources.md b/docs/de/operator/photo-sources.md index c72a668..6e21b07 100644 --- a/docs/de/operator/photo-sources.md +++ b/docs/de/operator/photo-sources.md @@ -24,14 +24,14 @@ Wähle in der [Google Cloud Console](https://console.cloud.google.com) ein Proje ## 3. OAuth-Client -*Anmeldedaten → Anmeldedaten erstellen → OAuth-Client-ID → Webanwendung*. Autorisierte Weiterleitungs-URI: +Nutzt du bereits Google-Login (`AUTH_GOOGLE_ID` / `AUTH_GOOGLE_SECRET`), kannst du `GOOGLE_SOURCES_CLIENT_ID` und `GOOGLE_SOURCES_CLIENT_SECRET` leer lassen: Wayfare verwendet dann automatisch denselben OAuth-Client und dessen bereits hinterlegte Weiterleitungs-URI (`${WAYFARE_BASE_URL}/api/auth/callback/google`). + +Wenn du stattdessen einen eigenen OAuth-Client für den Foto-Import anlegen möchtest (*Anmeldedaten → Anmeldedaten erstellen → OAuth-Client-ID → Webanwendung*), setze `GOOGLE_SOURCES_CLIENT_ID` und `GOOGLE_SOURCES_CLIENT_SECRET` und trage als autorisierte Weiterleitungs-URI ein: ``` ${WAYFARE_BASE_URL}/api/sources/callback ``` -Setze `GOOGLE_SOURCES_CLIENT_ID` und `GOOGLE_SOURCES_CLIENT_SECRET`. Lässt du sie leer, fällt Wayfare auf `AUTH_GOOGLE_ID`/`AUTH_GOOGLE_SECRET` zurück – dann trage **beide** Redirect-URIs (`/api/auth/callback/google` und `/api/sources/callback`) bei diesem Client ein. - ## 4. Drive Picker: API-Schlüssel und App-ID Der Drive-Dateiauswahldialog läuft im Browser und braucht: diff --git a/docs/en/operator/firebase.md b/docs/en/operator/firebase.md index c6f3a58..3d4027b 100644 --- a/docs/en/operator/firebase.md +++ b/docs/en/operator/firebase.md @@ -73,7 +73,7 @@ The re-deploy is required: a running backend keeps using the secret version it w ### Custom domain -Connect the domain in the Firebase console (*App Hosting → Settings → Domains*), then run the script with `--reconfigure` and enter the URL (it sets `WAYFARE_BASE_URL`). Update the redirect URIs of your OAuth clients: `/api/auth/callback/` for sign-in and `/api/sources/callback` for Google photo import. +Connect the domain in the Firebase console (*App Hosting → Settings → Domains*), then run the script with `--reconfigure` and enter the URL (it sets `WAYFARE_BASE_URL`). Update the redirect URIs of your OAuth clients: `/api/auth/callback/` for sign-in (and Google photo import when reusing `AUTH_GOOGLE_ID`), or `/api/sources/callback` when using a dedicated `GOOGLE_SOURCES_CLIENT_ID`. ### Tear down diff --git a/docs/en/operator/photo-sources.md b/docs/en/operator/photo-sources.md index fb13980..8d321cd 100644 --- a/docs/en/operator/photo-sources.md +++ b/docs/en/operator/photo-sources.md @@ -24,14 +24,14 @@ In the [Google Cloud Console](https://console.cloud.google.com) select (or creat ## 3. OAuth client -*Credentials → Create credentials → OAuth client ID → Web application*. Authorized redirect URI: +If you already use Google sign-in (`AUTH_GOOGLE_ID` / `AUTH_GOOGLE_SECRET`), you can leave `GOOGLE_SOURCES_CLIENT_ID` and `GOOGLE_SOURCES_CLIENT_SECRET` empty: Wayfare automatically reuses that OAuth client and its existing redirect URI (`${WAYFARE_BASE_URL}/api/auth/callback/google`). + +If you prefer a dedicated OAuth client for photo import (*Credentials → Create credentials → OAuth client ID → Web application*), set `GOOGLE_SOURCES_CLIENT_ID` and `GOOGLE_SOURCES_CLIENT_SECRET` and add the authorized redirect URI: ``` ${WAYFARE_BASE_URL}/api/sources/callback ``` -Set `GOOGLE_SOURCES_CLIENT_ID` and `GOOGLE_SOURCES_CLIENT_SECRET`. If you leave them empty Wayfare falls back to `AUTH_GOOGLE_ID`/`AUTH_GOOGLE_SECRET` — then add **both** redirect URIs (`/api/auth/callback/google` and `/api/sources/callback`) to that client. - ## 4. Drive Picker: API key and app ID The Drive file picker runs in the browser and needs: diff --git a/scripts/firebase-bootstrap.sh b/scripts/firebase-bootstrap.sh index 883e9f8..cd8c9ae 100755 --- a/scripts/firebase-bootstrap.sh +++ b/scripts/firebase-bootstrap.sh @@ -703,15 +703,18 @@ step_config() { done if [ "$WANT_PHOTO_IMPORT" = yes ]; then - info "Google photo import: add this redirect URI to your OAuth client: ${BASE_URL}/api/sources/callback" - note "(Reuses the Google sign-in client unless you set GOOGLE_SOURCES_CLIENT_ID/SECRET in the secret later.)" - info " API key (for the Drive Picker): https://console.cloud.google.com/apis/credentials?project=$PROJECT_ID -> Create credentials -> API key" - if ! env_has AUTH_GOOGLE_ID; then + if env_has AUTH_GOOGLE_ID; then + ok "Google photo import reuses your Google sign-in OAuth client (${BASE_URL}/api/auth/callback/google)." + note "Make sure the OAuth consent screen includes the scopes 'photospicker.mediaitems.readonly' and 'drive.file'." + else + info "Google photo import: create a Web OAuth client with authorized redirect URI: ${BASE_URL}/api/sources/callback" ask v "OAuth client ID for photo import:" "$(env_get GOOGLE_SOURCES_CLIENT_ID)" v_nonempty env_set GOOGLE_SOURCES_CLIENT_ID "$v" ask_secret v "OAuth client secret for photo import:" v_nonempty env_set GOOGLE_SOURCES_CLIENT_SECRET "$v" fi + info " API key (for the Drive Picker): https://console.cloud.google.com/apis/credentials?project=$PROJECT_ID -> Create credentials -> API key" + note " (If you restrict the API key to specific APIs, include 'Google Picker API' / picker.googleapis.com.)" ask_secret v "GOOGLE_API_KEY (empty = skip):" [ -n "$v" ] && env_set GOOGLE_API_KEY "$v" fi diff --git a/src/app/api/auth/[...all]/route.ts b/src/app/api/auth/[...all]/route.ts index feb9f5b..2be22c5 100644 --- a/src/app/api/auth/[...all]/route.ts +++ b/src/app/api/auth/[...all]/route.ts @@ -1,9 +1,23 @@ import { toNextJsHandler } from "better-auth/next-js"; import { getAuth } from "@/server/auth/auth"; +import { readState } from "@/server/sources/google-oauth"; +import { GET as handleSourcesCallback } from "../../sources/callback/route"; // Better Auth needs the Node.js runtime (better-sqlite3, crypto) and must never be cached. export const runtime = "nodejs"; export const dynamic = "force-dynamic"; // The instance is created lazily on the first request (config() reads env at call time). -export const { GET, POST } = toNextJsHandler((request: Request) => getAuth().handler(request)); +const authHandler = toNextJsHandler((request: Request) => getAuth().handler(request)); + +export const POST = authHandler.POST; + +// When GOOGLE_SOURCES_CLIENT_ID is unset and AUTH_GOOGLE_ID is reused for photo sources, +// Google redirects back to /api/auth/callback/google with a signed "oauth-state" token. +export async function GET(request: Request) { + const url = new URL(request.url); + if (url.pathname === "/api/auth/callback/google" && readState(url.searchParams.get("state"))) { + return handleSourcesCallback(request); + } + return authHandler.GET(request); +} diff --git a/src/server/sources/google-oauth.ts b/src/server/sources/google-oauth.ts index ea2d66e..446835a 100644 --- a/src/server/sources/google-oauth.ts +++ b/src/server/sources/google-oauth.ts @@ -31,7 +31,9 @@ export interface OAuthState extends Record { } export function redirectUri(): string { - return `${config().baseUrl}/api/sources/callback`; + const cfg = config(); + const usesAuthClient = Boolean(cfg.AUTH_GOOGLE_ID) && (!cfg.GOOGLE_SOURCES_CLIENT_ID || cfg.GOOGLE_SOURCES_CLIENT_ID === cfg.AUTH_GOOGLE_ID); + return `${cfg.baseUrl}${usesAuthClient ? "/api/auth/callback/google" : "/api/sources/callback"}`; } export function googleCredentials(): { clientId: string; clientSecret: string } { diff --git a/tests/sources/oauth.test.ts b/tests/sources/oauth.test.ts index 6b584d5..1e5370e 100644 --- a/tests/sources/oauth.test.ts +++ b/tests/sources/oauth.test.ts @@ -34,6 +34,17 @@ describe("buildAuthUrl", () => { expect(readState(p.get("state"))).toEqual({ userId: "u1", sourceId: "google-photos", returnTo: "/dashboard" }); }); + it("reuses /api/auth/callback/google when falling back to AUTH_GOOGLE_ID", () => { + delete process.env.GOOGLE_SOURCES_CLIENT_ID; + delete process.env.GOOGLE_SOURCES_CLIENT_SECRET; + process.env.AUTH_GOOGLE_ID = "shared-client.apps.googleusercontent.com"; + process.env.AUTH_GOOGLE_SECRET = "shared-secret"; + resetConfigForTests(); + const url = new URL(buildAuthUrl({ userId: "u1", sourceId: "google-photos", returnTo: "/account", scopes: [PHOTOS_SCOPE] })); + expect(url.searchParams.get("client_id")).toBe("shared-client.apps.googleusercontent.com"); + expect(url.searchParams.get("redirect_uri")).toBe("https://wayfare.example/api/auth/callback/google"); + }); + it("rejects when the operator did not configure credentials", () => { delete process.env.GOOGLE_SOURCES_CLIENT_ID; delete process.env.AUTH_GOOGLE_ID;