diff --git a/backend/app/routers/auth.py b/backend/app/routers/auth.py index 4e1e4ca..bee1061 100644 --- a/backend/app/routers/auth.py +++ b/backend/app/routers/auth.py @@ -1,7 +1,8 @@ -from fastapi import Depends, HTTPException, status +from fastapi import Depends, HTTPException, status, Request from fastapi.routing import APIRouter from fastapi.encoders import jsonable_encoder from fastapi.security import OAuth2PasswordBearer +from fastapi.responses import RedirectResponse from supabase import Client from app.config import settings from jose import JWTError, jwt @@ -59,7 +60,7 @@ async def login_with_google(supabase: Client = Depends(get_supabase)): response = supabase.auth.sign_in_with_oauth({ "provider": "google", "options": { - "redirect_to": "https://mhcafqvzbrrwvahpvvzd.supabase.co/auth/v1/callback" + "redirect_to": "http://localhost:8000/auth/callback" } }) return {"auth_url": response.url} @@ -69,6 +70,14 @@ async def login_with_google(supabase: Client = Depends(get_supabase)): detail=str(e) ) +@router.get("/callback") +async def google_callback(request: Request, supabase: Client = Depends(get_supabase)): + code = request.query_params.get("code") + if not code: + raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Missing authorization code") + + supabase.auth.exchange_code_for_session({"auth_code": code}) + return RedirectResponse(url="http://localhost:5173") @router.post("/logout") async def logout(user=Depends(get_current_user_required), supabase: Client = Depends(get_supabase)): diff --git a/backend/app/routers/helpers.py b/backend/app/routers/helpers.py index f9c2c34..ea75bd0 100644 --- a/backend/app/routers/helpers.py +++ b/backend/app/routers/helpers.py @@ -3,26 +3,13 @@ from fastapi.security import OAuth2PasswordBearer from typing import Optional from supabase import Client from app.config import settings - +from supabase import create_client oauth2_scheme = OAuth2PasswordBearer(tokenUrl="auth/login", auto_error=False) +supabase_client = create_client(settings.supabase_url, settings.supabase_key) + def get_supabase() -> Client: - from supabase import create_client - - # Temporary hardcoded values - import os - - - # Access environment variables - # Debugging purpose - - url = settings.supabase_url - key = settings.supabase_key # From Supabase dashboard - - # print("[HARDCODED] URL:", url) - # print("[HARDCODED] Key:", key[:10] + "...") - - return create_client(url, key) + return supabase_client # Updated current user dependency async def get_user_from_token(