fix(auth): OAUTH_REDIRECT_URI override for local login through the Vite proxy

Through the dev proxy the backend sees Host backend:8000, so url_for built
redirect_uri=https://backend:8000/... which Google rejects with 400
invalid_request. Allow pinning the callback via OAUTH_REDIRECT_URI; the
Host-derived behavior is unchanged when the variable is unset (prod).
This commit is contained in:
juancarlos.cavero 2026-07-17 19:31:41 +02:00
parent 40151e1708
commit 61954a7f28
2 changed files with 21 additions and 9 deletions

View file

@ -7,3 +7,9 @@ AWS_S3_PUBLIC_BUCKET=your-public-bucket-name
# YouTube cookies (optional — paste Netscape-format cookies to bypass bot detection) # YouTube cookies (optional — paste Netscape-format cookies to bypass bot detection)
# YOUTUBE_COOKIES=... # YOUTUBE_COOKIES=...
# Google OAuth (cloud/billing mode only). When logging in through a proxy whose
# Host header doesn't match a Google-registered URI (e.g. local dev via the Vite
# proxy), pin the callback explicitly:
# OAUTH_REDIRECT_URI=http://localhost:8000/api/auth/google/callback
# FRONTEND_URL=http://localhost:5175

View file

@ -8,6 +8,7 @@ GET /api/auth/google/callback -> exchange code, upsert user, redirect to the
Registration happens in ``register()`` (called from setup_sync) only when Google Registration happens in ``register()`` (called from setup_sync) only when Google
credentials are configured; otherwise the endpoints 404. credentials are configured; otherwise the endpoints 404.
""" """
import os
from datetime import datetime, timezone from datetime import datetime, timezone
from fastapi import APIRouter, Request, HTTPException from fastapi import APIRouter, Request, HTTPException
@ -47,6 +48,11 @@ def _now():
async def google_login(request: Request): async def google_login(request: Request):
if oauth is None: if oauth is None:
raise HTTPException(status_code=404, detail="Google login not configured") raise HTTPException(status_code=404, detail="Google login not configured")
# Explicit override for setups where the Host header doesn't match a
# Google-registered URI (e.g. local dev through the Vite proxy, where the
# backend sees the internal Docker host "backend:8000").
redirect_uri = os.environ.get("OAUTH_REDIRECT_URI", "").strip()
if not redirect_uri:
redirect_uri = str(request.url_for("google_callback")) redirect_uri = str(request.url_for("google_callback"))
# Behind a TLS-terminating reverse proxy the app sees the request as http, so # Behind a TLS-terminating reverse proxy the app sees the request as http, so
# url_for builds an http:// redirect_uri — but Google requires the exact # url_for builds an http:// redirect_uri — but Google requires the exact