generated from pptx704/fastapi-template
Added recruitment task
This commit is contained in:
@ -1,9 +1,8 @@
|
||||
import os
|
||||
from datetime import UTC, datetime, timedelta
|
||||
|
||||
import jwt
|
||||
from bcrypt import checkpw, gensalt, hashpw
|
||||
from fastapi import HTTPException, Security
|
||||
from fastapi import Depends, HTTPException, Security
|
||||
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
|
||||
|
||||
from app import schemas
|
||||
@ -25,7 +24,7 @@ def verify_password(plain_password: str, hashed_password: str) -> bool:
|
||||
def create_jwt_token(data: dict) -> str:
|
||||
_ed = timedelta(minutes=settings.JWT_EXPIRE_MINUTES)
|
||||
iat = datetime.now(UTC)
|
||||
exp = datetime.now(UTC) + _ed
|
||||
exp = iat + _ed
|
||||
token_payload = data
|
||||
token_payload.update({"iat": iat, "exp": exp})
|
||||
|
||||
@ -50,8 +49,18 @@ def get_user_from_token(token: str) -> schemas.User:
|
||||
|
||||
user_id = payload.get("user_id")
|
||||
|
||||
# Return user from database
|
||||
...
|
||||
if user_id is None:
|
||||
raise HTTPException(
|
||||
status_code=401, detail="Invalid authentication credentials"
|
||||
)
|
||||
|
||||
with SessionLocal() as db:
|
||||
user = db.query(User).filter(User.id == user_id).first()
|
||||
|
||||
if user is None:
|
||||
raise HTTPException(status_code=404, detail="User not found")
|
||||
|
||||
return schemas.User.model_validate(user)
|
||||
|
||||
|
||||
def get_user(
|
||||
@ -64,3 +73,10 @@ def get_user(
|
||||
|
||||
token = authorization.credentials
|
||||
return get_user_from_token(token)
|
||||
|
||||
|
||||
def get_user_strict(user: schemas.User = Depends(get_user)) -> schemas.User:
|
||||
if not user.is_verified:
|
||||
raise HTTPException(status_code=401, detail="User not verified")
|
||||
|
||||
return user
|
||||
|
||||
Reference in New Issue
Block a user