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): def authenticate_employee(db: Session, last_name: str, password: str):
employee = ( employee = db.query(tables.Employee).filter(tables.Employee.last_name == last_name).first()
db.query(tables.Employee).filter(tables.Employee.last_name == last_name).first()
)
if not employee: if not employee:
return None return None
if not verify_password(password, employee.password): if not verify_password(password, employee.password):
return None 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): def authenticate_admin(db: Session, username: str, password: str):

View File

@@ -36,20 +36,26 @@ app.add_middleware(
) )
# Auth endpoints # 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") @app.post("/api/auth/login")
def login(credentials: dict, db: Session = Depends(get_db)): def login(credentials: dict, db: Session = Depends(get_db)):
employee = auth.authenticate_employee( print(f"Login attempt for: {credentials['lastName']}") # Добавьте для отладки
db, credentials["lastName"], credentials["password"] employee = auth.authenticate_employee(db, credentials["lastName"], credentials["password"])
)
if not employee: if not employee:
raise HTTPException(status_code=401, detail="Invalid credentials") raise HTTPException(
return { status_code=401,
"id": employee.id, detail="Неверная фамилия или пароль"
"firstName": employee.first_name, )
"lastName": employee.last_name, return employee
"department": employee.department,
"createdAt": employee.created_at,
}
@app.post("/api/auth/admin") @app.post("/api/auth/admin")