Files
service-finder/backend/app/services/auth_service.py

82 lines
3.0 KiB
Python

from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select, text
from app.models.identity import User, Person, UserRole
from app.models.organization import Organization
from app.schemas.auth import UserLiteRegister
from app.core.security import get_password_hash, verify_password
from app.services.email_manager import email_manager # Importálva!
class AuthService:
@staticmethod
async def register_lite(db: AsyncSession, user_in: UserLiteRegister):
"""Step 1: Lite regisztráció + Email küldés."""
try:
# 1. Person shell
new_person = Person(
first_name=user_in.first_name,
last_name=user_in.last_name,
is_active=False
)
db.add(new_person)
await db.flush()
# 2. User fiók
new_user = User(
email=user_in.email,
hashed_password=get_password_hash(user_in.password),
person_id=new_person.id,
role=UserRole.user,
is_active=False,
region_code=user_in.region_code
)
db.add(new_user)
await db.flush()
# 3. Email kiküldése (Mester Könyv v1.4 szerint)
try:
await email_manager.send_email(
recipient=user_in.email,
template_key="registration", # 'registration.html' sablon használata
variables={
"first_name": user_in.first_name,
"login_url": "http://192.168.100.10:3000/login"
},
user_id=new_user.id
)
except Exception as email_err:
# Az email hiba nem állítja meg a regisztrációt, csak logoljuk
print(f"Email hiba regisztrációkor: {str(email_err)}")
await db.commit()
await db.refresh(new_user)
return new_user
except Exception as e:
await db.rollback()
raise e
@staticmethod
async def authenticate(db: AsyncSession, email: str, password: str):
stmt = select(User).where(User.email == email, User.is_deleted == False)
res = await db.execute(stmt)
user = res.scalar_one_or_none()
if not user or not user.hashed_password or not verify_password(password, user.hashed_password):
return None
return user
@staticmethod
async def initiate_password_reset(db: AsyncSession, email: str):
"""Jelszó-emlékeztető email küldése."""
stmt = select(User).where(User.email == email, User.is_deleted == False)
res = await db.execute(stmt)
user = res.scalar_one_or_none()
if user:
await email_manager.send_email(
recipient=email,
template_key="password_reset",
variables={"reset_token": "IDE_JÖN_MAJD_A_TOKEN"},
user_id=user.id
)
return True
return False