diff --git a/backend/app/api/auth.py b/backend/app/api/auth.py index 1abdeb8f..bfb712e2 100644 --- a/backend/app/api/auth.py +++ b/backend/app/api/auth.py @@ -163,11 +163,15 @@ async def google_callback( redirect_url = f"{frontend_url}/auth/callback?token={jwt_token}" return RedirectResponse(url=redirect_url) +from pydantic import BaseModel + +class TokenExchangeRequest(BaseModel): + code: str + redirect_uri: str @router.post("/google/token") async def exchange_google_token( - code: str, - redirect_uri: str, + request: TokenExchangeRequest, db: AsyncSession = Depends(get_db) ): """ @@ -185,10 +189,10 @@ async def exchange_google_token( token_response = await client.post( GOOGLE_TOKEN_URL, data={ - "code": code, + "code": request.code, "client_id": client_id, "client_secret": client_secret, - "redirect_uri": redirect_uri, + "redirect_uri": request.redirect_uri, "grant_type": "authorization_code", } ) diff --git a/frontend/app/auth/callback/page.tsx b/frontend/app/auth/callback/page.tsx index 03dc8731..f4d740a7 100644 --- a/frontend/app/auth/callback/page.tsx +++ b/frontend/app/auth/callback/page.tsx @@ -51,7 +51,14 @@ function AuthCallbackContent() { if (!response.ok) { const errorData = await response.json(); - throw new Error(errorData.detail || "Failed to exchange token"); + // Handle various error response formats + const errorMessage = + typeof errorData.detail === 'string' ? errorData.detail : + typeof errorData.error === 'string' ? errorData.error : + errorData.message || + JSON.stringify(errorData) || + "Failed to exchange token"; + throw new Error(errorMessage); } const data = await response.json(); @@ -59,7 +66,8 @@ function AuthCallbackContent() { router.replace("/"); } catch (err: any) { console.error("Auth callback error:", err); - setError(err.message || "Authentication failed"); + const msg = err.message || "Authentication failed"; + setError(typeof msg === 'string' ? msg : "Authentication failed"); setIsProcessing(false); } };