Skip to content

Commit 2edb1a8

Browse files
committed
Auth feature done
1 parent 2df5597 commit 2edb1a8

File tree

13 files changed

+123
-37
lines changed

13 files changed

+123
-37
lines changed

.env.example

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
DB_CONNECTION=mysql
2+
DB_HOST=localhost
3+
DB_USERNAME=root
4+
DB_PASSWORD=
5+
DB_NAME=testing
6+
DB_PORT=3306
7+
8+
HASH_SALT=
9+
HASH_ALGORITHM=HS256

app/controllers/authController.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from app.services import UserService
77

88

9-
async def registerUser(user: CreateUserDto) -> JSONResponse:
9+
async def registerUserController(user: CreateUserDto) -> JSONResponse:
1010

1111
# Process Register in UserService
1212
user = UserService.register(user)
@@ -21,7 +21,7 @@ async def registerUser(user: CreateUserDto) -> JSONResponse:
2121
headers={"Content-Type": "application/json"}
2222
)
2323

24-
async def login(body: LoginUserDto):
24+
async def loginController(body: LoginUserDto):
2525

2626
token = UserService.login(body=body)
2727

app/controllers/userController.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from starlette import status
2+
from starlette.responses import JSONResponse
3+
4+
from app.services import UserService
5+
from fastapi.encoders import jsonable_encoder
6+
7+
8+
async def getUsersController():
9+
# Get User in with services
10+
users = UserService.getUsers()
11+
12+
return JSONResponse(
13+
status_code=status.HTTP_200_OK,
14+
content={
15+
"status": "success",
16+
"message": "Success get all users",
17+
"data": jsonable_encoder(users),
18+
},
19+
headers={"Content-Type": "application/json"}
20+
)

app/middlewares/__init__.py

Whitespace-only changes.

app/middlewares/cors_middleware.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from fastapi import FastAPI
2+
from fastapi.middleware.cors import CORSMiddleware
3+
4+
5+
# function for enabling CORS on web server
6+
def add(app: FastAPI):
7+
app.add_middleware(
8+
CORSMiddleware,
9+
allow_origins=["*"],
10+
allow_credentials=True,
11+
allow_methods=["*"],
12+
allow_headers=["*"])

app/repositories/userRepository.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,9 @@ def getbyid(id: int) -> User:
3535

3636
def getByEmail(email: str):
3737
with SessionLocal() as session:
38-
return session.query(User).where(User.email == email).first()
38+
return session.query(User).where(User.email == email).first()
39+
40+
41+
def getUsers():
42+
with SessionLocal() as session:
43+
return session.query(User).all()

app/services/UserService.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,9 @@ def login(body: LoginUserDto):
5151
token = create_access_token(payload={"user_id": user.id}, expires_delta=access_token_expires)
5252

5353
return token
54+
55+
56+
def getUsers():
57+
# Handle query with User Repository to get All users
58+
users = userRepository.getUsers()
59+
return users

database/config.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
SQLALCHEMY_TRACK_MODIFICATIONS = False
2525
SQLALCHEMY_DATABASE_URL = f"{DB_CONNECTION}://{DB_USERNAME}:{DB_PASSWORD}@{DB_HOST}:{DB_PORT}/{DB_NAME}"
2626

27-
print(SQLALCHEMY_DATABASE_URL)
2827

2928
engine = create_engine(SQLALCHEMY_DATABASE_URL)
3029
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)

exceptions/__init__.py

Whitespace-only changes.

exceptions/validation_exception.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from fastapi import FastAPI
2+
from fastapi.exceptions import RequestValidationError
3+
from starlette.responses import JSONResponse
4+
5+
6+
def add(app: FastAPI):
7+
@app.exception_handler(RequestValidationError)
8+
async def validation_exception_handler(request, exc: RequestValidationError):
9+
error_format = {
10+
"status": "fail",
11+
"message": "Request Validation Error",
12+
"errors": []
13+
}
14+
field_list = []
15+
for error in exc.errors():
16+
field_error = {
17+
"type": error["type"],
18+
"location": error["loc"][0],
19+
"field": error["loc"][1],
20+
"message": error["msg"]
21+
}
22+
field_list.append(field_error)
23+
24+
error_format["errors"] = field_list
25+
26+
return JSONResponse(status_code=422, content=error_format)

0 commit comments

Comments
 (0)