update many files
This commit is contained in:
+13
-27
@@ -5,7 +5,10 @@ from datetime import datetime, timedelta
|
||||
from fastapi import Header, HTTPException
|
||||
from dotenv import load_dotenv
|
||||
import os
|
||||
from fastapi import Depends, HTTPException
|
||||
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
||||
|
||||
security = HTTPBearer()
|
||||
load_dotenv()
|
||||
|
||||
SECRET_KEY = os.getenv("SECRET_KEY")
|
||||
@@ -64,32 +67,15 @@ def verify_user(username, password):
|
||||
return None
|
||||
|
||||
|
||||
def get_current_user(authorization: str = Header(None)):
|
||||
def get_current_user(
|
||||
credentials: HTTPAuthorizationCredentials = Depends(security)
|
||||
):
|
||||
token = credentials.credentials
|
||||
|
||||
if authorization is None:
|
||||
raise HTTPException(
|
||||
status_code=401,
|
||||
detail="Missing token"
|
||||
)
|
||||
payload = jwt.decode(
|
||||
token,
|
||||
SECRET_KEY,
|
||||
algorithms=[ALGORITHM]
|
||||
)
|
||||
|
||||
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"
|
||||
)
|
||||
return payload["user_id"]
|
||||
Reference in New Issue
Block a user