1
0
mirror of https://gitlab.com/MoonTestUse1/AdministrationItDepartmens.git synced 2025-08-14 00:25:46 +02:00
This commit is contained in:
MoonTestUse1
2024-12-24 20:06:57 +06:00
parent b34ca1c2ce
commit 8a5fdbd674
2 changed files with 26 additions and 15 deletions

View File

@@ -4,14 +4,19 @@ from ..utils.auth import verify_password
def authenticate_employee(db: Session, last_name: str, password: str):
employee = (
db.query(tables.Employee).filter(tables.Employee.last_name == last_name).first()
)
employee = db.query(tables.Employee).filter(tables.Employee.last_name == last_name).first()
if not employee:
return None
if not verify_password(password, employee.password):
return None
return employee
return {
"id": employee.id,
"firstName": employee.first_name,
"lastName": employee.last_name,
"department": employee.department,
"office": employee.office,
"createdAt": employee.created_at
}
def authenticate_admin(db: Session, username: str, password: str):

View File

@@ -36,20 +36,26 @@ app.add_middleware(
)
# Auth endpoints
@app.post("/api/test/create-user")
def create_test_user(db: Session = Depends(get_db)):
test_user = employee_models.EmployeeCreate(
first_name="Test",
last_name="User",
department="general",
office="101",
password="test123"
)
return employees.create_employee(db=db, employee=test_user)
@app.post("/api/auth/login")
def login(credentials: dict, db: Session = Depends(get_db)):
employee = auth.authenticate_employee(
db, credentials["lastName"], credentials["password"]
)
print(f"Login attempt for: {credentials['lastName']}") # Добавьте для отладки
employee = auth.authenticate_employee(db, credentials["lastName"], credentials["password"])
if not employee:
raise HTTPException(status_code=401, detail="Invalid credentials")
return {
"id": employee.id,
"firstName": employee.first_name,
"lastName": employee.last_name,
"department": employee.department,
"createdAt": employee.created_at,
}
raise HTTPException(
status_code=401,
detail="Неверная фамилия или пароль"
)
return employee
@app.post("/api/auth/admin")