fix: login with id from mongo
This commit is contained in:
parent
7f5479188e
commit
bc5094ed7d
|
@ -13,13 +13,27 @@ class AuthController:
|
||||||
self.auth_service = authService
|
self.auth_service = authService
|
||||||
|
|
||||||
def login(self):
|
def login(self):
|
||||||
data = request.get_json()
|
try:
|
||||||
dataSchema = LoginSchema(**data)
|
data = request.get_json()
|
||||||
response = self.auth_service.login(dataSchema)
|
dataSchema = LoginSchema(**data)
|
||||||
|
response = self.auth_service.login(dataSchema)
|
||||||
|
|
||||||
if response.success:
|
if response.success:
|
||||||
return jsonify(response.to_dict()), 200
|
return jsonify({}), 200
|
||||||
return jsonify(response.to_dict()), 400
|
return jsonify({}), 400
|
||||||
|
except ValidationError as e:
|
||||||
|
current_app.logger.error(f"Validation error: {e}")
|
||||||
|
response = ResponseSchema(message="Invalid input", data=None, meta=None)
|
||||||
|
return jsonify(response.model_dump()), 400
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
current_app.logger.error(
|
||||||
|
f"Error during Google login: {str(e)}", exc_info=True
|
||||||
|
)
|
||||||
|
response = ResponseSchema(
|
||||||
|
message="Internal server error", data=None, meta=None
|
||||||
|
)
|
||||||
|
return jsonify(response.model_dump()), 500
|
||||||
|
|
||||||
def google_login(self):
|
def google_login(self):
|
||||||
"""Handles Google Login via ID Token verification"""
|
"""Handles Google Login via ID Token verification"""
|
||||||
|
|
|
@ -9,7 +9,6 @@ class UserMapper:
|
||||||
google_id: str, email: str, payload: Dict[str, Optional[str]]
|
google_id: str, email: str, payload: Dict[str, Optional[str]]
|
||||||
) -> UserEntity:
|
) -> UserEntity:
|
||||||
return UserEntity(
|
return UserEntity(
|
||||||
id=str(google_id),
|
|
||||||
google_id=google_id,
|
google_id=google_id,
|
||||||
email=email,
|
email=email,
|
||||||
name=payload.get("name"),
|
name=payload.get("name"),
|
||||||
|
|
|
@ -1,3 +1,7 @@
|
||||||
from .user_entity import UserEntity
|
from .user_entity import UserEntity
|
||||||
|
from .base import PyObjectId
|
||||||
|
|
||||||
__all__ = ["UserEntity"]
|
__all__ = [
|
||||||
|
"UserEntity",
|
||||||
|
"PyObjectId",
|
||||||
|
]
|
||||||
|
|
|
@ -0,0 +1,19 @@
|
||||||
|
from bson import ObjectId
|
||||||
|
|
||||||
|
|
||||||
|
class PyObjectId(ObjectId):
|
||||||
|
"""Custom ObjectId type for Pydantic to handle MongoDB _id"""
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def __get_validators__(cls):
|
||||||
|
yield cls.validate
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def validate(cls, v):
|
||||||
|
if not ObjectId.is_valid(v):
|
||||||
|
raise ValueError("Invalid ObjectId")
|
||||||
|
return ObjectId(v)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def __modify_schema__(cls, field_schema):
|
||||||
|
field_schema.update(type="string")
|
|
@ -1,10 +1,11 @@
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
from pydantic import BaseModel, EmailStr
|
from pydantic import BaseModel, EmailStr
|
||||||
from datetime import date, datetime
|
from datetime import date, datetime
|
||||||
|
from .base import PyObjectId
|
||||||
|
|
||||||
|
|
||||||
class UserEntity(BaseModel):
|
class UserEntity(BaseModel):
|
||||||
id: str
|
_id: Optional[PyObjectId] = None
|
||||||
google_id: Optional[str] = None
|
google_id: Optional[str] = None
|
||||||
email: EmailStr
|
email: EmailStr
|
||||||
password: Optional[str] = None
|
password: Optional[str] = None
|
||||||
|
|
|
@ -27,7 +27,6 @@ class AuthService:
|
||||||
|
|
||||||
existing_user = self.user_repository.get_by_google_id(google_id)
|
existing_user = self.user_repository.get_by_google_id(google_id)
|
||||||
if existing_user:
|
if existing_user:
|
||||||
current_app.logger.info(f"User {existing_user.email} already exists ")
|
|
||||||
if existing_user.email == email:
|
if existing_user.email == email:
|
||||||
return existing_user
|
return existing_user
|
||||||
return AuthException("Email not match")
|
return AuthException("Email not match")
|
||||||
|
@ -39,25 +38,13 @@ class AuthService:
|
||||||
return self.user_repository.get_user_by_id(user_id=user_id)
|
return self.user_repository.get_user_by_id(user_id=user_id)
|
||||||
|
|
||||||
def login(self, data: LoginSchema):
|
def login(self, data: LoginSchema):
|
||||||
try:
|
|
||||||
|
|
||||||
user_data = self.user_repository.get_user_by_email(data.email)
|
user_data = self.user_repository.get_user_by_email(data.email)
|
||||||
|
|
||||||
if user_data == None:
|
if user_data == None:
|
||||||
# return ApiResponse(success=False, message="User not found", data=None)
|
|
||||||
return None
|
|
||||||
|
|
||||||
if user_data["password"] == data.password:
|
|
||||||
del user_data["password"]
|
|
||||||
# return ApiResponse(
|
|
||||||
# success=True, message="Login success", data=user_data
|
|
||||||
# )
|
|
||||||
return None
|
|
||||||
# return ApiResponse(success=False, message="Invalid password", data=None)
|
|
||||||
return None
|
|
||||||
except Exception as e:
|
|
||||||
print(f"the issue is {e}")
|
|
||||||
# return ApiResponse(
|
|
||||||
# success=False, message="Internal server error", data=None
|
|
||||||
# )
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
if user_data.password == data.password:
|
||||||
|
del user_data.password
|
||||||
|
return user_data
|
||||||
|
return None
|
||||||
|
|
|
@ -1,2 +0,0 @@
|
||||||
2025-03-19 11:45:54,493 - INFO - Logger has been initialized for Flask application.
|
|
||||||
2025-03-19 11:46:06,381 - INFO - Logger has been initialized for Flask application.
|
|
Loading…
Reference in New Issue