ciyidogan commited on
Commit
09f6c98
·
verified ·
1 Parent(s): d0cf3c9

Update auth_controller.py

Browse files
Files changed (1) hide show
  1. auth_controller.py +6 -19
auth_controller.py CHANGED
@@ -1,35 +1,22 @@
1
- from fastapi import APIRouter, HTTPException, Request
2
  import bcrypt
3
- from service_config import ServiceConfig
4
- import json
5
- from log import log # ✅ eksik olan satır
6
 
7
  router = APIRouter()
8
- service_config = ServiceConfig()
9
- service_config.load()
10
 
11
  @router.post("/login")
12
- async def login(request: Request):
13
  data = await request.json()
14
  username = data.get("username")
15
  password = data.get("password")
16
- user = next((u for u in service_config.users if u["username"] == username), None)
17
  if not user:
18
- log(f"❌ User '{username}' not found.")
19
  raise HTTPException(status_code=401, detail="Invalid username or password")
20
 
21
  hashed = user["password_hash"].encode()
22
- log(f"🔑 Checking password for user '{username}' with hash '{hashed}'.")
23
-
24
- try:
25
- if not bcrypt.checkpw(password.encode(), hashed):
26
- log("❌ Password check failed.")
27
- raise HTTPException(status_code=401, detail="Invalid username or password")
28
- except Exception as e:
29
- log(f"❌ Bcrypt check failed with error: {e}")
30
- raise HTTPException(status_code=500, detail=f"Internal error during bcrypt check: {e}")
31
 
32
- log(f"✅ Login successful for user '{username}'.")
33
  return {"message": "Login successful"}
34
 
35
  @router.post("/change_password")
 
1
+ from fastapi import APIRouter, HTTPException, Request, Depends
2
  import bcrypt
3
+ from app import get_config, ServiceConfig
 
 
4
 
5
  router = APIRouter()
 
 
6
 
7
  @router.post("/login")
8
+ async def login(request: Request, config: ServiceConfig = Depends(get_config)):
9
  data = await request.json()
10
  username = data.get("username")
11
  password = data.get("password")
12
+ user = next((u for u in config.users if u["username"] == username), None)
13
  if not user:
 
14
  raise HTTPException(status_code=401, detail="Invalid username or password")
15
 
16
  hashed = user["password_hash"].encode()
17
+ if not bcrypt.checkpw(password.encode(), hashed):
18
+ raise HTTPException(status_code=401, detail="Invalid username or password")
 
 
 
 
 
 
 
19
 
 
20
  return {"message": "Login successful"}
21
 
22
  @router.post("/change_password")