Skip to content

Commit 21ac5c4

Browse files
committed
deps.py перенес в core, добил anotations в api, изменил зависимость get_jwt_manager на get_security, изменил API
1 parent 2f4fcef commit 21ac5c4

File tree

19 files changed

+145
-138
lines changed

19 files changed

+145
-138
lines changed

app/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from fastapi import FastAPI
66

77
from app import api
8-
from app.core import settings
8+
from app.core.settings import settings
99

1010
app = FastAPI(
1111
title=settings.APP_TITLE,

app/api/anotations.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""
2+
Anotations
3+
"""
4+
5+
from fastapi import Depends
6+
from typing_extensions import Annotated
7+
8+
from app.core import deps
9+
10+
Database = Annotated[deps.db.Database, Depends(deps.get_db)]
11+
12+
Security = Annotated[deps.Security, Depends(deps.get_security)]
13+
14+
CurrentUser = Annotated[deps.User, Depends(deps.get_current_user)]

app/api/deps.py

Lines changed: 0 additions & 36 deletions
This file was deleted.

app/api/endpoints/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44

55
from fastapi import APIRouter
66

7-
from . import auth, users
7+
from . import users
88

99
router = APIRouter()
10-
router.include_router(auth.router)
1110
router.include_router(users.router)
1211

1312
__all__ = ['router']

app/api/endpoints/auth/__init__.py

Lines changed: 0 additions & 8 deletions
This file was deleted.

app/api/endpoints/auth/email/__init__.py

Lines changed: 0 additions & 3 deletions
This file was deleted.

app/api/endpoints/auth/email/endpoints.py

Lines changed: 0 additions & 44 deletions
This file was deleted.

app/api/endpoints/users/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44

55
from fastapi import APIRouter
66

7-
from . import retrieve
7+
from . import auth, create, retrieve
88

99
router = APIRouter(prefix='/users', tags=['users'])
10+
router.include_router(auth.router)
11+
router.include_router(create.router)
1012
router.include_router(retrieve.router)
1113

1214
__all__ = ['router']

app/api/endpoints/users/auth.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from fastapi import APIRouter
2+
3+
from app.api import anotations
4+
from app.core import deps, exps
5+
from app.models.token import AccessToken
6+
from app.models.user import UserCreate
7+
8+
router = APIRouter(prefix="/auth")
9+
10+
11+
@router.post("/token/", response_model=AccessToken)
12+
async def token(
13+
data: UserCreate, db: anotations.Database, security: anotations.Security
14+
):
15+
"""
16+
Retrieve new access token
17+
"""
18+
if user := await db.user.retrieve_by_email(data.email):
19+
if not deps.pwd_context.verify(data.password, user.password):
20+
raise exps.USER_IS_CORRECT
21+
access_token = security.jwt.encode_token({"id": user.id}, 1440)
22+
return AccessToken(token=access_token)
23+
24+
raise exps.USER_NOT_FOUND

app/api/endpoints/users/create.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from fastapi import APIRouter
2+
3+
from app.api import anotations
4+
from app.core import deps, exps
5+
from app.models.user import User, UserCreate, UserRead
6+
7+
router = APIRouter(prefix="/create")
8+
9+
10+
@router.post("/", response_model=UserRead)
11+
async def registration(
12+
data: UserCreate,
13+
db: anotations.Database,
14+
):
15+
"""
16+
Create user
17+
"""
18+
if await db.user.retrieve_by_email(data.email):
19+
raise exps.USER_EXISTS
20+
21+
password_hash = deps.pwd_context.hash(data.password)
22+
model = User(email=data.email, password=password_hash)
23+
user = await db.user.create(model)
24+
return user

0 commit comments

Comments
 (0)