First we need access to our database in order to get the ID
from sqlalchemy.orm import Sessionfrom.import schemas, databasefrom fastapi.security import OAuth2PasswordBearer# Now we can make requests to the Databasedefverify_access_token(token:str,credentials_exception):try: payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])id:str= payload.get("user_id")ifstr(id)isNone:raise credentials_exception token_data = schemas.TokenData(id=id)except JWTError:raise credentials_exceptionreturn token_datadefget_current_user(token:str=Depends(oath2_scheme),db: Session =Depends(database.get_db)): credentials_exception =HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail=f"Could not validate cedentials", headers={"WWW-Authenticate": "Bearer"})# Fetch current user token =verify_access_token(token, credentials_exception) user = db.query(UserModels.User).filter(UserModels.User.id == token.id).first()return user
Next step is to update the user_id in the posts.py file