From f52da2d635d5e404a9ca240c65ca50d47683a671 Mon Sep 17 00:00:00 2001 From: kimtaejin3 Date: Thu, 12 Dec 2024 20:57:54 +0900 Subject: [PATCH] =?UTF-8?q?backend:=20=EC=9D=BC=EB=B6=80=20=EC=A3=BC?= =?UTF-8?q?=EC=84=9D=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/main.py | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/backend/main.py b/backend/main.py index 4c08677..f575132 100644 --- a/backend/main.py +++ b/backend/main.py @@ -13,28 +13,23 @@ 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: @@ -42,7 +37,6 @@ def get_db(): finally: db.close() -# JWT 토큰 생성 함수 def create_access_token(data: dict, expires_delta: timedelta | None = None): to_encode = data.copy() # if expires_delta: @@ -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, @@ -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() @@ -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,