Returning schemas.User from get_current_user

This commit is contained in:
DmitryGantimurov 2023-07-31 23:55:16 +03:00
parent 29d46be492
commit 0145ed8f44
3 changed files with 9 additions and 5 deletions

View File

@ -7,7 +7,7 @@ from fastapi.templating import Jinja2Templates
from fastapi.requests import Request from fastapi.requests import Request
from pydantic import json from pydantic import json
from typing import Any
from starlette.staticfiles import StaticFiles from starlette.staticfiles import StaticFiles
import requests import requests
@ -168,8 +168,11 @@ async def login_for_access_token(
return {"access_token":access_token} return {"access_token":access_token}
@app.get("/api/users/me/", response_model=None) # # @app.get("/api/users/me/", response_model=schemas.User) #
async def read_users_me(current_user: Depends(get_current_active_user)): #Annotated[schemas.User, Depends(get_current_active_user)]): # async def read_users_me(current_user: Annotated[schemas.User, Depends(get_current_active_user)]):
# return current_user #schemas.User(id=current_user.id, email=current_user.email, name=current_user.name, surname=current_user.surname, disabled=current_user.disabled, items=current_user.items)
@app.get("/api/users/me/", response_model=schemas.User) #
async def read_users_me(current_user: Annotated[schemas.User, Depends(get_current_active_user)]) -> Any:
return current_user #schemas.User(id=current_user.id, email=current_user.email, name=current_user.name, surname=current_user.surname, disabled=current_user.disabled, items=current_user.items) return current_user #schemas.User(id=current_user.id, email=current_user.email, name=current_user.name, surname=current_user.surname, disabled=current_user.disabled, items=current_user.items)

View File

@ -45,6 +45,7 @@ class User(BaseModel):
class Config: class Config:
orm_mode = True orm_mode = True
arbitrary_types_allowed=True
class UserInDB(User): class UserInDB(User):
hashed_password: str hashed_password: str

View File

@ -30,7 +30,6 @@ def get_password_hash(password):
return pwd_context.hash(password) return pwd_context.hash(password)
# проблема здесь
def get_user(db: Session, email: str): def get_user(db: Session, email: str):
user_with_required_email = db.query(models.User).filter(models.User.email == email).first() user_with_required_email = db.query(models.User).filter(models.User.email == email).first()
if user_with_required_email: if user_with_required_email:
@ -75,7 +74,8 @@ async def get_current_user(db: Session, token: Annotated[str, Depends(oauth2_sch
user = get_user(db, email=token_data.email) user = get_user(db, email=token_data.email)
if user is None: if user is None:
raise credentials_exception raise credentials_exception
return user return schemas.User(id=user.id, email=user.email, name=user.name, surname=user.surname, disabled=user.disabled, items=user.items)
# return user
async def get_current_active_user( async def get_current_active_user(