ready login endpoint

This commit is contained in:
IluaAir
2025-07-14 12:41:03 +03:00
parent 19a9b36173
commit d530412805
5 changed files with 44 additions and 10 deletions

View File

@@ -1,11 +1,14 @@
from src.schemas.users import UserRequestADD, User, UserAdd
from fastapi import HTTPException
from src.schemas.auth import Token
from src.schemas.users import UserRequestADD, User, UserAdd, UserWithHashedPass
from src.services.base import BaseService
from src.core.auth_manager import AuthManger
from src.core.auth_manager import AuthManager
class AuthService(BaseService):
async def registration(self, cred: UserRequestADD) -> User:
hashed_pass = AuthManger.get_password_hash(cred.password)
hashed_pass = AuthManager.get_password_hash(cred.password)
user_to_insert = UserAdd(
username=cred.username,
email=cred.email,
@@ -18,4 +21,20 @@ class AuthService(BaseService):
return User.model_validate(result)
async def login(self, username: str, password: str):
...
result = await self.session.user.get_one_or_none(username=username)
if result is None:
raise HTTPException(
status_code=401,
detail="Incorrect username or password",
)
user = UserWithHashedPass.model_validate(result)
verify = AuthManager.verify_password(
plain_password=password, hashed_password=user.hashed_password
)
if not verify:
raise HTTPException(
status_code=401,
detail="Incorrect username or password",
)
access_token = AuthManager.create_access_token(data={"sub": user.username})
return Token(access_token=access_token, token_type="bearer")