Añadido soporte para autenticación con Google y ajustes en la configuración del entorno

This commit is contained in:
Mireya Cueto Garrido
2026-04-13 11:08:14 +02:00
parent f216ddea80
commit 040989bdfc
4 changed files with 18 additions and 18 deletions
+1 -2
View File
@@ -25,8 +25,6 @@ async def lifespan(app: FastAPI):
app = FastAPI(lifespan=lifespan)
app.include_router(google_auth_router)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
@@ -45,3 +43,4 @@ app.include_router(test_mongo_router, prefix="/api")
app.include_router(auth_router, prefix="/api")
app.include_router(history_router, prefix="/api")
app.include_router(docit2mf_router, prefix="/api")
app.include_router(google_auth_router, prefix="/api")
+11 -14
View File
@@ -1,6 +1,6 @@
# api/routers/google_auth.py
from fastapi import APIRouter, HTTPException, Depends
from fastapi import APIRouter, HTTPException, Depends, Request
from fastapi.responses import RedirectResponse
from pydantic import BaseModel
from bson import ObjectId
@@ -13,10 +13,9 @@ from api.utils.security import create_access_token
router = APIRouter(prefix="/auth/google", tags=["auth"])
GOOGLE_CLIENT_ID = os.getenv("GOOGLE_CLIENT_ID")
GOOGLE_CLIENT_SECRET = os.getenv("GOOGLE_CLIENT_SECRET")
REDIRECT_URI = "http://localhost:8000/api/auth/google/callback"
REDIRECT_URI = os.getenv("GOOGLE_REDIRECT_URI")
# -----------------------------
@@ -25,7 +24,7 @@ REDIRECT_URI = "http://localhost:8000/api/auth/google/callback"
@router.get("/login")
async def google_login():
google_auth_url = (
"https://accounts.google.com/o/oauth2/v2/auth"
"https://accounts.google.com/o/oauth2/auth"
"?response_type=code"
f"&client_id={GOOGLE_CLIENT_ID}"
f"&redirect_uri={REDIRECT_URI}"
@@ -37,13 +36,17 @@ async def google_login():
return RedirectResponse(google_auth_url)
# -----------------------------
# 2. CALLBACK → GOOGLE DEVUELVE EL CODE
# -----------------------------
@router.get("/callback")
async def google_callback(code: str):
async def google_callback(request: Request):
code = request.query_params.get("code")
if not code:
raise HTTPException(status_code=400, detail="Missing code parameter")
# 1. Intercambiar code por access_token
token_url = "https://oauth2.googleapis.com/token"
data = {
@@ -59,11 +62,10 @@ async def google_callback(code: str):
token_json = token_response.json()
if "access_token" not in token_json:
raise HTTPException(status_code=400, detail="Error obteniendo token de Google")
raise HTTPException(status_code=400, detail=token_json)
access_token = token_json["access_token"]
# 2. Obtener datos del usuario desde Google
async with httpx.AsyncClient() as client:
userinfo = await client.get(
"https://www.googleapis.com/oauth2/v2/userinfo",
@@ -76,26 +78,21 @@ async def google_callback(code: str):
email = user_data["email"]
name = user_data.get("name", "Usuario")
# 3. Buscar usuario en MongoDB
user = await users_collection.find_one({"email": email})
if not user:
# Crear usuario nuevo
new_user = {
"username": name,
"email": email,
"password_hash": None, # No hay contraseña
"password_hash": None,
"google_id": google_id,
"history": [],
}
result = await users_collection.insert_one(new_user)
user_id = result.inserted_id
else:
user_id = user["_id"]
# 4. Crear JWT de tu sistema
token = create_access_token({"user_id": str(user_id)})
# 5. Redirigir al frontend con el token
return {"message": "Login con Google exitoso", "token": token}
+4
View File
@@ -4,6 +4,10 @@ from fastapi import Depends, HTTPException, status
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
from api.database.mongodb import users_collection
from bson import ObjectId
import os
import jwt
SECRET_KEY = os.getenv("SECRET_KEY")
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
+2 -2
View File
@@ -1,5 +1,3 @@
version: "3.9"
services:
backend:
build:
@@ -11,6 +9,8 @@ services:
- ./backend:/app
depends_on:
- db
env_file:
- backend\.env
frontend:
build: