1
0
mirror of https://gitlab.com/MoonTestUse1/AdministrationItDepartmens.git synced 2025-08-14 00:25:46 +02:00
Files
AdministrationItDepartmens/backend/app/routers/auth.py
2025-01-03 02:22:24 +06:00

50 lines
1.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""Authentication router"""
from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session
from ..database import get_db
from ..models.employee import Employee
from ..schemas.auth import AdminLogin, EmployeeLogin
from passlib.context import CryptContext
router = APIRouter()
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
@router.post("/admin/login")
def admin_login(login_data: AdminLogin, db: Session = Depends(get_db)):
"""Admin login endpoint"""
if login_data.username == "admin" and login_data.password == "admin123":
return {
"access_token": "admin_token",
"token_type": "bearer"
}
raise HTTPException(status_code=401, detail="Invalid credentials")
@router.post("/login")
def employee_login(login_data: EmployeeLogin, db: Session = Depends(get_db)):
"""Employee login endpoint"""
# Ищем сотрудника по фамилии
employee = db.query(Employee).filter(Employee.last_name == login_data.last_name).first()
if not employee:
raise HTTPException(
status_code=401,
detail="Сотрудник с такой фамилией не найден"
)
# Проверяем пароль
if not pwd_context.verify(login_data.password, employee.password):
raise HTTPException(
status_code=401,
detail="Неверный пароль"
)
# Возвращаем данные сотрудника
return {
"id": employee.id,
"first_name": employee.first_name,
"last_name": employee.last_name,
"department": employee.department,
"office": employee.office,
"access_token": f"employee_token_{employee.id}", # Добавляем токен для авторизации
"token_type": "bearer"
}