сделал генерацию jwt
This commit is contained in:
parent
f0aa556557
commit
3ec27706df
33
api/app/application/users_repository.py
Normal file
33
api/app/application/users_repository.py
Normal file
@ -0,0 +1,33 @@
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy.orm import joinedload
|
||||
|
||||
from app.domain.models.users import User
|
||||
|
||||
|
||||
class UsersRepository:
|
||||
def __init__(self, db: AsyncSession):
|
||||
self.db = db
|
||||
|
||||
def get_all(self):
|
||||
return self.db.query(User).all()
|
||||
|
||||
def get_by_id(self, user_id: int):
|
||||
return self.db.query(User).filter(User.id == user_id).first()
|
||||
|
||||
def get_by_login(self, user_login: str):
|
||||
return (
|
||||
self.db
|
||||
.query(User)
|
||||
.filter(User.login == user_login)
|
||||
.options(
|
||||
joinedload(User.role)
|
||||
)
|
||||
.first()
|
||||
)
|
||||
|
||||
def create(self, user: User):
|
||||
self.db.add(user)
|
||||
self.db.commit()
|
||||
self.db.refresh(user)
|
||||
|
||||
return user
|
||||
@ -1,5 +1,6 @@
|
||||
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
|
||||
from app.settings import settings
|
||||
|
||||
engine = create_async_engine(settings.DATABASE_URL, echo=True)
|
||||
@ -8,6 +9,7 @@ async_session_maker = sessionmaker(
|
||||
bind=engine, class_=AsyncSession, expire_on_commit=False
|
||||
)
|
||||
|
||||
|
||||
async def get_db():
|
||||
async with async_session_maker() as session:
|
||||
yield session
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
from sqlalchemy import Column, Integer, VARCHAR, ForeignKey, String
|
||||
from sqlalchemy.orm import relationship
|
||||
from werkzeug.security import check_password_hash, generate_password_hash
|
||||
|
||||
from app.domain.models import Base
|
||||
|
||||
@ -21,3 +22,9 @@ class User(Base):
|
||||
lens_issues = relationship('LensIssue', back_populates='doctor')
|
||||
appointments = relationship('Appointment', back_populates='doctor')
|
||||
mailing = relationship('Mailing', back_populates='user')
|
||||
|
||||
def check_password(self, password):
|
||||
return check_password_hash(self.password, password)
|
||||
|
||||
def set_password(self, password):
|
||||
self.password = generate_password_hash(password)
|
||||
|
||||
14
api/app/infrastructure/auth_service.py
Normal file
14
api/app/infrastructure/auth_service.py
Normal file
@ -0,0 +1,14 @@
|
||||
import datetime
|
||||
|
||||
from jose import jwt
|
||||
|
||||
from app.settings import get_auth_data
|
||||
|
||||
|
||||
def create_access_token(data: dict) -> str:
|
||||
to_encode = data.copy()
|
||||
expire = datetime.now(datetime.timezone.utc) + datetime.timedelta(days=30)
|
||||
to_encode.update({"exp": expire})
|
||||
auth_data = get_auth_data()
|
||||
encode_jwt = jwt.encode(to_encode, auth_data['secret_key'], algorithm=auth_data['algorithm'])
|
||||
return encode_jwt
|
||||
@ -5,6 +5,8 @@ class Settings(BaseSettings):
|
||||
DATABASE_URL: str
|
||||
LOG_LEVEL: str = "info"
|
||||
LOG_FILE: str = "logs/app.log"
|
||||
SECRET_KEY: str
|
||||
ALGORITHM: str
|
||||
|
||||
class Config:
|
||||
env_file = ".env"
|
||||
@ -12,3 +14,7 @@ class Settings(BaseSettings):
|
||||
|
||||
|
||||
settings = Settings()
|
||||
|
||||
|
||||
def get_auth_data():
|
||||
return {"secret_key": settings.SECRET_KEY, "algorithm": settings.ALGORITHM}
|
||||
|
||||
23
api/req.txt
Normal file
23
api/req.txt
Normal file
@ -0,0 +1,23 @@
|
||||
alembic==1.14.1
|
||||
annotated-types==0.7.0
|
||||
anyio==4.8.0
|
||||
asyncpg==0.30.0
|
||||
click==8.1.8
|
||||
fastapi==0.115.8
|
||||
greenlet==3.1.1
|
||||
h11==0.14.0
|
||||
idna==3.10
|
||||
Mako==1.3.8
|
||||
MarkupSafe==3.0.2
|
||||
psycopg==3.2.4
|
||||
psycopg-binary==3.2.4
|
||||
pydantic==2.10.6
|
||||
pydantic-settings==2.7.1
|
||||
pydantic_core==2.27.2
|
||||
python-dotenv==1.0.1
|
||||
sniffio==1.3.1
|
||||
SQLAlchemy==2.0.37
|
||||
starlette==0.45.3
|
||||
typing_extensions==4.12.2
|
||||
uvicorn==0.34.0
|
||||
Werkzeug==3.1.3
|
||||
Loading…
x
Reference in New Issue
Block a user