mirror of
https://github.com/kevin-DL/fast_api_api_template.git
synced 2026-01-11 17:54:35 +00:00
Register
Login Basic migrations with alembic Get items
This commit is contained in:
56
deps.py
Normal file
56
deps.py
Normal 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
|
||||
Reference in New Issue
Block a user