Skip to content

Commit

Permalink
backend: 일부 주석 제거
Browse files Browse the repository at this point in the history
  • Loading branch information
kimtaejin3 committed Dec 12, 2024
1 parent b12c71b commit f52da2d
Showing 1 changed file with 3 additions and 13 deletions.
16 changes: 3 additions & 13 deletions backend/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,30 @@
from fastapi.middleware.cors import CORSMiddleware
from starlette.middleware.base import BaseHTTPMiddleware

# 시크릿 키와 알고리즘 설정
SECRET_KEY = "09d25e094faa6ca2556c818166b7a9563b93f7099f6f0f4caa6cf63b88e8d3e7"
ALGORITHM = "HS256"
ACCESS_TOKEN_EXPIRE_MINUTES = 30

app = FastAPI()

# CORS 설정 추가
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # 모든 도메인 허용 (보안상 필요에 따라 특정 도메인으로 제한)
allow_methods=["*"], # 모든 HTTP 메서드 허용
allow_headers=["*"], # 모든 헤더 허용
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)

# 비밀번호 해싱 설정
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")

# OAuth2 설정
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")

# DB 세션 의존성
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()

# JWT 토큰 생성 함수
def create_access_token(data: dict, expires_delta: timedelta | None = None):
to_encode = data.copy()
# if expires_delta:
Expand All @@ -53,14 +47,12 @@ def create_access_token(data: dict, expires_delta: timedelta | None = None):
encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
return encoded_jwt

# 사용자 인증 함수
def authenticate_user(db: Session, username: str, password: str):
user = db.query(User).filter(User.username == username).first()
if not user or not pwd_context.verify(password, user.password):
return None
return user

# 인증된 사용자 가져오기
async def get_current_user(token: Annotated[str, Depends(oauth2_scheme)], db: Session = Depends(get_db)):
credentials_exception = HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
Expand All @@ -80,7 +72,6 @@ async def get_current_user(token: Annotated[str, Depends(oauth2_scheme)], db: Se
raise credentials_exception
return user

# 회원 가입 엔드포인트
@app.post("/users/")
def create_user(user: UserCreate, db: Session = Depends(get_db)):
existing_user = db.query(User).filter(User.username == user.username).first()
Expand All @@ -107,7 +98,6 @@ def create_user(user: UserCreate, db: Session = Depends(get_db)):
async def read_users_me(current_user: Annotated[User, Depends(get_current_user)]):
return current_user

# 토큰 발급 엔드포인트
@app.post("/token")
async def login_for_access_token(
form_data: UserCreate,
Expand Down

0 comments on commit f52da2d

Please sign in to comment.