mirror of
https://github.com/a-mayb3/Kanban_clone_backend.git
synced 2026-03-21 18:15:37 +01:00
moved token cheking to auth module
This commit is contained in:
parent
eccb3b35b4
commit
057797c07d
2 changed files with 33 additions and 33 deletions
|
|
@ -27,6 +27,38 @@ def create_access_token(data: dict, expires_delta: timedelta | None = None):
|
||||||
encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
|
encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
|
||||||
return encoded_jwt
|
return encoded_jwt
|
||||||
|
|
||||||
|
def check_for_valid_token(request: Request, db: db_dependency) -> models.User :
|
||||||
|
"""Helper function to check for valid JWT token in cookies"""
|
||||||
|
token = request.cookies.get("access_token")
|
||||||
|
if not token:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=401,
|
||||||
|
detail="Not logged in"
|
||||||
|
)
|
||||||
|
try:
|
||||||
|
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
|
||||||
|
user_id: str = str(payload.get("sub"))
|
||||||
|
if user_id is None:
|
||||||
|
request.cookies.clear() ## removing invalid auth cookie
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=401,
|
||||||
|
detail="Not logged in"
|
||||||
|
)
|
||||||
|
db_user = db.query(models.User).filter(models.User.id == int(user_id)).first()
|
||||||
|
if db_user is None:
|
||||||
|
request.cookies.clear() ## removing invalid auth cookie
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=401,
|
||||||
|
detail="User not found"
|
||||||
|
)
|
||||||
|
return db_user
|
||||||
|
|
||||||
|
except JWTError:
|
||||||
|
request.cookies.clear() ## removing invalid auth cookie
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=401,
|
||||||
|
detail="Could not validate credentials"
|
||||||
|
)
|
||||||
|
|
||||||
@router.post("/login")
|
@router.post("/login")
|
||||||
def login(user_data: user_schemas.UserLogin, request: Request, response: Response, db: db_dependency):
|
def login(user_data: user_schemas.UserLogin, request: Request, response: Response, db: db_dependency):
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@ import models
|
||||||
from routers import auth
|
from routers import auth
|
||||||
import schemas.users as users
|
import schemas.users as users
|
||||||
import schemas.projects as projects
|
import schemas.projects as projects
|
||||||
|
from routers.auth import check_for_valid_token
|
||||||
|
|
||||||
from pyargon2 import hash
|
from pyargon2 import hash
|
||||||
|
|
||||||
|
|
@ -72,37 +73,4 @@ def delete_user(user_id: int, db: db_dependency):
|
||||||
return {"detail": "User deleted"}
|
return {"detail": "User deleted"}
|
||||||
|
|
||||||
|
|
||||||
def check_for_valid_token(request: Request, db: db_dependency) -> models.User :
|
|
||||||
"""Helper function to check for valid JWT token in cookies"""
|
|
||||||
token = request.cookies.get("access_token")
|
|
||||||
if not token:
|
|
||||||
raise HTTPException(
|
|
||||||
status_code=401,
|
|
||||||
detail="Not logged in"
|
|
||||||
)
|
|
||||||
try:
|
|
||||||
payload = jwt.decode(token, auth.SECRET_KEY, algorithms=[auth.ALGORITHM])
|
|
||||||
user_id: str = str(payload.get("sub"))
|
|
||||||
if user_id is None:
|
|
||||||
request.cookies.clear() ## removing invalid auth cookie
|
|
||||||
raise HTTPException(
|
|
||||||
status_code=401,
|
|
||||||
detail="Not logged in"
|
|
||||||
)
|
|
||||||
db_user = db.query(models.User).filter(models.User.id == int(user_id)).first()
|
|
||||||
if db_user is None:
|
|
||||||
request.cookies.clear() ## removing invalid auth cookie
|
|
||||||
raise HTTPException(
|
|
||||||
status_code=401,
|
|
||||||
detail="User not found"
|
|
||||||
)
|
|
||||||
return db_user
|
|
||||||
|
|
||||||
except JWTError:
|
|
||||||
request.cookies.clear() ## removing invalid auth cookie
|
|
||||||
raise HTTPException(
|
|
||||||
status_code=401,
|
|
||||||
detail="Could not validate credentials"
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue