From 54acfe35866718d0f093f9886583492544a1438b Mon Sep 17 00:00:00 2001 From: lcsAndrade Date: Mon, 4 Dec 2023 16:53:23 -0300 Subject: [PATCH] =?UTF-8?q?Remove=20rotas=20n=C3=A3o=20utilizadas?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controller/authController.py | 30 ++++++++++++------------------ src/repository/userRepository.py | 21 --------------------- 2 files changed, 12 insertions(+), 39 deletions(-) diff --git a/src/controller/authController.py b/src/controller/authController.py index 61158c7..80519a4 100644 --- a/src/controller/authController.py +++ b/src/controller/authController.py @@ -66,24 +66,18 @@ async def login(data: authSchema.UserLogin, db: Session = Depends(get_db)): @auth.post("/login/social") async def login_social(user: authSchema.UserSocial, db: Session = Depends(get_db)): - - existing_user = userRepository.get_user_by_email(db, user.email) - if existing_user is None: - new_user = userRepository.create_user_social(db, user.name, user.email) - access_token = security.create_access_token(data={"id": new_user.id, "email": new_user.email, "role": new_user.role}) - return JSONResponse( - status_code=200, - content={ - "access_token": access_token, - "token_type": "bearer", - "is_new_user": True, - "user_id": new_user.id - } -) - else: - access_token = security.create_access_token(data={"id": existing_user.id, "email": existing_user.email, "role": existing_user.role}) - refresh_token = security.create_refresh_token(data={ "id": existing_user.id }) - return JSONResponse(status_code=200, content={"access_token": access_token, "refresh_token": refresh_token, "token_type": "bearer", "is_new_user": False}) + existing_user = userRepository.get_user_by_email(db, user.email) + + if existing_user is None: + new_user = userRepository.create_user_social(db, user.name, user.email) + access_token = security.create_access_token(data={"id": new_user.id, "email": new_user.email, "role": new_user.role}) + + return JSONResponse(status_code=200, content={ "access_token": access_token, "token_type": "bearer", "is_new_user": True, "user_id": new_user.id }) + else: + access_token = security.create_access_token(data={"id": existing_user.id, "email": existing_user.email, "role": existing_user.role}) + refresh_token = security.create_refresh_token(data={ "id": existing_user.id }) + + return JSONResponse(status_code=200, content={ "access_token": access_token, "refresh_token": refresh_token, "token_type": "bearer", "is_new_user": False }) @auth.post("/refresh", response_model=authSchema.RefreshTokenResponse) def refresh_token(token: dict = Depends(security.verify_token)): diff --git a/src/repository/userRepository.py b/src/repository/userRepository.py index 51077f8..c73ac60 100644 --- a/src/repository/userRepository.py +++ b/src/repository/userRepository.py @@ -58,27 +58,6 @@ def create_user_social(db: Session, name, email): db.refresh(db_user) return db_user -async def get_or_create_user(email: str, name: str, db: Session): - - user = userRepository.get_user_by_email(db, email) - - if user is None: - user = userRepository.create_by_login(db, name=name, email=email) - - return user - -def create_by_login(db: Session, name, email): - - db_user = userModel.User( - name=name, - email=email - ) - db.add(db_user) - db.commit() - db.refresh(db_user) - return db_user - - def update_user(db: Session, db_user: userSchema.User, user: userSchema.UserUpdate): user_data = user.dict(exclude_unset=True) for key, value in user_data.items():