Skip to content

Commit

Permalink
Merge pull request #17 from fga-eps-mds/fix-login-social
Browse files Browse the repository at this point in the history
Fix login social
  • Loading branch information
castroricardo1 authored Dec 5, 2023
2 parents 4196a0d + c5ca957 commit 2c80e24
Show file tree
Hide file tree
Showing 13 changed files with 76 additions and 656 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,4 @@ websocket-client==1.6.4
websockets==11.0.3
wrapt==1.15.0
zope.interface==6.1
fastapi_filter
15 changes: 15 additions & 0 deletions src/controller/authController.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,21 @@ async def login(data: authSchema.UserLogin, db: Session = Depends(get_db)):

return JSONResponse(status_code=200, content={ "access_token": access_token, "refresh_token": refresh_token, "token_type": "bearer" })

@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 })

@auth.post("/refresh", response_model=authSchema.RefreshTokenResponse)
def refresh_token(token: dict = Depends(security.verify_token)):
access_token=security.create_access_token(token)
Expand Down
72 changes: 0 additions & 72 deletions src/controller/facebookController.py

This file was deleted.

49 changes: 0 additions & 49 deletions src/controller/googleController.py

This file was deleted.

4 changes: 4 additions & 0 deletions src/domain/authSchema.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ class UserCreate(BaseModel):
email: str
password: str

class UserSocial(BaseModel):
name: str
email:str

class Token(BaseModel):
access_token: str
refresh_token: str
Expand Down
4 changes: 1 addition & 3 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
load_dotenv()
dotenv.validate_dotenv()

from controller import userController, authController, googleController, facebookController
from controller import userController, authController
from database import engine
from model import userModel

Expand All @@ -30,8 +30,6 @@
# Routers
app.include_router(prefix="/api", router=authController.auth)
app.include_router(prefix="/api", router=userController.user)
app.include_router(router=googleController.google)
app.include_router(router=facebookController.facebook)

@app.get("/")
def read_root():
Expand Down
4 changes: 2 additions & 2 deletions src/model/userModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ class User(Base):
connection = Column(String, nullable=False)
role = Column(String, default="USER")
email = Column(String, unique=True, index=True, nullable=False)
password = Column(String, nullable=False)
password = Column(String, nullable=True)
is_active = Column(Boolean, default=False)
activation_code = Column(Integer, nullable=True)
password_reset_code = Column(Integer, nullable=True)


30 changes: 11 additions & 19 deletions src/repository/userRepository.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,26 +44,18 @@ def create_user(db: Session, name, connection, email, password, activation_code)
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 create_user_social(db: Session, name, email):
db_user = userModel.User(
name=name,
connection="ESTUDANTE",
role="USER",
email=email,
is_active=True,)

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)
Expand Down
Loading

0 comments on commit 2c80e24

Please sign in to comment.