update usage of api for login by email or username

This commit is contained in:
Thomas Peetz
2026-04-15 11:32:18 +02:00
parent 7aa6a12044
commit 62e22c56c4
3 changed files with 28 additions and 10 deletions
+22 -4
View File
@@ -13,7 +13,7 @@ from pydantic import ValidationError
from src.core.config import settings
from src.core.log_conf import logger
from src.db.models.admin import Profile
from src.db.repository.admin import get_profile_by_username, is_database_empty
from src.db.repository.admin import get_profile_by_username, get_profile_by_email, is_database_empty
from src.db.session import SessionLocal
from src.schema.admin import ProfileModel, TokenData
@@ -51,10 +51,28 @@ oauth2_scheme = OAuth2PasswordBearer(
# return None
# return param
def authenticate_user(username: str, password: str) -> Optional[Profile]:
def authenticate_user_by_email(email: str, password: str) -> Optional[Profile]:
with SessionLocal() as db:
user = get_profile(username=username, db=db)
user = get_profile_by_email(email=email, db=db)
logger.debug(user)
if not user:
if is_database_empty(db):
logger.info("database is empty, use temporary access")
user = Profile()
user.email = "init_user@thpeetz.de"
return user
return None
else:
if bcrypt.checkpw(password.encode(), user.password.encode()):
logger.info("User successful authenticated")
else:
logger.info("Authentication failed!")
return user
def authenticate_user_by_username(username: str, password: str) -> Optional[Profile]:
with SessionLocal() as db:
user = get_profile_by_username(username=username, db=db)
logger.debug(user)
if not user:
if is_database_empty(db):