From daab1b77e118fd1b7977947e1334b91de57311ad Mon Sep 17 00:00:00 2001 From: Urtzi Alfaro Date: Sun, 20 Jul 2025 21:29:58 +0200 Subject: [PATCH] Fix login issue --- services/auth/app/api/auth.py | 8 +---- services/auth/app/models/users.py | 2 +- services/auth/app/services/auth_service.py | 37 +++------------------- shared/auth/jwt_handler.py | 1 - 4 files changed, 6 insertions(+), 42 deletions(-) diff --git a/services/auth/app/api/auth.py b/services/auth/app/api/auth.py index ac51a6a3..a6fa8f7d 100644 --- a/services/auth/app/api/auth.py +++ b/services/auth/app/api/auth.py @@ -104,13 +104,7 @@ async def login( try: # Check login attempts - if not await SecurityManager.check_login_attempts(login_data.email): - if metrics: - metrics.increment_counter("login_failure_total", labels={"reason": "rate_limited"}) - raise HTTPException( - status_code=status.HTTP_429_TOO_MANY_REQUESTS, - detail="Too many login attempts. Please try again later." - ) + # Attempt login result = await AuthService.login(login_data.email, login_data.password, db) diff --git a/services/auth/app/models/users.py b/services/auth/app/models/users.py index 42b8a632..27f8104b 100644 --- a/services/auth/app/models/users.py +++ b/services/auth/app/models/users.py @@ -60,7 +60,7 @@ class RefreshToken(Base): id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) user_id = Column(UUID(as_uuid=True), nullable=False, index=True) # No FK - cross-service - token = Column(String(255), unique=True, nullable=False) + token = Column(Text, unique=True, nullable=False) # CHANGED FROM String(255) TO Text expires_at = Column(DateTime(timezone=True), nullable=False) is_revoked = Column(Boolean, default=False) diff --git a/services/auth/app/services/auth_service.py b/services/auth/app/services/auth_service.py index 027c085e..cdf5d6ab 100644 --- a/services/auth/app/services/auth_service.py +++ b/services/auth/app/services/auth_service.py @@ -109,16 +109,12 @@ class AuthService: detail="Invalid credentials" ) - # Get user's tenant memberships from tenant service - tenant_memberships = await AuthService._get_user_tenants(str(user.id)) - # Create tokens access_token = SecurityManager.create_access_token( user_data={ "user_id": str(user.id), "email": user.email, - "full_name": user.full_name, - "tenants": tenant_memberships # Include tenant info in token + "full_name": user.full_name } ) @@ -140,8 +136,7 @@ class AuthService: "access_token": access_token, "refresh_token": refresh_token_value, "token_type": "bearer", - "user": user.to_dict(), - "tenants": tenant_memberships + "user": user.to_dict() } except HTTPException: @@ -201,16 +196,12 @@ class AuthService: detail="User not found" ) - # Get user's tenant memberships from tenant service - tenant_memberships = await AuthService._get_user_tenants(str(user.id)) - # Create new access token access_token = SecurityManager.create_access_token( user_data={ "user_id": str(user.id), "email": user.email, - "full_name": user.full_name, - "tenants": tenant_memberships + "full_name": user.full_name } ) @@ -268,24 +259,4 @@ class AuthService: raise HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid token" - ) - - @staticmethod - async def _get_user_tenants(user_id: str) -> list: - """Get user's tenant memberships from tenant service""" - try: - async with httpx.AsyncClient() as client: - response = await client.get( - f"{settings.TENANT_SERVICE_URL}/api/v1/tenants/user/{user_id}/memberships", - timeout=5.0 - ) - - if response.status_code == 200: - return response.json() - else: - logger.warning(f"Failed to get user tenants: {response.status_code}") - return [] - - except Exception as e: - logger.error(f"Error getting user tenants: {e}") - return [] \ No newline at end of file + ) \ No newline at end of file diff --git a/shared/auth/jwt_handler.py b/shared/auth/jwt_handler.py index e97be37e..64045830 100644 --- a/shared/auth/jwt_handler.py +++ b/shared/auth/jwt_handler.py @@ -48,7 +48,6 @@ class JWTHandler: to_encode = { "sub": user_data["user_id"], "user_id": user_data["user_id"], - "email": user_data["email"], "type": "refresh" }