I was wondering if it was possible to pass the results from the dependencies
kwarg in include_router
to the router that is passed to it. What I want to do is decode a JWT from the x-token
header of a request and pass the decoded payload to the books
routes.
I know that I could just write authenticate_and_decode_JWT
as a dependency of each of the routes in routers/book.py, but this would be quite repetitive for a large app.
main.py
from typing import Optional
from jose import jwt
from fastapi import FastAPI, Depends, Header, HTTPException, status
from jose.exceptions import JWTError
from routers import books
app = FastAPI()
def authenticate_and_decode_JWT(x_token: str = Header(None)):
try:
payload = jwt.decode(x_token.split(' ')[1], 'secret key', algorithms=['HS256'])
return payload # pass decoded user information from here to books.router routes somehow
except JWTError:
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED)
app.include_router(
books.router,
prefix="/books",
dependencies=[Depends(authenticate_and_decode_JWT)],
)
@app.get("/")
def read_root():
return {"Hello": "World"}
routers/books.py
from fastapi import APIRouter
router = APIRouter()
@router.get('/')
def get_book():
# do stuff with decoded authenticated user data from JWT payload here
pass
@router.post('/')
def create_book():
# do stuff with decoded authenticated user data from JWT payload here
pass