feat: finish login system
- adding response based model dto - adding logic for login system
This commit is contained in:
parent
06ae096c20
commit
19b7500bb6
|
@ -11,13 +11,10 @@ class AuthController:
|
|||
|
||||
def login(self):
|
||||
data = request.get_json()
|
||||
users = self.auth_service.login(data)
|
||||
response = {
|
||||
"status": True,
|
||||
"message": "success retrive data",
|
||||
"data": users,
|
||||
}
|
||||
return jsonify(response)
|
||||
response = self.auth_service.login(data)
|
||||
if response.success:
|
||||
return jsonify(response.to_dict()), 200
|
||||
return jsonify(response.to_dict()), 400
|
||||
|
||||
def register(self):
|
||||
return jsonify({"message": "register"})
|
||||
|
|
|
@ -17,4 +17,4 @@ class Container(containers.DeclarativeContainer):
|
|||
user_service = providers.Factory(UserService, user_repository)
|
||||
|
||||
# controllers
|
||||
auth_controller = providers.Factory(AuthController, auth_service, user_service)
|
||||
auth_controller = providers.Factory(AuthController, user_service, auth_service)
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
from .dto import ApiResponse
|
|
@ -0,0 +1 @@
|
|||
from .response import ApiResponse
|
|
@ -0,0 +1,22 @@
|
|||
from pydantic import BaseModel
|
||||
from typing import Generic, TypeVar, Optional
|
||||
|
||||
T = TypeVar("T")
|
||||
|
||||
|
||||
class ApiResponse(BaseModel, Generic[T]):
|
||||
success: bool
|
||||
message: str
|
||||
data: Optional[T] = None
|
||||
|
||||
def to_json(self) -> str:
|
||||
"""
|
||||
Convert the model to a properly formatted JSON string.
|
||||
"""
|
||||
return self.model_dump_json(indent=4)
|
||||
|
||||
def to_dict(self) -> dict:
|
||||
"""
|
||||
Convert the model to a dictionary with proper key-value pairs.
|
||||
"""
|
||||
return self.model_dump()
|
|
@ -8,21 +8,16 @@ class UserRepository:
|
|||
|
||||
def get_all_users(self):
|
||||
try:
|
||||
# mongo.db.command("ping")
|
||||
# # Retrieve all users, excluding "_id"
|
||||
# users = list(self.collection.find({}, {"_id": 0}))
|
||||
# print(f"✅ All Users Data: {users}", file=sys.stderr)
|
||||
return None
|
||||
|
||||
users = list(self.collection.find({}, {"_id": 0}))
|
||||
|
||||
return users if users else []
|
||||
except Exception as e:
|
||||
print(f"❌ Error fetching users: {e}", file=sys.stderr)
|
||||
return []
|
||||
|
||||
def get_user_by_email(self, email):
|
||||
try:
|
||||
# user = mongo.db.find_one({"email": email}, {"_id": 0})
|
||||
# print(f"✅ User Data for {email}: {user}", file=sys.stderr)
|
||||
# return user if user else None
|
||||
return None
|
||||
user = self.collection.find_one({"email": email}, {"_id": 0})
|
||||
return user if user else None
|
||||
except Exception as e:
|
||||
print(f"❌ Error fetching user: {e}", file=sys.stderr)
|
||||
return None
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
from repositories import UserRepository
|
||||
from models import ApiResponse
|
||||
|
||||
|
||||
class AuthService:
|
||||
|
@ -13,13 +14,17 @@ class AuthService:
|
|||
user_data = self.user_repository.get_user_by_email(email)
|
||||
|
||||
if user_data == None:
|
||||
return None
|
||||
return ApiResponse(success=False, message="User not found", data=None)
|
||||
|
||||
if user_data["password"] == password:
|
||||
del user_data["password"]
|
||||
return user_data
|
||||
return ApiResponse(
|
||||
success=True, message="Login success", data=user_data
|
||||
)
|
||||
|
||||
return None
|
||||
return ApiResponse(success=False, message="Invalid password", data=None)
|
||||
except Exception as e:
|
||||
print(f"the issue is {e}")
|
||||
return None
|
||||
return ApiResponse(
|
||||
success=False, message="Internal server error", data=None
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue