изменил выдачу токена и починил сваггер авторизации
This commit is contained in:
parent
0a9cc6bf4e
commit
ad76dac42a
@ -1,3 +1,5 @@
|
||||
from typing import Optional
|
||||
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy.future import select
|
||||
from sqlalchemy.orm import joinedload
|
||||
@ -14,12 +16,12 @@ class UsersRepository:
|
||||
result = await self.db.execute(stmt)
|
||||
return result.scalars().all()
|
||||
|
||||
async def get_by_id(self, user_id: int):
|
||||
async def get_by_id(self, user_id: int) -> Optional[User]:
|
||||
stmt = select(User).filter(User.id == user_id)
|
||||
result = await self.db.execute(stmt)
|
||||
return result.scalars().first()
|
||||
|
||||
async def get_by_login(self, user_login: str):
|
||||
async def get_by_login(self, user_login: str) -> Optional[User]:
|
||||
stmt = (
|
||||
select(User)
|
||||
.filter(User.login == user_login)
|
||||
|
||||
@ -1,8 +1,9 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException, Response, status
|
||||
from fastapi import APIRouter, Depends, HTTPException, Response
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.database.session import get_db
|
||||
from app.domain.entities.auth import AuthEntity
|
||||
from app.domain.entities.token_entity import TokenEntity
|
||||
from app.infrastructure.auth_service import AuthService
|
||||
|
||||
router = APIRouter()
|
||||
@ -10,7 +11,7 @@ router = APIRouter()
|
||||
|
||||
@router.post(
|
||||
"/login/",
|
||||
response_model=dict,
|
||||
response_model=TokenEntity,
|
||||
responses={401: {"description": "Invalid username or password"}},
|
||||
summary="User authentication",
|
||||
description="Logs in the user and outputs the `access_token` in the `cookie'",
|
||||
@ -22,23 +23,19 @@ async def auth_user(
|
||||
):
|
||||
auth_service = AuthService(db)
|
||||
|
||||
check = await auth_service.authenticate_user(
|
||||
login=user_data.login, password=user_data.password
|
||||
)
|
||||
token = await auth_service.authenticate_user(user_data.login, user_data.password)
|
||||
|
||||
if check is None:
|
||||
if token is None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="Invalid username or password",
|
||||
status_code=401,
|
||||
detail="Incorrect username or password"
|
||||
)
|
||||
|
||||
access_token = auth_service.create_access_token({"sub": str(check.id)})
|
||||
|
||||
response.set_cookie(
|
||||
key="users_access_token",
|
||||
value=access_token,
|
||||
value=token["access_token"],
|
||||
httponly=True,
|
||||
samesite="Lax",
|
||||
)
|
||||
|
||||
return {"access_token": access_token, "refresh_token": None}
|
||||
return token
|
||||
|
||||
@ -1,10 +1,9 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException, Response, status
|
||||
from fastapi import APIRouter, Depends
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.database.session import get_db
|
||||
from app.domain.entities.register import RegisterEntity
|
||||
from app.infrastructure.auth_service import AuthService
|
||||
from app.infrastructure.user_service import UserService
|
||||
from app.infrastructure.users_service import UsersService
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@ -19,6 +18,6 @@ async def register_user(
|
||||
user_data: RegisterEntity,
|
||||
db: AsyncSession = Depends(get_db)
|
||||
):
|
||||
user_service = UserService(db)
|
||||
user_service = UsersService(db)
|
||||
user = await user_service.register_user(user_data)
|
||||
return user.model_dump()
|
||||
|
||||
11
api/app/domain/entities/token_entity.py
Normal file
11
api/app/domain/entities/token_entity.py
Normal file
@ -0,0 +1,11 @@
|
||||
from typing import Optional
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class TokenEntity(BaseModel):
|
||||
access_token: str
|
||||
user_id: int
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
@ -1,4 +1,5 @@
|
||||
import datetime
|
||||
from typing import Optional
|
||||
|
||||
import jwt
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
@ -9,18 +10,18 @@ from app.settings import get_auth_data
|
||||
|
||||
class AuthService:
|
||||
def __init__(self, db: AsyncSession):
|
||||
self.user_repository = UsersRepository(db)
|
||||
self.users_repository = UsersRepository(db)
|
||||
|
||||
async def authenticate_user(self, login: str, password: str):
|
||||
user = await self.user_repository.get_by_login(login)
|
||||
async def authenticate_user(self, login: str, password: str) -> Optional[dict]:
|
||||
user = await self.users_repository.get_by_login(login)
|
||||
if user and user.check_password(password):
|
||||
access_token = self.create_access_token({"user_id": user.id})
|
||||
return {
|
||||
"access_token": access_token,
|
||||
"user_id": user.id
|
||||
}
|
||||
|
||||
if not user:
|
||||
return None
|
||||
|
||||
if not user.check_password(password):
|
||||
return None
|
||||
|
||||
return user
|
||||
return None
|
||||
|
||||
@staticmethod
|
||||
def create_access_token(data: dict) -> str:
|
||||
|
||||
@ -7,7 +7,7 @@ from app.domain.entities.patient import PatientEntity
|
||||
from app.domain.models import Patient
|
||||
|
||||
|
||||
class PatientService:
|
||||
class PatientsService:
|
||||
def __init__(self, db: AsyncSession):
|
||||
self.patient_repository = PatientsRepository(db)
|
||||
|
||||
@ -10,7 +10,7 @@ from app.domain.entities.user import UserEntity
|
||||
from app.domain.models import User
|
||||
|
||||
|
||||
class UserService:
|
||||
class UsersService:
|
||||
def __init__(self, db: AsyncSession):
|
||||
self.user_repository = UsersRepository(db)
|
||||
self.role_repository = RolesRepository(db)
|
||||
@ -17,8 +17,8 @@ def start_app():
|
||||
allow_headers=['*'],
|
||||
)
|
||||
|
||||
api_app.include_router(auth_router, prefix=settings.APP_PREFIX)
|
||||
api_app.include_router(register_router, prefix=settings.APP_PREFIX)
|
||||
api_app.include_router(auth_router, prefix=settings.APP_PREFIX, tags=['auth'])
|
||||
api_app.include_router(register_router, prefix=settings.APP_PREFIX, tags=['register'])
|
||||
|
||||
return api_app
|
||||
|
||||
@ -26,6 +26,6 @@ def start_app():
|
||||
app = start_app()
|
||||
|
||||
|
||||
@app.get("/")
|
||||
@app.get("/", tags=['root'])
|
||||
async def root():
|
||||
return {"message": "OK"}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user