Skip to content
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

feat: development 환경에서 회원가입, 로그인, 인증 기능 추가 #27

Open
wants to merge 2 commits into
base: file-file-file
Choose a base branch
from

Conversation

mjkweon17
Copy link
Member

No description provided.

Comment on lines -33 to -34
if not user.is_valid:
raise credentials_exception
Copy link
Member Author

@mjkweon17 mjkweon17 Aug 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

user 테이블에서 is_valid가 사라져서 해당 부분을 그대로 냅두면 에러가 발생합니다. 그래서 코드 삭제했습니다.

Copy link
Collaborator

@smreosms13 smreosms13 Aug 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

user.is_deleted로 수정한 다음 시도해보세요

Comment on lines +17 to 19
class FirebaseLoginRequest(BaseModel):
email: str = Field(..., example="[email protected]")
password: str = Field(..., example="asdf1234")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prod 환경에서 로그인할 때는 firebase에 저장된 사용자 정보(lms 회원가입/로그인할 때 사용되는 정보)를 받아야 합니다. 이 스키마는 그때 사용됩니다.

Comment on lines +22 to +23
class LoginRequest(BaseModel):
auth_id: str = Field(..., example="테스트 이름")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

local, exp에서 로그인할 때 사용되는 schema입니다.

Comment on lines +14 to 31
async def register(request: auth_schemas.RegisterRequest, db: Session):

# Check if user information exists in the DB
user = db.query(User).filter(User.id == user_id).first()
user = db.query(User).filter(User.user_name == request.user_name).first()

# If user name is TEMP_USER_NAME(not registered), update user name, else reutrn error(already registered)
if user.user_name == Settings().TEMP_USER_NAME:
user.user_name = request.user_name
user.is_active = request.is_active
# If user information does not exist in the DB, create a new user
if user is None:
user = User(
auth_id=request.user_name,
auth_type='EXP',
email="none",
user_name=request.user_name,
is_active=True
)
db.add(user)
db.commit()
db.refresh(user)
else:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prod 환경에서는 lms 계정으로 로그인하면 돼서 쿠책책에서 회원가입을 할 필요가 없습니다. 그래서 이 register 함수는 local, exp 환경에서 테스트 계정을 만들기 위한 회원가입을 할 떄 사용됩니다.

Comment on lines +47 to 52
# firebase를 사용한 로그인

async def login(request, db: Session):

async def login_with_firebase(request, db: Session):
# Authenticate user
# Check if user exists in Firebase
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

firebase로 로그인하는 부분은... 아직 작동하지 않을 겁니다. 빠른 시일 내로 작동되게 수정하겠습니다.

Comment on lines +93 to +119
async def login_with_username(
request: auth_schemas.LoginRequest,
db: Session):
# Authenticate user
# Check if user information exists in the DB
user = db.query(User).filter(User.auth_id == request.auth_id).first()

# If user information does not exist in the DB, return error
if user is None:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="User not found")

# Check if the user is active
if not user.is_active:
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="User disabled")

# Create JWT tokens
token_response = create_user_tokens(user.id)

return {
"token": token_response,
"user": {
"id": user.id,
"user_name": user.user_name,
"is_active": user.is_active,
"email": user.email
}
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

얘도 local, exp 환경에서 로그인할 때 사용되는 함수입니다. 로그인할 때 request body에다가 username만 넣으면 db에 해당 username이 있는지 확인하고 인증해주는 기능을 합니다.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여기서 response schema는 따로 설계해서 domain.schemas에 올리지 않는 이유가 있을까요?

Comment on lines -23 to -24
created_at = Column(DateTime, nullable=False)
updated_at = Column(DateTime, nullable=False)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

모든 테이블의 created_at하고 updated_at에 nullable하지 않아서 테이블들에 새로운 row를 만들 때마다 updated_at하고 created_at에 값을 넣어줘야 합니다. 그런데 models.py에 있는 테이블들의 class에 default 값이 없어서 domain layer에서 시간을 넣어줘야 하는 문제가 있었습니다. 이를 해결하기 위해 models.py의 created_at, updated_at에 현재 시간을 넣어주도록 코드를 추가했습니다.

Comment on lines 61 to +67
request: auth_schemas.LoginRequest,
db: Session = Depends(get_db)
):
return await auth_service.login(request, db)
if settings.ENVIRONMENT == "development":
return await auth_service.login_with_username(request, db)
# elif settings.ENVIRONMENT == "production":
# return await auth_service.login(request, db)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

login api입니다. 현재 환경이 development 인지 production(prod)인지에 따라 분기쳐서 다른 함수를 사용하도록 했습니다. firebase로 로그인하는 함수가 아직 완성되지 않아서 prod 부분은 일단 주석처리했습니다.

Comment on lines +21 to 29
@router.post("", response_model=food_schemas.Food)
def create_food(
food: food_schemas.FoodCreate,
db: Session = Depends(get_db),
get_current_active_user=Depends(get_current_active_user)
):
print(get_current_active_user)
print(get_current_active_user.user_name)
return food_service.create_food(db, food)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get_current_active_user를 사용하는 예시 코드입니다.


# Check if user information exists in the DB
user = db.query(User).filter(User.id == user_id).first()
user = db.query(User).filter(User.user_name == request.user_name).first()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여기도 .filter() 말고 admin_service.py get_item이나 SQLAlchemy 2.0으로 하면 좋을 것 같습니다!

user.user_name = request.user_name
user.is_active = request.is_active
# If user information does not exist in the DB, create a new user
if user is None:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

user is None으로 하면 user.is_deleted == true 인 user도 해당되지 않는지 궁금합니다

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants