Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/de/operator/firebase.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: `<URL>/api/auth/callback/<anbieter>` für den Login und `<URL>/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: `<URL>/api/auth/callback/<anbieter>` für den Login (und den Google-Foto-Import bei Mitnutzung von `AUTH_GOOGLE_ID`) bzw. `<URL>/api/sources/callback` bei einem separaten `GOOGLE_SOURCES_CLIENT_ID`.

### Abbauen

Expand Down
6 changes: 3 additions & 3 deletions docs/de/operator/photo-sources.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion docs/en/operator/firebase.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: `<URL>/api/auth/callback/<provider>` for sign-in and `<URL>/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: `<URL>/api/auth/callback/<provider>` for sign-in (and Google photo import when reusing `AUTH_GOOGLE_ID`), or `<URL>/api/sources/callback` when using a dedicated `GOOGLE_SOURCES_CLIENT_ID`.

### Tear down

Expand Down
6 changes: 3 additions & 3 deletions docs/en/operator/photo-sources.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
11 changes: 7 additions & 4 deletions scripts/firebase-bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 15 additions & 1 deletion src/app/api/auth/[...all]/route.ts
Original file line number Diff line number Diff line change
@@ -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);
}
4 changes: 3 additions & 1 deletion src/server/sources/google-oauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ export interface OAuthState extends Record<string, unknown> {
}

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 } {
Expand Down
11 changes: 11 additions & 0 deletions tests/sources/oauth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading