update auth.py

This commit is contained in:
ben
2026-08-03 15:50:18 +02:00
parent 06438c8905
commit db16d3e201
+33 -1
View File
@@ -2,6 +2,7 @@ from database import execute, fetch_one
import bcrypt import bcrypt
from jose import jwt, JWTError from jose import jwt, JWTError
from datetime import datetime, timedelta from datetime import datetime, timedelta
from fastapi import Header, HTTPException
SECRET_KEY = "" SECRET_KEY = ""
ALGORITHM = "HS256" ALGORITHM = "HS256"
@@ -56,4 +57,35 @@ def verify_user(username, password):
): ):
return user["id"] return user["id"]
return None return None
def get_current_user(authorization: str = Header(None)):
if authorization is None:
raise HTTPException(
status_code=401,
detail="Missing token"
)
try:
scheme, token = authorization.split(" ")
if scheme.lower() != "bearer":
raise Exception()
payload = jwt.decode(
token,
SECRET_KEY,
algorithms=[ALGORITHM]
)
user_id = payload["user_id"]
return user_id
except JWTError:
raise HTTPException(
status_code=401,
detail="Invalid token"
)