Skip to content

Commit 21ba8d4

Browse files
committed
Jwt and Alembic added
1 parent d5e80d7 commit 21ba8d4

25 files changed

+362
-72
lines changed

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/DTO/CreateUserDto.py

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

app/controllers/authController.py

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,37 @@
1-
from fastapi import HTTPException
21
from starlette import status
2+
from starlette.responses import JSONResponse
33

4-
from app.DTO import CreateUserDto
4+
from app.dto import CreateUserDto
5+
from app.dto.LoginUserDto import LoginUserDto
56
from app.services import UserService
6-
from app.utilities import formating
77

88

9-
async def registerUser(user: CreateUserDto):
10-
email = formating.format_string(user.email)
11-
12-
if not email:
13-
raise HTTPException(
14-
detail="Email can not be empty",
15-
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY
16-
)
17-
18-
if not user.password:
19-
raise HTTPException(
20-
detail="Password can not be empty",
21-
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY
22-
)
9+
async def registerUser(user: CreateUserDto) -> JSONResponse:
2310

2411
# Process Register in UserService
2512
user = UserService.register(user)
2613

27-
return user
14+
return JSONResponse(
15+
status_code=status.HTTP_201_CREATED,
16+
content={
17+
"status": "success",
18+
"message": "User created successfully",
19+
"data": user.toJson()
20+
},
21+
headers={"Content-Type": "application/json"}
22+
)
23+
24+
async def login(body: LoginUserDto):
25+
26+
token = UserService.login(body=body)
27+
28+
return JSONResponse(
29+
status_code=status.HTTP_201_CREATED,
30+
content={
31+
"status": "success",
32+
"message": "Login successfully",
33+
"token": token,
34+
"type": "bearer"
35+
},
36+
headers={"Content-Type": "application/json"}
37+
)

app/dto/CreateUserDto.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import re
2+
3+
from pydantic import BaseModel, Field, EmailStr
4+
5+
password_regex = r"^(?=.*\d)(?=.*[a-zA-Z]).{8,}$"
6+
7+
class CreateUserDto(BaseModel):
8+
name: str
9+
email: EmailStr
10+
password: str = Field(default="password", pattern=re.compile(password_regex))
File renamed without changes.

app/dto/LoginUserDto.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from pydantic import BaseModel, EmailStr
2+
3+
class LoginUserDto(BaseModel):
4+
email: EmailStr
5+
password: str
6+
File renamed without changes.

app/models/User.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
from sqlalchemy import Integer, String, Column, DateTime, ForeignKey
1+
from sqlalchemy import Integer, String, Column, DateTime
22
from sqlalchemy.sql.functions import current_timestamp
3-
from sqlalchemy.orm import relationship
43

54
from app.models import Base
65

@@ -12,4 +11,13 @@ class User(Base):
1211
name = Column("name", String(100))
1312
password = Column("hashed_password", String(255))
1413
updated_at = Column("updated_at", DateTime(), default=current_timestamp())
15-
created_at = Column("created_at", DateTime(), default=current_timestamp())
14+
created_at = Column("created_at", DateTime(), default=current_timestamp())
15+
16+
def toJson(self):
17+
return {
18+
"id": self.id,
19+
"name": self.name,
20+
"email": self.email,
21+
"updated_at": str(self.updated_at),
22+
"created_at": str(self.created_at)
23+
}

app/repositories/userRepository.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from fastapi import HTTPException
22

33
from app.models.User import User
4-
from app.database.Config import SessionLocal
4+
from database.config import SessionLocal
55

66

77
def store(name: str, email: str, password: str)-> User:
@@ -30,8 +30,8 @@ def store(name: str, email: str, password: str)-> User:
3030
detail=str(e))
3131

3232
def getbyid(id: int) -> User:
33-
user = SessionLocal.query(User).filter(User.id == id).first()
34-
return user
33+
with SessionLocal() as session:
34+
return session.query(User).filter(User.id == id).first()
3535

3636
def getByEmail(email: str):
3737
with SessionLocal() as session:

app/routes/api.py

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

0 commit comments

Comments
 (0)