-
Notifications
You must be signed in to change notification settings - Fork 0
refactor/suggestions_to_refactor_code #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ArmCM
wants to merge
2
commits into
develop
Choose a base branch
from
refactor/suggestions_to_refactor_code
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,41 @@ | ||
from fastapi import status,APIRouter | ||
from fastapi import status, APIRouter | ||
from fastapi.responses import JSONResponse | ||
from utils.jwt_manager import create_token | ||
from schemas.user import User,UserBase,UserCreate | ||
from schemas.user import User, UserBase, UserCreate | ||
from config.database import Session | ||
from services.user import UserService | ||
from services.auth import Auth | ||
|
||
user_router = APIRouter() | ||
db = Session() | ||
|
||
|
||
@user_router.post('/users', tags=['Auth'], response_model=User, status_code=status.HTTP_200_OK) | ||
def create_user(user: UserCreate): | ||
if check_user_exists(user): | ||
return JSONResponse(status_code=status.HTTP_400_BAD_REQUEST, content={"message": "User already exists"}) | ||
|
||
@user_router.post('/users',tags=['Auth'],response_model=User,status_code=status.HTTP_200_OK) | ||
def create_user(user:UserCreate): | ||
|
||
db = Session() | ||
|
||
result = UserService(db).get_user_by_email(email=user.email) | ||
|
||
if result: | ||
|
||
return JSONResponse(status_code=status.HTTP_400_BAD_REQUEST,content={"message":"User already exists"}) | ||
|
||
UserService(db).create_user(user) | ||
|
||
return JSONResponse(status_code=status.HTTP_200_OK,content={"message":"User created"}) | ||
|
||
|
||
@user_router.post('/login',tags=['Auth'],status_code=status.HTTP_200_OK) | ||
def login(user:UserCreate): | ||
|
||
db = Session() | ||
result = UserService(db).get_user_by_email(email=user.email) | ||
|
||
if not (result and Auth().verify_password(user.password,result.password)): | ||
|
||
return JSONResponse(status_code=status.HTTP_401_UNAUTHORIZED,content={"message":"Unauthorized"}) | ||
|
||
token:str = create_token(user.dict()) | ||
|
||
return JSONResponse(status_code=status.HTTP_200_OK,content=token) | ||
|
||
return JSONResponse(status_code=status.HTTP_200_OK, content={"message": "User created"}) | ||
|
||
|
||
def check_user_exists(user): | ||
return bool(UserService(db).get_user_by_email(email=user.email)) | ||
|
||
|
||
@user_router.post('/login', tags=['Auth'], status_code=status.HTTP_200_OK) | ||
def login(user: UserCreate): | ||
|
||
if validates_password(user): | ||
return JSONResponse(status_code=status.HTTP_401_UNAUTHORIZED, content={"message": "Unauthorized"}) | ||
|
||
token: str = create_token(user.dict()) | ||
|
||
return JSONResponse(status_code=status.HTTP_200_OK, content=token) | ||
|
||
|
||
def validates_password(user): | ||
user_found = UserService(db).get_user_by_email(email=user.email) | ||
|
||
return not bool(check_user_exists(user) and Auth().verify_password(user.password, user_found.password)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,29 @@ | ||
from jwt import encode,decode | ||
from datetime import datetime,timedelta | ||
from jwt import encode, decode | ||
from datetime import datetime, timedelta | ||
from utils.settings import Settings | ||
|
||
settings = Settings() | ||
|
||
def create_token(data:dict) -> dict: | ||
payload = expire_token(data) | ||
token:str = encode(payload,key=settings.MY_SECRET_KEY,algorithm="HS256") | ||
|
||
def create_token(data: dict) -> dict: | ||
payload = add_expiration_date(data) | ||
|
||
token: str = encode(payload, key=settings.MY_SECRET_KEY, algorithm="HS256") | ||
return token | ||
|
||
def validate_token(token:str) -> dict: | ||
data:dict = decode(token,key=settings.MY_SECRET_KEY,algorithms=["HS256"]) | ||
|
||
def validate_token(token: str) -> dict: | ||
data: dict = decode(token, key=settings.MY_SECRET_KEY, algorithms=["HS256"]) | ||
|
||
return data | ||
|
||
def expire_token(data:dict): | ||
|
||
def calculate_token_expiration(): | ||
return datetime.utcnow() + timedelta(minutes=settings.TOKEN_EXPIRE_MINUTES) | ||
|
||
|
||
def add_expiration_date(data: dict): | ||
to_encode = data.copy() | ||
token_expires = timedelta(minutes=settings.TOKEN_EXPIRE_MINUTES) | ||
expire = datetime.utcnow() + token_expires | ||
to_encode.update({'exp':expire}) | ||
return to_encode | ||
to_encode['exp'] = calculate_token_expiration() | ||
|
||
return to_encode |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
duda 🤔 , cual es el tipo de dato exacto que debe ser el token?
ya que aqui indicas que
token
debe ser de tipostring
pero el métodocreate_token
retorna undict
.y el IDE me marco un error, busque y me indica el
dict
esta deprecated para la versión 2 depydantic
https://docs.pydantic.dev/latest/migration/#changes-to-pydanticbasemodel
pero desconozco si se hizo así por alguna razón .Aquí es a tu consideración bro