Login
Basic migrations with alembic
Get items
This commit is contained in:
2023-02-19 02:47:14 +00:00
parent c807fbcf03
commit 6700a4e2ef
25 changed files with 616 additions and 9 deletions

56
deps.py Normal file
View File

@@ -0,0 +1,56 @@
from fastapi import Depends, HTTPException, status
from fastapi.security import OAuth2PasswordBearer
from jose import jwt, JWTError
from pydantic import BaseModel
from sqlalchemy.orm import Session
from api.config import settings
from crud.users import get_user
from api.database import SessionLocal
from schemas.users import User
class Token(BaseModel):
access_token: str
token_type: str
class TokenData(BaseModel):
sub: str | None = None
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
async def get_current_user(db: Session = Depends(get_db), token: str = Depends(oauth2_scheme)):
credentials_exception = HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Could not validate credentials",
headers={"WWW-Authenticate": "Bearer"},
)
try:
payload = jwt.decode(token, settings.secret_key, algorithms=[settings.algorithm])
sub: str = payload.get("sub")
if sub is None:
raise credentials_exception
token_data = TokenData(sub=sub)
except JWTError:
raise credentials_exception
user = get_user(db, user_id=token_data.sub)
if user is None:
raise credentials_exception
return user
async def get_current_active_user(current_user: User = Depends(get_current_user)):
if not current_user.is_active:
raise HTTPException(status_code=400, detail="Inactive user")
return current_user