update auth.py

This commit is contained in:
ben
2026-08-03 15:50:18 +02:00
parent 06438c8905
commit db16d3e201
+32
View File
@@ -2,6 +2,7 @@ from database import execute, fetch_one
import bcrypt
from jose import jwt, JWTError
from datetime import datetime, timedelta
from fastapi import Header, HTTPException
SECRET_KEY = ""
ALGORITHM = "HS256"
@@ -57,3 +58,34 @@ def verify_user(username, password):
return user["id"]
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"
)